Before starting with our main topic “Advantages of Threaded Binary Tree”, did you know what is Threaded Binary Tree? If not, then, a threaded binary tree is a variation of a normal binary tree that helps to easily traverse the tree in a more optimised manner by introducing new additional links, known as threads.

## What is the purpose of the threaded binary tree?

In Threaded Binary Tree, we have a special pointer called “thread” and with the use of these thread pointer, we can easily establish new connections between the nodes.

However, a threaded binary tree allows for efficient traversal without the help of extra space or recursive function calls.

Let us understand it with an example,

• So, In a threaded binary tree, the leaf node null pointers are replaced with special threads ( represented as dotted lines) that point towards the in-order predecessor or successor of the node.
• And these threads will establish a connection between the nodes to traverse in a specific order and if there is no right and left child node, then it will be declared as a null node.
• If there is a right and a left child node, then it will be pointed by the parent node according to the traverse path.

So, there are two types of threading:

One-way threading is done only from one side whether it can be from the left child node or right child node. But, we have to follow the same direction to thread for every node.

This threading can be done by both sides depending on the order of the tree.

## Understand the concept of threading with the example:

So, in the above figure, we have a diagram of a single-threaded binary tree and if we write the in-order traversal route, then, it will be 1,3,5,6,7,8,9,11,13.

### For the single-threaded binary tree,

We have to either make the threads for all left nodes or right nodes, we cannot make threads simultaneously for both the child nodes in single threading.

If we are following the right node approach,

1. We will first take the leave Node 1 and after Node 1 in the in-order traversal, we have Node 3. So, Node 1 will make a thread towards Node 3.

2. Next, we will move to the next leaf node which is Node 5. On the right side of Node 5 in the in-order traverse, we have Node 6. So, it will be pointed towards root Node 6.

3. Then, in the same way, Node 7 will be pointed towards Node 8 and Node 9 will make a thread toward Node 11.

4. And for the last leaf Node 13, we can see there is no node present after Node 13 in the transverse route. So, it will not be threaded towards any node and remain null.

So, in the double threaded we will make threads on both the left and right child of a leaf node.

1. For Node 1, According to the in-order traverse (1,3,5,6,7,8,9,11,13), we can see there is no node present on the left side of Node 1 according to the traverse path and on the right side, we have Node 3. So, Node 1 will be threaded toward Node 3 only.

2. For Node 5, we will thread the left node towards Node 3 and the right node is thread towards Node 6.

3. Node 7, Node 9 and Node 13 will also follow the same approach.

So, in this, we will make our single and double-threaded binary tree.

## Why do we make threads for the leaf node only?

1. We make threads of leaf nodes only because leaves nodes have no left or right child and creating threads for that nodes makes the traversal operation very easier.
2.  And the other reason is that it allows the node to traverse in any direction without the need or help for additional data structure or recursive calls.
3.  If make threads for parent nodes, then it requires additional information and could lead to increased more memory usage and complexity.

## Which approach did the threaded binary tree follow?

So, there are two types of approaches to making threads:

This is one of the most used approaches.

In this approach, threads are connected to a node to its in-order predecessor or successor.

In the in-order traverse, we will first take the left node, then the root node and finally the right node.

### 2. Pre-order or post-order threads:

These threads connect a node to its pre-order or post-order predecessor and successor.

For the pre-order traverse, we will first take the root node then the left node and finally the right node.

In post-order traverse, we will first take the left node, then the right node and end up taking the root node.

In the above image, you can see all the traversal nodes.

## Difference between binary tree and threaded binary tree :

So, in a traditional binary tree, each node has, at most, two children that is a left child node and a right child node. However, in a Threaded Binary Tree, each or single node contains additional pointers called “threads” depending on the type of threaded tree.

1. In some problem statements, there is frequent use of tree traversal operations, and then a threaded binary tree is used as it eliminates the need for any direct use of other data structures or recursion.

2. Threaded binary trees give forward and backward traversal operations very easily.

3. We can easily find in order the predecessor and successor for any node. So, searching is much more easier in the threaded binary tree.

4. In a threaded binary tree there is no null pointer node present. And because of this, there is no memory wastage in occupying null nodes.

5. There is no need for stack data structure while traversing the tree node because, with the help of thread links, we can reach previously visited nodes easily.

However, there are some disadvantages also to using the threaded tree, like:

1. It increased complexity during threaded tree construction and modifications, as the threads binary tree needs to be properly tracked.

2. The implementation of threaded binary trees requires a lot of consideration as during tree insertion and delete operations, we have to make sure that the threads remain consistent and valid.

3. Implementing threads for every null node is not possible.

## Applications of threaded binary tree :

1. Threaded binary trees are widely used to evaluate arithmetic expressions.

2. It is used while database indexing.

3. In a compiler or interpreter, threaded binary trees data structure can be used to store and manage symbol tables for various variables and functions. Then, the threaded tree can be constructed with the symbols as keys and then traversed in order or pre-order to perform various operations on the symbol table.

4. In some applications, threaded binary trees can be used to navigate hierarchical data structures, such as file systems or website directories.

## Conclusion

So, in the binary trees, the leaf node has null values which results in the wastage of storage space. So in order to effectively manage the space, we use a threaded binary tree and replace the null nodes with special links known as threads. We also discussed the advantages of threaded binary tree and their application, so it already makes us realize how important this data structure is.

Myself Bharath Choudhary, software developer at Oracle.