#include #include struct StringTree { string data; StringTree * left, * right; StringTree(string s): data(s), left(NULL), right(NULL) { } }; StringTree * insert(StringTree * t, string s) { if (t==NULL) t = new StringTree(s); else if (s <= t->data) t->left = insert(t->left, s); else t->right = insert(t->right, s); return t; } bool find(StringTree * t, string s) { string ans; if (t==NULL) return 0; else if (s == t->data) return 1; else if (s < t->data) return find(t->left, s); else return find(t->right, s); } void print(StringTree * t) { if (t==NULL) return; print(t->left); cout << t->data << endl; print(t->right); } void main(void) { cout << "enter:\n"; cout << " i word to insert word\n"; cout << " f word to find word\n"; cout << " p to print all\n"; cout << " x to exit\n"; StringTree * tree = NULL; while (1) { cout << "> "; string command; cin >> command; if (command=="i") { string operand; cin >> operand; tree = insert(tree, operand); cout << operand << " inserted\n"; } else if (command=="f") { string operand; cin >> operand; int found = find(tree, operand); cout << operand << ( found ? " present" : " not found" ) << "\n"; } else if (command=="p") { print(tree); } else if (command=="x") { break; } else { cout << "Get it right.\n"; } } }