#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); }; 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); } void tree::print(node * ptr) { if (ptr == NULL) cout << "."; else { cout << "("; print(ptr->left); cout << ptr->data; print(ptr->right); cout << ")"; } } void main() { tree T; while (true) { T.print(); cout << "? "; cout << "\n\n"; string s; cin >> s; if (cin.fail()) break; T.insert(s); } T.print(); }