Threadedbin TreeImplementation
Threadedbin TreeImplementation
1. In the linked representation of binary trees, more than one half of the link fields contain NULL values
which results in wastage of storage space. If a binary tree consists of n nodes then n+1 link fields contain
NULL values. So in order to effectively manage the space, a method was devised by Perlis and Thornton
in which the NULL links are replaced with special links known as threads. Such binary trees with
threads are known as threaded binary trees. Each node in a threaded binary tree either contains a link to
its child node or thread to other nodes in the tree.
2. Inorder traversal of a Binary tree can either be done using recursion or with the use of a auxiliary stack.
The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without
recursion. A binary tree is made threaded by making all right child pointers that would normally be
NULL point to the inorder successor of the node (if it exists).
3. A threaded binary tree is a type of binary tree data structure where the empty left and right child pointers
in a binary tree are replaced with threads that link nodes directly to their in-order predecessor or
successor, thereby providing a way to traverse the tree without using recursion or a stack.
4. Threaded binary trees can be useful when space is a concern, as they can eliminate the need for a stack
during traversal. However, they can be more complex to implement than standard binary trees.
A Binary Tree
In one-way threaded binary trees, a thread will appear either in the right or left link field of a node. If it appears
in the right link field of a node then it will point to the next node that will appear on performing in order
traversal. Such trees are called Right threaded binary trees. If thread appears in the left field of a node then it
will point to the nodes inorder predecessor. Such trees are called Left threaded binary trees. Left threaded
binary trees are used less often as they don't yield the last advantages of right threaded binary trees. In one-way
threaded binary trees, the right link field of last node and left link field of first node contains a NULL. In order to
distinguish threads from normal links they are represented by dotted lines.
The above figure shows the inorder traversal of this binary tree yields D, B, E, A, C, F. When this tree is
represented as a right threaded binary tree, the right link field of leaf node D which contains a NULL value is
replaced with a thread that points to node B which is the inorder successor of a node D. In the same way other
nodes containing values in the right link field will contain NULL value.
In two-way threaded Binary trees, the right link field of a node containing NULL values is replaced by a thread
that points to nodes inorder successor and left field of a node containing NULL values is replaced by a thread
that points to nodes inorder predecessor.
The above figure shows the inorder traversal of this binary tree yields D, B, E, G, A, C, F. If we consider the
two-way threaded Binary tree, the node E whose left field contains NULL is replaced by a thread pointing to its
inorder predecessor i.e. node B. Similarly, for node G whose right and left linked fields contain NULL values are
replaced by threads such that right link field points to its inorder successor and left link field points to its inorder
predecessor. In the same way, other nodes containing NULL values in their link fields are filled with threads.
In the above figure of two-way threaded Binary tree, we noticed that no left thread is possible for the first node
and no right thread is possible for the last node. This is because they don't have any inorder predecessor and
successor respectively. This is indicated by threads pointing nowhere. So in order to maintain the uniformity of
threads, we maintain a special node called the header node. The header node does not contain any data part and
its left link field points to the root node and its right link field points to itself. If this header node is included in
the two-way threaded Binary tree then this node becomes the inorder predecessor of the first node and inorder
successor of the last node. Now threads of left link fields of the first node and right link fields of the last node
will point to the header node.
✔ In threaded binary tree, linear and fast traversal of nodes in the tree so there is no requirement of stack. If
the stack is used then it consumes a lot of memory and time.
✔ It is more general as one can efficiently determine the successor and predecessor of any node by simply
following the thread and links. It almost behaves like a circular linked list.
✔ When implemented, the threaded binary tree needs to maintain the extra information for each node to
indicate whether the link field of each node points to an ordinary node or the node's successor and
predecessor.
✔ Insertion into and deletion from a threaded binary tree are more time consuming since both threads and
ordinary links need to be maintained.
// Insertion in Threaded Binary Search Tree.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
struct Node *left, *right;
int info;
if (par == NULL)
{
root = tmp;
tmp -> left = NULL;
tmp -> right = NULL;
}
else if (ikey < (par -> info))
{
tmp -> left = par -> left;
tmp -> right = par;
par -> lthread = 0;
par -> left = tmp;
}
else
{
tmp -> left = par;
tmp -> right = par -> right;
par -> rthread = 0;
par -> right = tmp;
}
return root;
}
// Driver Program
int main()
{
struct Node *root = NULL;
inorder(root);
return 0;
}