First try
This commit is contained in:
parent
b9974c6fc8
commit
868b56cc67
108
a2/program.c
Normal file
108
a2/program.c
Normal file
@ -0,0 +1,108 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
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; i<depth; i++){
|
||||
printf(" ");
|
||||
}
|
||||
printf("%d\n", heap[index]->vek);
|
||||
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 && r<pocet && i<pocet)
|
||||
|
||||
{
|
||||
|
||||
if(pole[l]->vek <pole[i]->vek || pole[r]->vek <pole[i]->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;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user