### Can anyone explain why my loop to traverse a binary tree is working but not how my professor wants it to work?

Hey guys I'm pretty stumped on part of my code right now. I've done some testing and it looks like my code is working how it's supposed to work. It's essentially just a simple guessing game that learns from it's mistake. I'm stumped on this one part though. On line 50 `if(!p->yes) ` my professor wanted us to use `if(p->yes) //if p->yes is 0 ` but when I change my `if(!p->yes) ` to how my professor wants it my program doesn't run properly. Later on in my code my professor wants us to use in line 109 `else if(!p->yes) //else if p->yes is not 0 ` but what I've got is `else if(p->yes)` which is working fine as is and when I change it to how my professor wants it my program doesn't run properly. We were basically given a list of directions to follow and I've followed them except for switching up line 50 and 109 because I noticed that when I followed the directions my program wouldn't work. Can anyone here see what is going on and how can I make my program work the way my professor wants it to?

Thanks!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164`` ``````#include using std::cin; using std::cout; using std::endl; #include using std::fstream; using std::ios; class animal { public: char text[128]; //a yes/no question OR an animal name animal* yes; //pointer to next node if question is answered YES animal* no; //pointer to next node if question is answered NO }; //if "yes" and "no" are 0 then "text" is an animal name // Prototypes void deallocateTree(animal*); int main() { //initialize a "top" pointer named "animal* top animal* top; top = new animal; //set "top" to a newly created node strcpy(top->text, "Elephant"); //set its text to "elephant" //pointers to 0 top->yes = 0; top->no = 0; //start a "while" loop that runs each cycle of the program while(true) { char play; cout << endl; cout << "Would you like to play the guessing game? [Y/N]: "; //invide the user to think of an animal which it will try to guess cin >> play; cin.ignore(1000, 10); if (play == 'N' || play == 'n') break; //await the user's response, and break out of the loop if he declines else if (play == 'Y' || play == 'y') { animal* p = top; //declare a pointer "p" to traverse the tree, and initialize it to "top" while(p) //start a loop to traverse the binary tree { if(!p->yes) { char guess; //ask use if guessed animal is correct. Y/N? cout << "Is it a " << p->text << "? [Y/N]: "; //print p->text as the guessed animal cin >> guess; cin.ignore(1000, 10); if (guess == 'Y' || guess == 'y') //if guess Y brag and break { cout << "I win!" << endl; break; } else if (guess == 'N' || guess == 'n') { char yesNo; //yes or no question char a[128]; //store answer in char a[128] (animal thinking of) char q[128]; //question in char q[128] (question differentiates) cout << "No? Which animal were you thinking of?: "; //ask user what animal he was thinking of ANSWER cin.getline(a, 128); cout << "What question should I ask to tell the difference?: "; //ask what yes/no question differentiates "p->text" from "a" cin.getline(q, 128); cout << "Which answer is correct for that animal? [Y/N]: "; //ask which response is correct for "a" -- yes or no cin >> yesNo; cin.ignore(1000, 10); //create two new nodes, names "y" and "n" animal* y = new animal; animal* n = new animal; //if the correct response for "a" is yes [a[128] (animal thinking of)] if (yesNo == 'Y' || yesNo == 'y') { strcpy(y->text, a); //copy "a" into y->text strcpy(n->text, p->text); //copy p->text into n->text } //else if the correct response is no else if(yesNo == 'N'|| yesNo == 'n') { strcpy(n->text, a); //copy "a" into n-text strcpy(y->text, p->text); //copy p->text into y->text } strcpy(p->text, q); //copy "q" into p->text //set y->yes, n->yes, y->no, n->no to 0 y->yes = 0; n->yes = 0; y->no = 0; n->no = 0; p->yes = y; //set p->yes to y p->no = n; //set p->no to n break; //break from loop } } else if(p->yes) { char yesNo; cout << p->text << "? [Y/N]: "; //print p->text as a question ask for a yes/no reply cin >> yesNo; //store in char yesNo cin.ignore(1000, 10); if(yesNo == 'Y' || yesNo == 'y') { p = p->yes; //if yes set p to p->yes } else if (yesNo =='N'|| yesNo=='n') { p = p->no; //else if set p to p->no } //reclaim memory else if (yesNo == 'D' || yesNo == 'd') { deallocateTree(p->yes); deallocateTree(p->no); cout << "Provide an answer to replace the question: "; cin.getline((char*)p->text, 128); p->yes = 0; p->no = 0; } else if (yesNo == 'E' || yesNo == 'e') { cout << "Enter the new question: "; cin.getline((char*)p->text, 128); } else { cout << "Incorrect entry! Try again." << endl; continue; } } } } } } void deallocateTree(animal* x) { if(x) { deallocateTree(x->yes); deallocateTree(x->no); delete x; } } // end deallocateTree ``````
This code is quite confusing for me... I don't understand why it is structured in this way and what you tried to do.

what is the original assignment?
Topic archived. No new replies allowed.