120 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include<stdio.h>
 | 
						|
#include<stdlib.h>
 | 
						|
#define LINESIZE 100
 | 
						|
struct heap {
 | 
						|
    int* array;
 | 
						|
    int size;
 | 
						|
    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->array = 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->array[i] > h->array[pravy] || h->array[i] > h->array[lavy]){
 | 
						|
            printf("%s\n","Niejekopa.");
 | 
						|
            exit(0);
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void delete_heap(struct heap* h){
 | 
						|
    free(h->array);
 | 
						|
    free(h);
 | 
						|
}
 | 
						|
 | 
						|
void swap(int arr[],int a, int b){
 | 
						|
    int temp = arr[a];
 | 
						|
    arr[a] = arr[b];
 | 
						|
    arr[b] = temp;
 | 
						|
}
 | 
						|
 | 
						|
void add(struct heap* h,int value){
 | 
						|
 | 
						|
    int i = h->size;
 | 
						|
    if(h->size>=h->capacity){
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    h->size += 1;
 | 
						|
    h->array[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->array[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->array[i] < h->array[p]) {
 | 
						|
        swap(h->array,i, p);
 | 
						|
        i = parent(i);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
int main(){
 | 
						|
    int i  =0;
 | 
						|
    int temp_array[LINESIZE] = {0};
 | 
						|
 | 
						|
    while(scanf("%d",&temp_array[i]) == 1) {
 | 
						|
        i++;
 | 
						|
    }
 | 
						|
 | 
						|
    struct heap* h = create_heap(i);
 | 
						|
 | 
						|
    for(int j =0;j<i;j++){
 | 
						|
        add(h,temp_array[j]);
 | 
						|
    }
 | 
						|
 | 
						|
    check_heap_property(h);
 | 
						|
    sort(h);
 | 
						|
    puts("Jetotakakopa:");
 | 
						|
 | 
						|
    print(h,0,0);
 | 
						|
    return 0;
 | 
						|
} |