#include #include #include #include #include using namespace std; class node { protected: string data; vector child; public: node(string d) { data = d; } node * add(node * p) { child.push_back(p); return this; } string get_data() { return data; } int get_num_children() { return child.size(); } node * get_child(int i) { return child[i]; } }; node * mn(string s) // mn calls constructor, but is shorter to type { return new node(s); } void print(node * p, int depth = 0) { cout << setw(depth * 5) << "" << p->get_data() << "\n"; int num = p->get_num_children(); int i = 0; while (i < num) { print(p->get_child(i), depth + 1); i += 1; } } void bf_print(node * here) { deque queue; queue.push_back(here); while (! queue.empty()) { here = queue.front(); queue.pop_front(); cout << here->get_data() << "\n"; int num = here->get_num_children(); for (int i = 0; i < num; i += 1) queue.push_back(here->get_child(i)); } } int main() { node * t = mn("ant") ->add(mn("bat") ->add(mn("egg") ->add(mn("kit")) ->add(mn("log"))) ->add(mn("fig")) ->add(mn("git") ->add(mn("map")) ->add(mn("nut")) ->add(mn("old")) ->add(mn("pig")))) ->add(mn("cat") ->add(mn("hat"))) ->add(mn("dog") ->add(mn("ink") ->add(mn("qat")) ->add(mn("rat"))) ->add(mn("jug") ->add(mn("sud")) ->add(mn("tip")))); bf_print(t); }