From 868b56cc6747ba83fe71a12e61a2329140f17314 Mon Sep 17 00:00:00 2001 From: Anton Dolozin Date: Mon, 24 Nov 2025 13:52:09 +0100 Subject: [PATCH] First try --- a2/program.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 a2/program.c diff --git a/a2/program.c b/a2/program.c new file mode 100644 index 0000000..a563d3c --- /dev/null +++ b/a2/program.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include + +struct osoba{ + int vek; +}; +// Počet prvkov v kope sa zväčší o jedna +void add_person(struct osoba** heap,int hsz, int vek ){ + struct osoba* item = calloc(1,sizeof(struct osoba)); + item->vek = vek; + // Uložíme na koniec + heap[hsz] = item; + // Pozrieme na jeho rodiča + int p = (hsz -1) / 2; + int i = hsz; + // Pokiaľ rodič existuje a jeho hodnota je väčšia + while(p >= 0 && heap[p]->vek > heap[i]->vek){ + // Vymeníme ich hodnoty + struct osoba* this = heap[i]; + heap[i] = heap[p]; + heap[p] = this; + // Pokračujeme ďalším rodičom + i = p; + p = (p - 1) / 2; + } +} +void heapify(struct osoba** heap, int hsz,int i){ + struct osoba* this = heap[i]; + int left_index = (2*i) + 1; + int right_index = (2*i) + 2; + // Zistíme kde sa nachádza minimum + int m = i; + if (left_index < hsz && heap[left_index]->vek < this->vek){ + m = left_index; + } + if (right_index < hsz && heap[right_index]->vek < this->vek){ + m = right_index; + } + if (m != i){ + heap[i] = heap[m]; + heap[m] = this; + heapify(heap,hsz,m); + } +} +void print_heap_with_levels(struct osoba** heap, int size, int index, int depth){ + if(index >= size || heap[index] == NULL){ + return; + } + for(int i = 0; ivek); + print_heap_with_levels(heap, size, 2*index+1, depth+1); + print_heap_with_levels(heap, size, 2*index+2, depth+1); + +} +int main(){ + struct osoba* pole[20]; + memset(pole,0, sizeof(struct osoba*)); + int pocet = 0; + char buff[20]; + if(!fgets(buff, 20, stdin)){ + printf("Chyba\n"); + return 0; + } + buff[strcspn(buff, "\n")] = '\0'; + char* token = strtok(buff, " "); + while (token) + { + char* endptr; + if(endptr == token){ + printf("Nie je to kopa\n"); + return 0; + } + int val = strtod(token, &endptr); + struct osoba* temp = malloc(sizeof(struct osoba)); + temp->vek = val; + pole[pocet] = temp; + pocet++; + token = strtok(NULL, " ");} + + + + + + int i = 0; +int l = i*2 +1; + int r = i*2 +2; + while (l< pocet && rvek vek || pole[r]->vek vek){ + printf("Nie je to kopa\n"); + return 0; + } + i++; + l = i*2 +1; + r = i*2 +2; + + } + printf("Je to taka kopa:\n"); + print_heap_with_levels(pole, pocet, 0, 0); + return 0; + +}