#include #include #include #include using namespace std; struct node { string data; node * left, * right; node(string s, node * l, node * r) { data = s; left = l; right = r; } }; class tree { protected: node * root; public: tree() { root = NULL; } void build_demo_tree() { node * a = new node("ant", NULL, NULL); node * c = new node("cat", NULL, NULL); node * e = new node("emu", NULL, NULL); node * g = new node("gnu", NULL, NULL); node * i = new node("ink", NULL, NULL); node * k = new node("key", NULL, NULL); node * m = new node("mat", NULL, NULL); node * o = new node("oaf", NULL, NULL); node * b = new node("bat", a, c); node * f = new node("fly", e, g); node * j = new node("jug", i, k); node * n = new node("not", m, o); node * d = new node("dog", b, f); node * l = new node("log", j, n); root = new node("hog", d, l); } string navigate(node * n, string path) { for (int i = 0; i < path.length(); i += 1) { char step = path[i]; if (n == NULL) return "Error: path too long"; if (step == 'L') n = n->left; else n = n->right; } return n->data; } string navigate(string path) { return navigate(root, path); } bool loop_search(string wanted) { node * n = root; while (n != NULL) { if (n->data == wanted) return true; if (wanted < n->data) n = n->left; else n = n->right; } return false; } bool search(node * n, string wanted) { if (n == NULL) return false; if (n->data == wanted) return true; if (wanted < n->data) return search(n->left, wanted); else return search(n->right, wanted); } bool search(string wanted) { return search(root, wanted); } string pathto(node * n, string wanted) { if (n == NULL) return "error"; if (n->data == wanted) return ""; if (wanted < n->data) return string("L") + pathto(n->left, wanted); else return string("R") + pathto(n->right, wanted); } string pathto(string wanted) { return pathto(root, wanted); } void printall(node * n) { if (n == NULL) return; printall(n->left); cout << n->data << "\n"; printall(n->right); } void printall() { printall(root); } void printnicely(node * n, int depth) { if (n == NULL) return; printnicely(n->left, depth+1); cout << setw(depth*3) << "" << n->data << "\n"; printnicely(n->right, depth+1); } void printnicely() { printnicely(root, 0); } void insert(node * & n, string ins) { if (n == NULL) n = new node(ins, NULL, NULL); else if (ins < n->data) insert(n->left, ins); else insert(n->right, ins); } void insert(string ins) { insert(root, ins); } void insert_loop(string ins) { if (root == NULL) { root = new node(ins, NULL, NULL); return; } node * n = root; while (true) { if (ins < n->data) { if (n->left == NULL) { n->left = new node(ins, NULL, NULL); return; } n = n->left; } else { if (n->right == NULL) { n->right = new node(ins, NULL, NULL); return; } n = n->right; } } } void flatten(node * n, vector & V) { if (n == NULL) return; flatten(n->left, V); V.push_back(n->data); flatten(n->right, V); } void flatten(vector & V) { flatten(root, V); } void entree(vector & V) { while (! V.empty()) { insert(V.back()); V.pop_back(); } } }; int main() { vector vec; while (true) { string s; cin >> s; if (s == "****") break; vec.push_back(s); } tree T; T.entree(vec); T.flatten(vec); for (int i = 0; i