Generating Pictorial Representation Of Binary tree

It took so long to get back to system and start blogging again.. ..anyways lets stick to the topic.

Every time i code a problem in binary trees and do some deletion and insertions and then try to figure out from the raw textual output, is my procedure did it fine.That takes some amount of time isn't it .

So why not an output be generated in a way that somehow looks like a binary tree ...
something like this: 


To start from somewhere all it takes is a simple idea of  "how can i print an output in a proper way that is first of all easier to understand and can be decoded into a tree image or something".

Step 1:
Look'd back to my data-structure classes n found that, there is a way called Digraphs which clearly defines the order and dependency of any node in the tree.And Google-ing it a bit to find out there is a language known as the DOT Language for Di-Graph representation of a tree.

Step 2:
So now we have to decode this digraph info n convert it into a image (gif/jpeg).
There is a Open source software named Graphviz (that u can find out as a related term while google-ing) which can be installed on ubuntu as "sudo apt-get install Graphviz" (other installation details are given on the website).

Note: Graphviz is a set of tools including dot,neato,circo etc. although we will be needing only dot for now.

Step 3:
Gluing together the pieces ...
the source code for binary tree:


The shell script to automate stuff:


An example tree.dot file:


Following functions will take care of printing output to a file in di-graph format
1. void digraphOutput(Node root) 
2. void printOutput(Node root,FILE *fp)


other tree functions that are implemented in the source-code:

01. count_nodes
02. count_leaves
03. deleteNode
04. maxSumOfTreeFromANode
05. heightOftree
06. minimum
07. binaryInsertion
08. binarySearch_iterative
09. binarySearch_recursive
10. maximum
11. traverse_inorder
12. traverse_preorder
13. traverse_postorder
14. findParent (incase u r not storing the parent in the treenode structure)




So just run the script and see the output as:

Comments

  1. next post to come by tomorrow

    Building a webview application on Android and using example of theHindu.

    ReplyDelete

Post a Comment