# Tree data-structure visualization :

off topic here:

There is a lot variation of tree QS + c_programming QS in gate using recursion:

I found it helpful to run all those and test the correctness of them and try different variations using minor modifications in the questions.

To do that, we need a tree data structure at first and we need to build it using C language because in gate we are most concerned with C only

Constructing a tree is OK ! and running those programs are also fine! but to check it manually we must need to use pen/paper to trace all node after drawing the given tree. And if further nodes are added or leaves are added then tree structure changes and manual checking becomes a little bit lengthy.

a few weeks back, I wrote a tree program and trying to visualize it while executing the executable in C lang.

I went though few posts in StackOverflow and found a nice way to do in c++.

I downloaded the source and modified it to our requirement so that while writing our questions we need to worry about c++ issues and advanced functions.

at present in the modified code: we can do the following operations:

1. add nodes directly using left and right pointers.
2. write the required function func()
3. and call a func(root)
4. That func() you must write to some task : Like finding height using recursion / Or finding no of nodes using recursion.
5. No need to initialize the tree + No need to de_allocate + No need to worry any other stuff (all are taken care of in the code)

I commented the code and clearly pointed out the portions:

• where  we need to write code to add the nodes (as required in a particular question)
• where we need to write the problem specific function (as required in a particular question)

For example writing height_function():

Where to add nodes and how?

Here giv_new is predefined macro which will give us a random number where the range you can modify as your wish in the program starting few lines #define giv_new rand()%107. just change the $107$ to something you max need.

Before doing these we must need to update GCC to 5.1.

Just install TDM-GCC according to your system (x86 / x64)

• double click install- no need to set path and other modifications
• After installing gcc or g++ directly available to you command prompt in windows.

This the source file : save in a folder say . abc

• navigate to the saved folder abc  using the command prompt.
• g++ -std=c++11 filename.cpp is the command to compile the program.
• run it : a.exe if you want to see the results directly in the present command prompt.
• Or run it: a.exe > outfilename.txt . outfilename.txt will be created in the same folder.
• You can view that text file in a nearby window simultaneously.(better for large tree)

Although the program is available in c++, to practice our question we don't need to worry about c++ stuff just plain c would work. Few exceptions might be there : I hope you can handle those :)

If you are using a better editor like sublime: you can collapse (toggle) unnecessary codes and can focus only on the codes you need.

Like this:

posted Jan 13, 2017
edited Jan 13, 2017
1
Like
0
Love
0
Haha
0
Wow
0
Angry
0

I used already existing c++ function max. But any other utility function like max-min you can write there and access it using :

• bt.max_of_two()
• bt.min_of_two(). etc

Will it do manually calculation easier (which mostly needed in GATE)?

There is no apparent advantage of it. You/We can always perform manual checking and we should do it considering GATE. you are right !.

But I hope this visualization will give greater flexibility of viewing the tree and verifying our manual calculation. While studying in front of PC or while doing DS QS ... keeping one editor+command prompt open will not degrade our manual checking procedure.

Anyway if someone does not like to print the tree and get rid of fancy visualization and want just the minimal function writing:

• Then just comment the bt.Dump() line inside main(). Tree will hide :)
The motive of this post is not to avoid manual checking as @srestha said above.

But manual checking also does not tell us to always avoid constructing a Tree and doing it in real instead of writing codes in a paper.

I guess posting it few months earlier would have been better
yes, @Debasish

Time factor now :)
I am amazed! how brilliantly you explain things @Debashish.
Awesome!!!