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).
There are two types of threaded binary trees.
Single Threaded: Where a NULL right pointers is made to point to the inorder successor (if successor exists)
Double Threaded: Where both left and right NULL pointers are made to point to inorder predecessor and inorder successor respectively. The predecessor threads are useful for reverse inorder traversal and postorder traversal.
The threads are also useful for fast accessing ancestors of a node.
Following diagram shows an example Single Threaded Binary Tree. The dotted lines represent threads.

C and C++ representation of a Threaded Node
Following is C and C++representation of a single-threaded node.
C
struct Node
{
int data;
struct Node *left, *right;
bool rightThread;
}
|
C++
struct node {
int data;
struct node* left;
struct node* right;
bool rightThread;
};
class Node {
public :
int data;
Node* left;
Node* right;
bool rightThread;
Node( int val){
data = val;
left = NULL;
right = NULL;
rightThread = false ;
}
};
|
Java representation of a Threaded Node
Following is Java representation of a single-threaded node.
Java
static class Node
{
int data;
Node left, right;
boolean rightThread;
}
|
Python3
class Node:
def __init__( self , data,rightThread):
self .data = data;
self .left = None ;
self .right = None ;
self .rightThread = rightThread;
|
C#
public class Node
{
public int data;
public Node left, right;
public bool rightThread;
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
this .rightThread = false ;
}
}
</script>
|
Since right pointer is used for two purposes, the boolean variable rightThread is used to indicate whether right pointer points to right child or inorder successor. Similarly, we can add leftThread for a double threaded binary tree.
Inorder Traversal using Threads
Following is code for inorder traversal in a threaded binary tree.
C
struct Node* leftMost( struct Node* n)
{
if (n == NULL)
return NULL;
while (n->left != NULL)
n = n->left;
return n;
}
void inOrder( struct Node* root)
{
struct Node* cur = leftMost(root);
while (cur != NULL) {
printf ( "%d " , cur->data);
if (cur->rightThread)
cur = cur->right;
else
cur = leftmost(cur->right);
}
}
|
Java
Node leftMost(Node n)
{
if (n == null )
return null ;
while (n.left != null )
n = n.left;
return n;
}
static void inOrder(Node root)
{
Node cur = leftMost(root);
while (cur != null ) {
System.out.printf( "%d " , cur.data);
if (cur.rightThread)
cur = cur.right;
else
cur = leftmost(cur.right);
}
}
|
Python3
def leftMost(n):
if (n = = None ):
return None ;
while (n.left ! = None ):
n = n.left;
return n;
def inOrder(root):
cur = leftMost(root);
while (cur ! = None ):
print (cur.data, " " );
if (cur.rightThread):
cur = cur.right;
else :
cur = leftmost(cur.right);
|
C#
Node leftMost(Node n)
{
if (n == null )
return null ;
while (n.left != null )
n = n.left;
return n;
}
static void inOrder(Node root)
{
Node cur = leftMost(root);
while (cur != null )
{
Console.Write( "{0} " , cur.data);
if (cur.rightThread)
cur = cur.right;
else
cur = leftmost(cur.right);
}
}
|
Javascript
<script>
function leftMost(n)
{
if (n == null )
return null ;
while (n.left != null )
n = n.left;
return n;
}
function inOrder(root)
{
let cur = leftMost(root);
while (cur != null ) {
document.write(cur.data+ " " );
if (cur.rightThread)
cur = cur.right;
else
cur = leftmost(cur.right);
}
}
</script>
|
C++
Node* leftMost(Node* n){
if (n == NULL)
return NULL;
while (n->left != NULL)
n = n->left;
return n;
}
void inOrder(Node* root){
Node* cur = leftMost(root);
while (cur != NULL) {
cout<<cur->data<< " " ;
if (cur->rightThread)
cur = cur->right;
else
cur = leftmost(cur->right);
}
}
|
Following diagram demonstrates inorder order traversal using threads.

Advantages of Threaded Binary Tree
- In this Tree it enables linear traversal of elements.
- It eliminates the use of stack as it perform linear traversal, so save memory.
- Enables to find parent node without explicit use of parent pointer
- Threaded tree give forward and backward traversal of nodes by in-order fashion
- Nodes contain pointers to in-order predecessor and successor
- For a given node, we can easily find inorder predecessor and successor. So, searching is much more easier.
Disadvantages of Threaded Binary Tree
- Every node in threaded binary tree need extra information(extra memory) to indicate whether its left or right node indicated its child nodes or its inorder predecessor or successor. So, the node consumes extra memory to implement.
- Insertion and deletion are way more complex and time consuming than the normal one since both threads and ordinary links need to be maintained.
We will soon be discussing insertion and deletion in threaded binary trees.
Sources:
http://en.wikipedia.org/wiki/Threaded_binary_tree
www.cs.berkeley.edu/~kamil/teaching/su02/080802.ppt
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Please Login to comment...