#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) { return new node(s); } bool print(node * p, int wanted_depth, int actual_depth) { if (wanted_depth == actual_depth) { cout << setw(actual_depth * 5) << "" << p->get_data() << "\n"; return p->get_num_children() > 0; } bool any_children = false; int num = p->get_num_children(); int i = 0; while (i < num) { bool any_children_here = print(p->get_child(i), wanted_depth, actual_depth + 1); any_children = any_children || any_children_here; i += 1; } return any_children; } void print(node * p) { for (int wanted = 1; true; wanted += 1) if (! print(p, wanted, 1)) break; } 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")))); print(t); }