This commit is contained in:
Maryna Kravtsova 2020-11-21 18:34:29 +01:00
parent 739a79589a
commit 01052f7dfe

View File

@ -9,14 +9,6 @@ struct tree {
struct tree* right; struct tree* right;
}; };
/*struct tree* create_node(char* string){
struct tree* node = calloc(1, sizeof(struct tree));
node->left = NULL;
node->right = NULL;
strcpy(node->value, string);
return node;
}*/
struct tree* read_tree(){ struct tree* read_tree(){
char buffer[20]; char buffer[20];
@ -26,15 +18,12 @@ struct tree* read_tree(){
buffer[x-1]='\0'; buffer[x-1]='\0';
assert(r); assert(r);
struct tree* node = calloc(1,sizeof(struct tree)); struct tree* node = calloc(1,sizeof(struct tree));
if(buffer[0] == '\0'){ if(buffer[0] == '\0'){
return node; return node;
} }
memcpy(node->value, buffer, 20); memcpy(node->value, buffer, 20);
/*if(node == NULL){
return create_node();
}*/
if(buffer[0] != '*'){ if(buffer[0] != '*'){
if(node->left == NULL){ if(node->left == NULL){
node->left = read_tree(); node->left = read_tree();
@ -43,14 +32,6 @@ struct tree* read_tree(){
node->right = read_tree(); node->right = read_tree();
} }
} }
/*else if(string[0] == '*'){
if(node->left == NULL){
strcpy(node->value, string);
}
else if(node->right == NULL){
strcpy(node->value, string);
}
}*/
//if(node->left != NULL && node->right != NULL){ //if(node->left != NULL && node->right != NULL){
return node; return node;
// } // }
@ -68,18 +49,31 @@ void print_tree(struct tree* node, int offset){
} }
} }
struct tree* search(struct tree* this, char answer){ struct tree* search(struct tree* this){
if(this != NULL){
if(answer == 'a'){
printf("%s\n", this->value); char buffer[5];
this->left = search(this, answer); memset(buffer,0,5);
char* r = fgets(buffer,5,stdin);
int x = strlen(buffer);
buffer[x-1]='\0';
if(buffer[0] != 'a' && buffer[0] != 'n'){
printf("Chyba\n");
exit(0);
} }
else if(answer == 'n'){ if(this != NULL){
if(buffer[0] == 'a'){
printf("%s\n", this->value); printf("%s\n", this->value);
this->right = search(this, answer); this->left = search(this);
}
else if(buffer[0] == 'n'){
printf("%s\n", this->value);
this->right = search(this);
} }
} }
else{ else{
printf("Koniec\n");
exit(0); exit(0);
} }
@ -93,28 +87,47 @@ void destroy_tree(struct tree* root){
free(root); free(root);
root = NULL; root = NULL;
} }
int count_l(struct tree* node){ int count_leaves(struct tree* node){
int count = 0;
if(node->left == NULL && node->right == NULL){
count = 1;
}
else{
if(node->left != NULL){
count += count_leaves(node->left);
}
if(node->right != NULL){
count += count_leaves(node->right);
}
}
return count;
}
int count_all(struct tree* node){
if(node == NULL){ if(node == NULL){
return 0; return 0;
} }
if(node->left == NULL && node->right == NULL){ int count = 1;
return 1; if(node->left != NULL){
count += count_all(node->left);
} }
else{ if(node->right != NULL){
return count_l(node->left) + count_l(node->right); count += count_all(node->right);
} }
return count;
} }
int main(){ int main(){
struct tree* tree = NULL; struct tree* tree = NULL;
tree = read_tree(); tree = read_tree();
int count = count_l(tree); int count = count_leaves(tree);
printf("Expert z bufetu to vie\n"); printf("Expert z bufetu to vie\n");
printf("Pozna %d druhov ovocia a zeleniny\n", count); printf("Pozna %d druhov ovocia a zeleniny\n", count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
printf("%s\n", tree->value);
tree = search(tree+1);
//print_tree(tree, 3); //print_tree(tree, 3);
//display(tree); //display(tree);