I am a \"bit\" lost trying to print a binary tree like below in c++:
8
/ \\
/ \\
/ \\
5 10
/
I wrote arbitrary tree pretty printer as a part of C++ algorithms self-education.
The approach is following.
Next, printable nodes position are calculated.
For the last step lines iterated once again to be written to the provided output stream, filling with spaces offsets according to the calculated nodes positions.
Unix box-drawing symbols are used to draw lines. Not sure if they will be printed correctly in Windows cmd, maybe they should be replaced by their DOS counterparts for Windows.
1
┌────────────┬────────┴────────────────────┐
11 12 13
┌────┼────┐ ┌──┴─┐ ┌─────────┴────┬────┐
111 112 113 121 122 131 132 133
┌─────┼─────┐ ┌─────┼─────┐ ┌──┴──┐
1221 1222 1223 1311 1312 1313 1321 1322
Unit tests with usage samples