#include #include #include "btree.h" using namespace std; void printPreorder(btreeT , node ); bool askYesNo(string); bool askIsItA(string); void addNewCritter(btreeT ,node ); main () { btreeT T; node n; T.replaceElement(T.Root(),"frog"); while(1) { cout << "Think of an animal and I will try to guess it with yes/no questions" << endl; n = T.Root(); while(!T.isExternal(n)) { if(askYesNo(T.element(n)) ) { n = T.rightChild(n); } else { n = T.leftChild(n); } } // now down to an external node. if (askIsItA(T.element(n))) { cout << "Cool, I win" << endl; } else { addNewCritter(T,n); } printPreorder(T, T.Root()); } } void printPreorder(btreeT T, node n){ cout << T.element(n) << endl; if(!T.isExternal(n) ) { printPreorder(T, T.leftChild(n)); printPreorder(T, T.rightChild(n)); } return; } bool askYesNo(string s) { char ans; cout << "Does it have " << s << "? (y/n) " ; cin >> ans; return(ans == 'y'); } bool askIsItA(string s){ char ans; cout << "Is it a " << s << "? (y/n) " ; cin >> ans; return(ans == 'y'); } void addNewCritter(btreeT T, node n){ string critter; string desc; cout << "I guessed wrong, tell me about this animal" << endl; cout << "What is it? "; cin >> critter; cout << "How is a " << critter << " different from a " << T.element(n) << endl; cout << "Fill in the blank: Does it have _____? " ; cin >> desc; T.expandExternal(n); T.replaceElement(T.leftChild(n),T.element(n)); T.replaceElement(T.rightChild(n),critter); T.replaceElement(n,desc); return; }