This piece of code helps you to draw the binary tree beautifully.
DrawFactory provides the drawer, which displays your tree in following formats :
By set Mode to DrawMode.TREE
-
Displaying the binary tree in tree-format. The logic would take into account the length of your value in the tree node.
Besides, you can also modify the distance between 2 stages as well as the minimal indent-distance between 2 tree nodes.
-
Here is the demo
By set Mode to DrawMode.DIRECTORY
-
Displaying the binary tree in directory-format.
You can modyfy the distance between 2 stages as well as the horizontal indent-distance
-
Here is the demo
By set Mode to DrawMode.HIERARCHY
-
Displaying the binary tree in hierarchy-format
You can modify minimal indent-distance between tree nodes.
-
Here is the demo
In addition: If you are tired of constructing the tree node-instances by hand. You can provide your tree as following rules to get the tree
-
Line format: {position} , {node's value} with position :=
- t (root)
- r (right)
- l (left)
You can look into this Example
-
Just read text file (.txt-format)
-
One node information per line