diff --git a/cv8/program.c b/cv8/program.c index dfff5fe..14ae515 100644 --- a/cv8/program.c +++ b/cv8/program.c @@ -8,32 +8,39 @@ struct node { // Otázka aleo odpoveď char data[SIZE]; + struct node* parent; // Odpoveď áno struct node* left; // Odpoveď nie struct node* right; }; -struct node* getNewNode(char data[SIZE]){ - struct node* newNode = (struct node*)calloc(1, sizeof(struct node)); - memcpy(newNode->data, data, SIZE); - newNode->left = NULL; - newNode->right = NULL; - return newNode; +struct node* read_tree(int* count, int* leafs){ + char buffer[SIZE]; + memset(buffer,0,SIZE); + char* r = fgets(buffer,SIZE,stdin); + assert(r); + struct node* node = calloc(1,sizeof(struct node)); + memcpy(node->data,buffer,SIZE); + if(r[0]!='*'){ + *count = *count +1; + node->left=read_tree(count, leafs); + node->right=read_tree(count, leafs); + }else { + *leafs = *leafs+1; + } + return node; } - -struct node* insert(struct node* root, char data[SIZE]){ - if(root == NULL){ - root = getNewNode(data); - return root; +void print_tree(struct node* tree,int offset){ + for (int i = 0; i < offset; i++){ + printf(" "); } - if(root->left == NULL){ - root->left = insert(root->left,data); - }else{ - root->right = insert(root->right,data); + printf("%s",tree->data); + if (tree->left){ + print_tree(tree->left,offset +3); + print_tree(tree->right,offset +3); } - return root; } void remove_tree(struct node* node){ @@ -41,70 +48,34 @@ void remove_tree(struct node* node){ remove_tree(node->left); remove_tree(node->right); free(node); - } + } } -void printt_tree(struct node* tree,int offset){ - for (int i = 0; i < offset; i++){ - printf(" "); +void ask(struct node* tree){ + printf("%s", tree->data); + if(tree->data[0] == '*'){ + puts("Koniec"); } - printf("%s",tree->data); - if (tree->left){ - printt_tree(tree->left,offset+3); - printt_tree(tree->right,offset+3); - } -} -void printTabs(int numtabs){ - for(int i=0; ileft != NULL){ + tree = tree->left; + ask(tree); + }else if(tree->right != NULL) { + tree = tree->right; + ask(tree); + }else{ return; } - printTabs(level); - printf("hodnota %s\n", root->data); - printTabs(level); - printf("vlavo \n"); - printTree(root->left, level+1); - printTabs(level); - printf("vpravo \n"); - printTree(root->right, level+1); - printTabs(level); - printf("hotovo \n"); } -void printtree(struct node* root){ - printTree(root, 0); -} int main(){ - struct node* tree = NULL; + struct node* tree; + int count = 0; + int leafs = 0; + tree = read_tree(&count, &leafs); + //print_tree(tree, 20); + ask(tree); + remove_tree(tree); + - char buff[SIZE]; - char* r; - int counter=0; - int lists=0; - while(1){ - r = fgets(buff, SIZE, stdin); - if(buff[0] == '\n' || r == NULL){ - break; - } - - tree = insert(tree, buff); - counter++; - } - puts("Expert z bufetu to vie."); - printf("Pozna %d druhov ovocia a zeleniny.\n", lists); - puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost."); - - printf("counter %d, lists %d\n", counter, lists); - - - //printt_tree(tree, 1); - printtree(tree); - remove_tree(tree); return 0; }