1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#ifndef BINTREE_H
#define BINTREE_H
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
class TaxonomyTree
{
public:
struct TreeNode {
string info; // The question at the node
TreeNode *yes; // Pointer to left child node
TreeNode *no; // Pointer to right child node
};
TreeNode* taxonomy_root_ptr;
TaxonomyTree() {
taxonomy_root_ptr = nullptr;
}
bool isLeaf(TreeNode* node) {
if ((node->yes == nullptr || node->yes->info == "@ ") && (node->no == nullptr|| node->no->info == "@ ")) {
return true;
}
else {
return false;
}
}
void swap(TreeNode* &x, TreeNode* &y) {
TreeNode* temp = x;
x = y;
y = temp;
}
TreeNode* addNode(string info) {
TreeNode* newNode = new TreeNode;
newNode->info = info;
newNode->yes = nullptr;
newNode->no = nullptr;
add(taxonomy_root_ptr, newNode);
return newNode;
};
void add(TreeNode* &node, TreeNode* &newNode) {
if (node == nullptr) {
node = newNode;
}
}
void traverseTree() {
traverseTree(taxonomy_root_ptr);
}
void traverseTree(TreeNode* &node) { //////////////////////////
TreeNode* locPtr = node;
string response;
string newInfo1;
string newInfo2;
TreeNode* newEntry1;
TreeNode* newEntry2;
while(!(isLeaf(locPtr))) {
cout << locPtr->info << endl;
cin >> response;
cout << endl;
if (response == "y") {
locPtr = locPtr->yes;
}
else if (response == "n") {
locPtr = locPtr->no;
}
else {
cout << "\nPlease enter one of the offered responses.\n";
}
}
if (isLeaf(locPtr) == true) {
cout << "My guess is " << locPtr->info << ". Am I right? Please answer [y or n]\n";
cin >> response;
cin.ignore();
cout << endl;
while (response != "y" && response != "n") {
cout << "\nPlease enter one of the offered responses.\n";
cout << "My guess is " << locPtr->info << ". Am I right? Please answer [y or n]\n";
cin >> response;
}
if (response == "y") {
cout << "\nI knew it! What a great animal! One of my favorites!\n\n";
}
else {
cout << "I give up. What are you?\n";
getline(cin, newInfo1);
cout << "\nPlease type a yes/no question that will distinguish a " << locPtr->info << " from a " << newInfo1;
cout << ".\nYour question: (ending with ?)\n";
getline(cin, newInfo2);
cout << "\nAs a " << newInfo1 << ", " << newInfo2 << " Please answer [y or n]\n";
cin >> response;
while (response != "y" && response != "n") {
cout << "\nPlease enter one of the offered responses.\n";
cout << "As a " << newInfo1 << ", " << newInfo2 << " Please answer [y or n]\n";
cin >> response;
}
newInfo2 += " Please answer [y or n]";
swap(newEntry2, locPtr);
if (response == "y") {
//locPtr = locPtr->yes;
newEntry2->yes = newEntry1;
newEntry2->no = locPtr;
}
else {
//locPtr = locPtr->no;
newEntry2->yes = locPtr;
newEntry2->no = newEntry1;
}
newEntry1->info = newInfo1;//////////////////// RUNTIME ERROR/PROGRAM CRASHING ///////////////////////////////////////////////
newEntry2->info = newInfo2;//////////////////// RUNTIME ERROR/PROGRAM CRASHING ///////////////////////////////////////////////
}
}
};
void readIn(TreeNode* &node, ifstream &infile) {
string entry;
if (!(getline(infile, entry)) || entry == "@ ") {
return;
}
node = addNode(entry);
readIn(node->yes, infile);
readIn(node->no, infile);
};
void saveTo(TreeNode* node, ofstream &outfile) {
//symbol for distinguishing leaf
//complete tree with un-populated leaves
//if leaf node, node IS an answer
if (node == nullptr) {
outfile << "@ " << endl;
}
else {
outfile << node->info << endl;
saveTo(node->yes, outfile);
saveTo(node->no, outfile);
}
};
void deleteNode() {
};
void printIt() {
printTree(taxonomy_root_ptr, 1);
}
void printTree(TreeNode* node, int treeLevel) {
if (node != nullptr) {
printTree(node->no, treeLevel + 1);
cout << endl;
if (node == taxonomy_root_ptr) {
cout << "ROOT->";
}
for (int i = 0; i < treeLevel && node != taxonomy_root_ptr; i++) {
cout << " ";
}
cout << (node->info);
printTree(node->yes, treeLevel + 1);
}
};
};
#endif
| |