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:
- add nodes directly using left and right pointers.
- write the required function func()
- and call a func(root)
- That func() you must write to some task : Like finding height using recursion / Or finding no of nodes using recursion.
- 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:
