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): #include <stdio.h>
n = len(arr) #include <stdbool.h>
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
def preorder(arr, index, level): bool is_min_heap(int arr[], int n) {
if index >= len(arr): for (int i = 0; i < n; i++) {
return int l = 2 * i + 1;
print(' ' * level + str(arr[index])) int r = 2 * i + 2;
l = 2 * index + 1 if ((l < n && arr[i] > arr[l]) || (r < n && arr[i] > arr[r])) {
r = 2 * index + 2 return false;
if l < len(arr): }
preorder(arr, l, level + 1) }
if r < len(arr): return true;
preorder(arr, r, level + 1) }
def main(): void preorder(int arr[], int n, int index, int level) {
# Načítanie vstupu if (index >= n) return;
arr = list(map(int, input().split())) for (int i = 0; i < level; i++) {
printf(" ");
if is_min_heap(arr): }
print("Je to taka kopa:") printf("%d\n", arr[index]);
preorder(arr, 0, 0) preorder(arr, n, 2 * index + 1, level + 1);
else: preorder(arr, n, 2 * index + 2, level + 1);
print("Nie je kopa.") }
int main() {
if __name__ == "__main__": int arr[100];
main() 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;
}