#include #include #include struct node { string data; node * left, * right; node(string s); }; node::node(string s) { data = s; left = right = NULL; } struct tree { node * root; tree(); void insert(string s); void insert(string s, node * & ptr); void print(); void print(node * ptr, int depth); }; tree::tree() { root = NULL; } void tree::insert(string s) { insert(s, root); } void tree::insert(string s, node * & ptr) { if (ptr == NULL) ptr = new node(s); else if (s < ptr->data) insert(s, ptr->left); else insert(s, ptr->right); } void tree::print() { print(root, 0); } void tree::print(node * ptr, int depth) // indentation shows the tree's structure { if (ptr != NULL) { print(ptr->left, depth+1); cout << setw(depth*3) << "" << ptr->data << "\n"; print(ptr->right, depth+1); } } void main() { tree T; while (true) { T.print(); cout << "? "; string s; cin >> s; cout << "\n"; if (cin.fail()) break; T.insert(s); } T.print(); }