124 lines
2.0 KiB
C
124 lines
2.0 KiB
C
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#define LINESIZE 100
|
|
struct heap {
|
|
int size;
|
|
int* line;
|
|
int capacity;
|
|
};
|
|
|
|
int parent(int i){
|
|
return (i -1) / 2;
|
|
}
|
|
|
|
int lavy_syn(int i){
|
|
return (2*i) + 1;
|
|
}
|
|
|
|
int pravy_syn(int i){
|
|
return (2*i) + 2;
|
|
}
|
|
|
|
struct heap* create_heap(int capacity){
|
|
struct heap* h = calloc(1,sizeof(struct heap));
|
|
h->line = calloc(capacity,sizeof(int));
|
|
h->capacity =capacity;
|
|
h->size = 0;
|
|
return h;
|
|
}
|
|
|
|
void check_heap_property(struct heap* h){
|
|
for(int i =0;i<h->size;i++){
|
|
|
|
int lavy = lavy_syn(i);
|
|
int pravy = pravy_syn(i);
|
|
|
|
if(pravy >= h->size || lavy>=h->size){
|
|
break;
|
|
}
|
|
|
|
if(h->line[i] > h->line[pravy] || h->line[i] > h->line[lavy]){
|
|
printf("%s\n","Niejekopa.");
|
|
exit(0);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void delete_heap(struct heap* h){
|
|
free(h->line);
|
|
free(h);
|
|
}
|
|
|
|
//void vymen(int arr[],int a, int b){
|
|
// int pom = arr[a];
|
|
// arr[a] = arr[b];
|
|
// arr[b] = pom;
|
|
//}
|
|
|
|
void add(struct heap* h,int value){
|
|
|
|
int i = h->size;
|
|
if(h->size>=h->capacity){
|
|
return;
|
|
}
|
|
|
|
h->size += 1;
|
|
h->line[i] = value;
|
|
|
|
|
|
}
|
|
|
|
void print(struct heap* h,int count_spaces,int index){
|
|
|
|
if(index>=h->size){
|
|
return ;
|
|
}
|
|
|
|
|
|
for(int i =0;i<count_spaces;i++){
|
|
printf(" ");
|
|
}
|
|
printf("%d\n",h->line[index]);
|
|
print(h,count_spaces+1,lavy_syn(index));
|
|
print(h,count_spaces+1,pravy_syn(index));
|
|
|
|
|
|
|
|
}
|
|
|
|
void sort(struct heap *h){
|
|
int i = h->size-1;
|
|
int p = parent(i);
|
|
|
|
while(h->line[i] < h->line[p]) {
|
|
int arr[] = h->line;
|
|
int pom = arr[i];
|
|
arr[i] = arr[p];
|
|
arr[p] = pom;
|
|
//vymen(h->line,i, p);
|
|
i = parent(i);
|
|
}
|
|
}
|
|
|
|
int main(){
|
|
int i = 0;
|
|
int temp_line[LINESIZE] = {0};
|
|
|
|
while(scanf("%d",&temp_line[i])== 1) {
|
|
i++;
|
|
}
|
|
|
|
struct heap* h = create_heap(i);
|
|
|
|
for(int j =0;j<i;j++){
|
|
add(h,temp_line[j]);
|
|
}
|
|
|
|
check_heap_property(h);
|
|
sort(h);
|
|
puts("Jetotakakopa:");
|
|
|
|
print(h,0,0);
|
|
return 0;
|
|
} |