third commit
This commit is contained in:
parent
07e5f2eb4a
commit
4e584924fc
@ -4,6 +4,7 @@
|
||||
#include <assert.h>
|
||||
#define SIZE 100
|
||||
|
||||
|
||||
struct node {
|
||||
// Otázka aleo odpoveď
|
||||
char data[SIZE];
|
||||
@ -27,7 +28,9 @@ struct node* insert(struct node* root, char data[SIZE]){
|
||||
root = getNewNode(data);
|
||||
return root;
|
||||
}
|
||||
|
||||
if(strncmp(data, "*", 1)){
|
||||
return root;
|
||||
}
|
||||
if(root->left == NULL){
|
||||
root->left = insert(root->left,data);
|
||||
}else{
|
||||
@ -41,7 +44,6 @@ void remove_tree(struct node* node){
|
||||
remove_tree(node->left);
|
||||
remove_tree(node->right);
|
||||
free(node);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,40 +57,57 @@ void printt_tree(struct node* tree,int offset){
|
||||
printt_tree(tree->right,offset+3);
|
||||
}
|
||||
}
|
||||
void printTabs(int numtabs){
|
||||
for(int i=0; i<numtabs; i++){
|
||||
printf("\t");
|
||||
}
|
||||
}
|
||||
void printTree(struct node* root, int level){
|
||||
if(root == NULL) {
|
||||
printTabs(level);
|
||||
printf("empty\n");
|
||||
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;
|
||||
|
||||
char buff[SIZE];
|
||||
char* r;
|
||||
int counter=0;
|
||||
int lists=0;
|
||||
while(1){
|
||||
r = fgets(buff, SIZE, stdin);
|
||||
if(r == NULL){
|
||||
if(buff[0] == '\n' || r == NULL){
|
||||
break;
|
||||
}
|
||||
if(strncmp(buff, "*", 1)) lists++;
|
||||
|
||||
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.");
|
||||
for(int i=0; i< counter; i++){
|
||||
printf("%s", tree->data);
|
||||
r = fgets(buff, SIZE, stdin);
|
||||
if(strncmp(buff, "a", 1) && tree->right != NULL){
|
||||
tree = tree->right;
|
||||
continue;
|
||||
}else if(strncmp(buff, "n", 1) && tree->left != NULL){
|
||||
tree = tree->left;
|
||||
continue;
|
||||
}else {
|
||||
puts("Koniec");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("counter %d, lists %d\n", counter, lists);
|
||||
|
||||
|
||||
//printt_tree(tree, 1);
|
||||
printtree(tree);
|
||||
remove_tree(tree);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user