usaa24/a4/program.c
2024-11-27 18:27:03 +00:00

39 lines
857 B
C

#include <stdio.h>
#include <stdbool.h>
bool is_min_heap(int arr[], int n) {
for (int i = 0; i < n; i++) {
int l = 2 * i + 1;
int r = 2 * i + 2;
if ((l < n && arr[i] > arr[l]) || (r < n && arr[i] > arr[r])) {
return false;
}
}
return true;
}
void preorder(int arr[], int n, int index, int level) {
if (index >= n) return;
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%d\n", arr[index]);
preorder(arr, n, 2 * index + 1, level + 1);
preorder(arr, n, 2 * index + 2, level + 1);
}
int main() {
int arr[100];
int n = 0;
while (scanf("%d", &arr[n]) != EOF) {
n++;
}
if (is_min_heap(arr, n)) {
printf("Je to taka kopa:\n");
preorder(arr, n, 0, 0);
} else {
printf("Nie je kopa.\n");
}
return 0;
}