////////////////////////////////////// // Class for nodes of the binary tree. class QuestionNode { public: string text; QuestionNode *yes, *no; QuestionNode(string t, QuestionNode *y= NULL, QuestionNode *n= NULL) { text= t; yes= y; no= n; } ///////////////////////////////////////// // Write out serialised form of the tree. static void Save(ofstream &out, QuestionNode *qN, unsigned depth) { for (unsigned i= 0; i < depth; i++) out << " "; if (qN == NULL) { out << "N:" << endl; } else { out << "T: " << qN->text << endl; Save(out, qN->yes, depth+1); Save(out, qN->no, depth+1); } } /////////////////////////////////////// // Read in serialised form of the tree. static QuestionNode *Load(ifstream &in) { string line; getline(in,line); unsigned nB= 0; while (nB < line.length() && line[nB] == ' ') nB++; // Skip leading blanks. if (line.substr(nB,2) == "N:") { return NULL; } else if (line.substr(nB,3) == "T: ") { return new QuestionNode(line.substr(nB+3), Load(in), Load(in)); } else { cout << "Load error." << endl; exit(1); } return NULL; // To supress compile-time warning; it can "never" get here. } QuestionNode *& nextQuestion(); bool guessed(); };