From a2367c326adbafa5fcfc523ec2d1da7b05e38034 Mon Sep 17 00:00:00 2001 From: Ivan Leichenko Date: Wed, 27 Nov 2024 23:17:35 +0100 Subject: [PATCH] add a4 --- a4/program.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 a4/program.c diff --git a/a4/program.c b/a4/program.c new file mode 100644 index 0000000..0d3eb75 --- /dev/null +++ b/a4/program.c @@ -0,0 +1,61 @@ +#include +#include +#include + +int isMinHeap(int arr[], int len) +{ + for (int i = 0; i <= (len - 2) / 2; i++) + { + int l = 2 * i + 1; + int r = 2 * i + 2; + if ((l < len && arr[i] > arr[l]) || (r < len && arr[i] > arr[r])) + { + return 0; + } + } + return 1; +} + +void printPreorder(int arr[], int len, int index, int level) +{ + if (index >= len) + { + return; + } + + for (int i = 0; i < level; i++) + { + printf(" "); + } + printf("%d\n", arr[index]); + + printPreorder(arr, len, 2 * index + 1, level + 1); + printPreorder(arr, len, 2 * index + 2, level + 1); +} + +int main() +{ + char input[1000]; + fgets(input, sizeof(input), stdin); + + int kopa[100]; + int c = 0; + char *token = strtok(input, " "); + while (token != NULL) + { + kopa[c++] = atoi(token); + token = strtok(NULL, " "); + } + + if (isMinHeap(kopa, c)) + { + printf("Je to taka kopa:\n"); + printPreorder(kopa, c, 0, 0); + } + else + { + printf("Nie je kopa.\n"); + } + + return 0; +}