How to print a tree structure? With Recursion - SO / How to print binary tree diagram? template<class Node> void printTree(const Node& tree, const string& indent = "", bool last = true) { cout << indent << "+- " << tree.to_s() << endl; auto new_indent = indent + ( last ? " " : "| "); for (int i = 0; i < tree.child_size(); ++i) { bool last = i == (tree.child_size() - 1); printTree( tree.childs()[i], new_indent, last); } } // call with printTree( tree); Output look likes: +- root +- branch-A | +- sibling-X | | +- grandchild-A | | +- grandchild-B | +- sibling-Y | | +- grandchild-C | | +- grandchild-D | +- sibling-Z | +- grandchild-E | +- grandchild-F +- branch-B +- sibling-J +- sibling-K Written on May 5, 2019, Last update on July 3, 2021 algorithm graph tree c++