From b884843e8ec50d16c649c69c608632dd9098b76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Korpesio?= Date: Thu, 2 Dec 2021 13:19:19 +0100 Subject: [PATCH] prvy --- a4/program.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 a4/program.c diff --git a/a4/program.c b/a4/program.c new file mode 100644 index 0000000..c0915b9 --- /dev/null +++ b/a4/program.c @@ -0,0 +1,120 @@ +#include +#include +#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;isize;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;iarray[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