Tree: Through the Eyes of Recursion
By: Ddexterr Lab
Tags: Recursion, Data Structures, Recursion Tree, Tree, Binary Tree

Tree: Through the Eyes of Recursion

Let’s go easy

We all know tree is part of nature, something that has a root, a lot of leaves, a trunk and branches. We are also quite comfortable seeing it around us. But the moment this enters technical world we find it difficult to cope up with it. Everything makes sense till the point we are dealing with linear data structures to solve our problems as they can be easily understood and visualised. But the moment we try to deal with non linear data structure, its implementation, things become tricky and difficult to understand. Most of the time we tend to forget its essence and feel the need to turn pages backward. Also we cannot ignore this concept as it has got myriad of applications in technical world Disk directories, memory arrangement, topologies etc are some of the wonders of non- linear data structures like tree, which is going to be the main focus of this article.

Since we ourselves were facing a lot of troubles in getting through it, we decided to prepare an analogy and compare it with real world examples and images. This article shows, how we can alter the way of looking at the basics of tree for a beginner.

Let us begin with a real life example to visualise things better.

Consider tree as a person like you, with traits like relations, friends, family etc. Now suppose, I being an outsider wants to contact one of the members of your circle, but don’t know him personally, but I do know that the person is indirectly reachable in some way. So, How can this be done in an efficient manner? Lets innovate a bit here. I can contact my friends (as I already have their contact numbers), they can contact their friends, and the chain may go on like this till one of them is able to reach the member that I actually want to contact. Now in order to get that member’s contact number, a reverse process must happen and member’s contact number is passed back in the same order as I reached it i.e in a hierarchical manner. So, following this strategy I can easily get the contact number. The whole process can be visualised as an algorithm working on a tree structure.

After looking at the example one thing is very interesting to look at, a process is calling itself again and again repeatedly till a certain point, what is

it ..?

Yes you are right, it is nothing else but recursion!

Recursion

Recursion is the strategy to solve a problem by breaking it down into smaller sub problems and applying the same strategy to these smaller sub problems. Once we reach a base condition or the smallest sub-problem for which we already have a solution, we can use this solution to construct the solution of the original problem i.e the one which was broken into smaller sub-problems. By using the solutions of smaller sub-problems in a reverse hierarchical manner we can find the solution of the original problem.

Figure T.2

In the example T.1 we saw how we laid down the situation into a tree. Also, we saw how recursion plays a crucial part in it. It clearly shows that a better grasp of recursion can make your life a lot easier for understanding trees thoroughly. Let’s get some more insight on this concept.

Connecting trees with recursion

Recursion works like a tree.

While solving a problem using recursion, if we analyse the stack function call, it takes shape of a tree. Now, It can be a linear tree (linked list) or a branched tree.

Example when have to print numbers from 1 to 10, recursion will take shape of linear tree, whereas if we try to find nth fibonacci number it will take shape of a branched tree.

2. Both follow the bottom up approach to get the results.

Applying recursion to a problem builds a tree from top to bottom where each function call in stack acts as a node of a tree. To obtain the result we follow reverse or bottom up approach. The image below clearly depicts this!

Taking example T.1, consider F as a friend. On moving from top to bottom, we are contacting our friends to get the contact number. Once one of them gets the contact number he returns it in bottom up manner.

Bonus tip:

This is how debugger works for tree or recursion.

3. Both grow horizontally and vertically.

From Figure T.1 or T.3 we can observe that how tree or recursion is growing horizontally and vertically.

4. In recursion, each function call in stack gives rise to a new tree. Leaves of the tree are the base conditions. For simplicity refer Figure T.1 or T.3.

Conclusion

Through this article we tried to give an abstract view of basic tree data structure with a different flavour in order to make it easily understandable in a much practical way. We compared it with real life examples as a newbie can relate easily to objects which are a part of his/her daily life more easily. We also explained the connection of recursion with tree and its importance in understanding the concepts deeply. In the upcoming articles , we would be diving deeper into various operations that can be performed on a tree like traversals, searching etc. and will present some easy and innovative ways to visualise and understand them better.