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* 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(){
char buffer[20];
@ -26,15 +18,12 @@ struct tree* read_tree(){
buffer[x-1]='\0';
assert(r);
struct tree* node = calloc(1,sizeof(struct tree));
if(buffer[0] == '\0'){
return node;
}
memcpy(node->value, buffer, 20);
/*if(node == NULL){
return create_node();
}*/
if(buffer[0] != '*'){
if(node->left == NULL){
node->left = read_tree();
@ -43,17 +32,9 @@ struct tree* 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){
return node;
//}
// }
}
void print_tree(struct tree* node, int offset){
@ -68,18 +49,31 @@ void print_tree(struct tree* node, int offset){
}
}
struct tree* search(struct tree* this, char answer){
if(this != NULL){
if(answer == 'a'){
printf("%s\n", this->value);
this->left = search(this, answer);
struct tree* search(struct tree* this){
char buffer[5];
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);
this->right = search(this, answer);
this->left = search(this);
}
else if(buffer[0] == 'n'){
printf("%s\n", this->value);
this->right = search(this);
}
}
else{
printf("Koniec\n");
exit(0);
}
@ -93,28 +87,47 @@ void destroy_tree(struct tree* root){
free(root);
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){
return 0;
}
if(node->left == NULL && node->right == NULL){
return 1;
int count = 1;
if(node->left != NULL){
count += count_all(node->left);
}
else{
return count_l(node->left) + count_l(node->right);
if(node->right != NULL){
count += count_all(node->right);
}
return count;
}
int main(){
struct tree* tree = NULL;
tree = read_tree();
int count = count_l(tree);
int count = count_leaves(tree);
printf("Expert z bufetu to vie\n");
printf("Pozna %d druhov ovocia a zeleniny\n", count);
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);
//display(tree);