This commit is contained in:
Weber 2024-11-27 18:27:03 +00:00
parent 7e07d88bd4
commit be4b8cb12e

View File

@ -1,35 +1,38 @@
def is_min_heap(arr):
n = len(arr)
for i in range(n):
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] > arr[l]:
return False
if r < n and arr[i] > arr[r]:
return False
return True
#include <stdio.h>
#include <stdbool.h>
def preorder(arr, index, level):
if index >= len(arr):
return
print(' ' * level + str(arr[index]))
l = 2 * index + 1
r = 2 * index + 2
if l < len(arr):
preorder(arr, l, level + 1)
if r < len(arr):
preorder(arr, r, level + 1)
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;
}
def main():
# Načítanie vstupu
arr = list(map(int, input().split()))
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);
}
if is_min_heap(arr):
print("Je to taka kopa:")
preorder(arr, 0, 0)
else:
print("Nie je kopa.")
if __name__ == "__main__":
main()
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;
}