This commit is contained in:
Ivan Leichenko 2024-11-27 23:17:35 +01:00
parent 40d875d18f
commit a2367c326a

61
a4/program.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}