#include #include #include #include #include #include #include #include using namespace std; template class tree { protected: struct node { T data; node * left, * right; node(T d, node * l = NULL, node * r = NULL) { data = d; left = l; right = r; } }; void add(node * np, T x) { assert(np != NULL); while (true) { if (x < np->data) if (np->left == NULL) { np->left = new node(x); break; } else np = np->left; else if (np->right == NULL) { np->right = new node(x); break; } else np = np->right; } } bool find(node * np, T x) { while (true) { if (np == NULL) return false; if (np->data == x) return true; if (x < np->data) np = np->left; else np = np->right; } } /* I have removed the definitions of void print(node * np, vector & v) and void nsprint(node * np) because otherwise people would be lazy and just look at them. You need to be sure you can reconstruct them without help. */ node * root; public: tree() { root = NULL; } void add(T x) { if (root == NULL) root = new node(x); else add(root, x); } bool find(T x) { return find(root, x); } void print(vector & v) { print(root, v); cout << "\n"; } void msprint() { nsprint(root); cout << "\n"; } }; int main() { string s; tree t; vector data; while (true) { cin >> s; if (cin.fail()) break; data.push_back(s); } for (string x: data) t.add(x); data.clear(); t.print(data); for (string x: data) cout << x << " "; cout << "\n"; }