Open In App

Threaded Binary Tree

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).

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.

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.


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. 

threadedBT


C and C++ representation of a Threaded Node 
Following is C and C++representation of a single-threaded node.

// Use any below method to represent a Threaded Node

// Method 1: Using "struct" to make
// user-define data type
struct node {
    int data;
    struct node* left;
    struct node* right;
    bool rightThread;
};

// Method 2: Using "class" to make
// user-define data type
class Node {
public:
    int data;
    Node* left;
    Node* right;
    bool rightThread;
    // Val is the key or the value that has to be added to
    // the data part
    Node(int val){
        data = val;
        // Left and right child for node will be initialized
        // to null
        left = NULL;
        right = NULL;
        // rightThread will be initialized to false
        rightThread = false;
    }
};

// This code is contributed by Susobhan Akhuli
typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
    bool rightThread;
} Node;

// Constructor function
Node* createNode(int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode != NULL) {
        newNode->data = val;
        newNode->left = NULL;
        newNode->right = NULL;
        newNode->rightThread = false;
    }
    return newNode;
}
// Method 1: Using "struct" to define a threaded node
class ThreadedNode1 {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
        this.rightThread = false;
    }
}

// Method 2: Using "class" to define a threaded node
class ThreadedNode2 {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
        this.rightThread = false;
    }
}

Java representation of a Threaded Node

Following is Java representation of a single-threaded node.

static class Node 
{
    int data;
    Node left, right;
    boolean rightThread;  
}

// This code contributed by aashish1995 

Python3 representation of a Threaded Node

Following is Python3 representation of a single-threaded node.

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
        self.rightThread = False

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.

// Utility function to find leftmost node in a tree rooted with n
Node* leftMost(Node* n){
    if (n == NULL)
        return NULL;

    while (n->left != NULL)
        n = n->left;

    return n;
}

// CPP code to do inorder traversal in a threaded binary tree
void inOrder(Node* root){
    Node* cur = leftMost(root);
    while (cur != NULL) {
        cout<<cur->data<<" ";

        // If this node is a thread node, then go to
        // inorder successor
        if (cur->rightThread)
            cur = cur->right;
        else // Else go to the leftmost child in right
             // subtree
            cur = leftmost(cur->right);
    }
}

// This code is contributed by Susobhan Akhuli
// Utility function to find leftmost node in a tree rooted
// with n
struct Node* leftMost(struct Node* n)
{
    if (n == NULL)
        return NULL;

    while (n->left != NULL)
        n = n->left;

    return n;
}

// C code to do inorder traversal in a threaded binary tree
void inOrder(struct Node* root)
{
    struct Node* cur = leftMost(root);
    while (cur != NULL) {
        printf("%d ", cur->data);

        // If this node is a thread node, then go to
        // inorder successor
        if (cur->rightThread)
            cur = cur->right;
        else // Else go to the leftmost child in right
             // subtree
            cur = leftmost(cur->right);
    }
}
// Utility function to find leftmost node in a tree rooted
// with n
Node leftMost(Node n)
{
    if (n == null)
        return null;

    while (n.left != null)
        n = n.left;

    return n;
}

// C code to do inorder traversal in a threaded binary tree
static void inOrder(Node root)
{
    Node cur = leftMost(root);
    while (cur != null) {
        System.out.printf("%d ", cur.data);

        // If this node is a thread node, then go to
        // inorder successor
        if (cur.rightThread)
            cur = cur.right;
        else // Else go to the leftmost child in right
             // subtree
            cur = leftmost(cur.right);
    }
}

// This code contributed by aashish1995
// Utility function to find leftmost node in a tree rooted
// with n
Node leftMost(Node n)
{
  if (n == null)
    return null;

  while (n.left != null)
    n = n.left;

  return n;
}

// C code to do inorder traversal in a threaded binary tree
static void inOrder(Node root)
{
  Node cur = leftMost(root);
  while (cur != null) 
  {
    Console.Write("{0} ", cur.data);

    // If this node is a thread node, then go to
    // inorder successor
    if (cur.rightThread)
      cur = cur.right;
    else // Else go to the leftmost child in right
      // subtree
      cur = leftmost(cur.right);
  }
}

// This code is contributed by gauravrajput1
<script>

// Utility function to find leftmost node in a tree rooted
// with n
function leftMost(n)
{
    if (n == null)
        return null;
 
    while (n.left != null)
        n = n.left;
 
    return n;
}

// JavaScript code to do inorder traversal in 
// a threaded binary tree
function inOrder(root)
{
    let cur = leftMost(root);
    while (cur != null) {
        document.write(cur.data+" ");
 
        // If this node is a thread node, then go to
        // inorder successor
        if (cur.rightThread)
            cur = cur.right;
        else // Else go to the leftmost child in right
             // subtree
            cur = leftmost(cur.right);
        }
}

// This code is contributed by unknown2108

</script>
# Utility function to find leftmost Node in a tree rooted
# with n
def leftMost(n):

    if (n == None):
        return None;

    while (n.left != None):
        n = n.left;

    return n;


# C code to do inorder traversal in a threaded binary tree
def inOrder(root):

    cur = leftMost(root);
    while (cur != None):
        print(cur.data," ");

        # If this Node is a thread Node, then go to
        # inorder successor
        if (cur.rightThread):
            cur = cur.right;
        else: # Else go to the leftmost child in right
             # subtree
            cur = leftmost(cur.right);

# This code is contributed by Rajput-Ji 

Following diagram demonstrates inorder order traversal using threads.
 

threadedTraversal

Advantages of Threaded Binary Tree

Disadvantages of Threaded Binary Tree

Applications of threaded binary tree -

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

Article Tags :