72 lines
1.5 KiB
C
72 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE 20
|
|
|
|
int checkMinHeap(const long* arr, const int size){
|
|
for (int i = 0; i<=(size-2)/2; i++) {
|
|
if(arr[2*i] < arr[i]) return -1;
|
|
if(arr[2*i+1] < arr[i]) return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
long int* strToInt(const char* chArr){
|
|
int j = strlen(chArr);
|
|
//int i = 0;
|
|
//printf("dlength %d\n",j/2-1);
|
|
long int* dArr = (long int*)calloc(j,sizeof(long));
|
|
char* pEND;
|
|
for(int i=0; i<j; i++){
|
|
dArr[i] = strtol(chArr, &pEND, 10);
|
|
//printf("%ld \n", dArr[i]);
|
|
if(dArr[i] == 0) break;
|
|
chArr = pEND;
|
|
}
|
|
/* for(int i = 0; i<j/2-1; i++){
|
|
printf("%ld ", dArr[i]);
|
|
}*/
|
|
return dArr;
|
|
}
|
|
|
|
void print_tree(const long* arr){
|
|
int offset = 0;
|
|
int j =0;
|
|
while(j != sizeof(&arr)/sizeof(arr[0])){
|
|
for (int i = 0; i < offset; i++){
|
|
printf(" ");
|
|
}
|
|
printf("%ld\n",arr[j]);
|
|
offset+=1;
|
|
for (int i = 0; i < offset; i++){
|
|
printf(" ");
|
|
}
|
|
printf("%ld\n",arr[j*2+1]);
|
|
for (int i = 0; i < offset; i++){
|
|
printf(" ");
|
|
}
|
|
printf("%ld\n",arr[j*2+2]);
|
|
j++;
|
|
offset+=1;
|
|
}
|
|
}
|
|
|
|
int main(){
|
|
char chArr[SIZE];
|
|
while(1){
|
|
char* r = fgets(chArr, SIZE, stdin);
|
|
if(r == NULL){
|
|
break;
|
|
}
|
|
}
|
|
long int* dArr = strToInt(chArr);
|
|
|
|
int heap = checkMinHeap(dArr, sizeof(&dArr)/sizeof(long));
|
|
if(heap == 1){
|
|
printf("Je to taka kopa:\n");
|
|
print_tree(dArr);
|
|
}
|
|
free(dArr);
|
|
return 0;
|
|
} |