Advantages of Threaded Binary TreeA Binary Tree is a data structure that can be represented with the help of an Array or a Linked List. Whenever the representation of a Binary Tree is done using a Linked List, the nodes of the list are not stored at adjacent or neighboring memory addresses. They are linked to each other through the parent-child relationship associated with trees. In this representation, every node consists of three different components:
If a node in a Binary Tree does not have a left or right child or is a leaf node, then the absence child node is denoted by a NULL pointer. Let us consider the following Binary Tree as an example. Figure 1. A Binary Tree The above figure exemplifies that there are many nodes present in this that hold a NULL value in their Left or Right child pointer. We can use the space occupied by these NULL values in order to store some kind of valuable information. One feasible way to use this space is to have a special pointer pointing to the nodes higher in the Tree (i.e., Ancestors). These special pointers are known as Threads, and the Binary Tree consisting of such pointers is known as the Threaded Binary Tree. A Threaded Binary Tree is one of the variants of a normal Binary Tree that assists faster tree traversal and doesn't need a Stack or Recursion. It reduces memory wastage by placing the null pointers of a leaf node to the in-order successor or in-order predecessor. In the following tutorial, we will learn about the advantages of the Threaded Binary Tree. But before we get started, let's briefly understand the Threaded Binary Tree and its types. Understanding the Threaded Binary TreeA Threaded Binary Tree is a Binary Tree where all left child pointers are NULL points to its in-order predecessor, and all right child pointers are NULL points to its in-order successor. Moreover, the leftmost and the rightmost child pointer of a Treaded Binary Tree always points to null as their in-order predecessor and successor do not exist. Let us consider the following example of a Threaded Binary Tree in order to understand the above statements. Figure 2. A Threaded Binary Tree In the diagram shown above, we can observe that the right pointer of the node value 7 points to 9:
The primary objective of creating such as structure is to make the in-order and pre-order traversal of the binary tree faster without any help of additional data structure (like an auxiliary stack) or memory for its traversal. Understanding the Types of Threaded Binary TreeThere are two types of Threaded Binary Trees:
Let us understand the types mentioned earlier in brief. Single Threaded Binary TreeThe Single Threaded Binary Tree is a Threaded Binary Tee where only the right NULL pointers are made in order to point to the in-order successor. Structure of a Node in Single Threaded Binary Trees: The structure of a node in a Binary Threaded Tree is pretty much identical to that of a Binary Tree; however, with a few adjustments. In Threaded Binary Trees, we use additional Boolean variables in the node structure. We will only use a variable representing the right thread for the Single Threaded Binary Trees. Let us consider the following snippet of code illustrating the structure of a node in C++ Language: Syntax: Let us now consider the following diagram depicting an example of a Single Threaded Binary Tree. Figure 3. A Single Threaded Binary Tree In the diagram shown above, we can observe that the node value 10 does not consist of any child nodes. So, the right pointer of node value 10 is null; therefore, it is pointing to its in-order successor, i.e., node value 20, through a thread. In the same way, the other nodes of this tree consisting of a right null pointer refer to their in-order successor, as depicted in the diagram. Double Threaded Binary TreeThe Double Threaded Binary Tree is a Threaded Binary Tree where the left, as well as the right NULL pointers, are made in order to point towards the in-order predecessor and in-order successor, respectively. (Here, the left threads are supportable in the reverse in-order traversal of the tree.) We will use two Boolean variables representing the left and right threads for the Double Threaded Binary Trees. Let us consider the following snippet of code illustrating the structure of a node in C++ Language: Syntax: In the above snippet of code, the Boolean variables, left_thread, and right_thread, help us to differentiate whether the left/right pointer stores the in-order predecessor/successor or left/right child. Let us now consider the following diagram depicting an example of a Single Threaded Binary Tree. Figure 4. A Double Threaded Binary Tree The above diagram is a representation of a Double Threaded Binary Tree. Here, we can observe that the node value 30 has no left and right child. Therefore, its left pointer points towards its in-order predecessor, i.e., node value 20, and its right pointer points towards its in-order successor, i.e., node value 40. In a similar way, the other nodes of this tree with a left/right null pointer refer to their in-order predecessor/successor with the help of the threads. Understanding the Advantages of the Threaded Binary TreeThere are numerous advantages of a Threaded Binary Tree over a Non-Threaded Binary Tree. Some of them are listed below:
Understanding some Disadvantages of the Threaded Binary TreeLet us now discuss some of the disadvantages of the Threaded Binary Tree:
The ConclusionThe above tutorial taught us about the Threaded Binary Tree and its types. We have also discussed various advantages of the Threaded Binary Tree along with some disadvantages. Next TopicSerialize and Deserialize a Binary Tree |