This file was created as an educational tool to display basic Binary Trees in a more human readable form. I created this file in the hope that students new to the concept of trees would see their own implemented Binary Trees displayed on the output console, thus simplifying the debugging process a bit.
Note: This code produces a "condensed" display of the tree. Be careful when working out tree's Top and Bottom views using this display!
This header defines a functor 'shoTree' template which takes your tree's node type as a template parameter and the root node as the function parameter.
e.g. First, include this header:
#include "shoTree"
Then, make an object of shoTree template :
shoTree<Tnode> treeDisp;
where, Tnode is the node type used by your Binary Tree
Note: Your tree node MUST use 'data', 'left' and 'right' fields ,otherwise you'll have to do a search and replace in this header, carefully avoiding std::left and std::right
Now, use this object as a funtion to display the tree:
treeDisp(root);
This call must be made by a tree class member, since most likely, the root node will be a private member Not to mention, 'root' must be of type (Tnode *)
Using the support of multi-byte characters, box drawing characters can be used for _shoTreeDash, _shoTreeLchild and _shoTreeRchild to get a prettier display, add this compiler option with a 64bit version of g++:
-fexec-charset=cp437
Get 64bit gcc for Windows here: https://sourceforge.net/projects/mingw-w64/
Lack of multi-byte characters leaves only '-', '/', '\\' fit for the job... Comment out the box drawing #defines and uncomment the ones using dashes and slashes. No extra compiler options needed.
int , char , string types are supported by default. If your tree stores data types other than int, char and string, then you would need to define two functions for the data type in global scope or class shoTree:
int width(DType data);
{
Returns the length of data in no. of characters as
printed on console
}
std::ostream& operator<<(std::ostream& cout,DType data)
{
Prints the data on console without any newlines
}