Idea of Threaded Binary Tree is to make inorder traversal faster and do it without stack and without recursion. In a simple threaded binary tree, the NULL right pointers are used to store inorder successor. Wherever a right pointer is NULL, it is used to store inorder successor.
Following diagram shows an example Single Threaded Binary Tree. The dotted lines represent threads.

Following is structure of a single-threaded binary tree.
C++
struct Node
{
int key;
Node *left, *right;
bool isThreaded;
};
|
Java
static class Node
{
int key;
Node left, right;
boolean isThreaded;
};
|
Python3
class Node:
def __init__( self , data):
self .key = data;
self .left = none;
self .right = none;
self .isThreaded = false;
|
C#
public class Node {
public int key;
public Node left, right;
public bool isThreaded;
};
|
Javascript
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
this .isThreaded = false ;
}
}
|
How to convert a Given Binary Tree to Threaded Binary Tree?
We have discussed a Queue-based solution here. In this post, space-efficient solution is discussed that doesn’t require a queue.
The idea is based on the fact that we link from inorder predecessor to a node. We link those inorder predecessor which lie in subtree of node. So we find inorder predecessor of a node if its left is not NULL. Inorder predecessor of a node (whose left is NULL) is a rightmost node in the left child. Once we find the predecessor, we link a thread from it to the current node.
Following is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int key;
Node *left, *right;
bool isThreaded;
};
Node *createThreaded(Node *root)
{
if (root == NULL)
return NULL;
if (root->left == NULL &&
root->right == NULL)
return root;
if (root->left != NULL)
{
Node* l = createThreaded(root->left);
l->right = root;
l->isThreaded = true ;
}
if (root->right == NULL)
return root;
return createThreaded(root->right);
}
Node *leftMost(Node *root)
{
while (root != NULL && root->left != NULL)
root = root->left;
return root;
}
void inOrder(Node *root)
{
if (root == NULL) return ;
Node *cur = leftMost(root);
while (cur != NULL)
{
cout << cur->key << " " ;
if (cur->isThreaded)
cur = cur->right;
else
cur = leftMost(cur->right);
}
}
Node *newNode( int key)
{
Node *temp = new Node;
temp->left = temp->right = NULL;
temp->key = key;
return temp;
}
int main()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
createThreaded(root);
cout << "Inorder traversal of created "
"threaded tree is\n" ;
inOrder(root);
return 0;
}
|
Java
import java.util.*;
class solution
{
static class Node
{
int key;
Node left, right;
boolean isThreaded;
};
static Node createThreaded(Node root)
{
if (root == null )
return null ;
if (root.left == null &&
root.right == null )
return root;
if (root.left != null )
{
Node l = createThreaded(root.left);
l.right = root;
l.isThreaded = true ;
}
if (root.right == null )
return root;
return createThreaded(root.right);
}
static Node leftMost(Node root)
{
while (root != null && root.left != null )
root = root.left;
return root;
}
static void inOrder(Node root)
{
if (root == null ) return ;
Node cur = leftMost(root);
while (cur != null )
{
System.out.print(cur.key + " " );
if (cur.isThreaded)
cur = cur.right;
else
cur = leftMost(cur.right);
}
}
static Node newNode( int key)
{
Node temp = new Node();
temp.left = temp.right = null ;
temp.key = key;
return temp;
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right.left = newNode( 6 );
root.right.right = newNode( 7 );
createThreaded(root);
System.out.println( "Inorder traversal of created " + "threaded tree is\n" );
inOrder(root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .left = self .right = None
self .key = key
self .isThreaded = None
def createThreaded(root):
if root = = None :
return None
if root.left = = None and root.right = = None :
return root
if root.left ! = None :
l = createThreaded(root.left)
l.right = root
l.isThreaded = True
if root.right = = None :
return root
return createThreaded(root.right)
def leftMost(root):
while root ! = None and root.left ! = None :
root = root.left
return root
def inOrder(root):
if root = = None :
return
cur = leftMost(root)
while cur ! = None :
print (cur.key, end = " " )
if cur.isThreaded:
cur = cur.right
else :
cur = leftMost(cur.right)
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right.left = newNode( 6 )
root.right.right = newNode( 7 )
createThreaded(root)
print ( "Inorder traversal of created" ,
"threaded tree is" )
inOrder(root)
|
C#
using System;
public class solution
{
public class Node
{
public int key;
public Node left, right;
public bool isThreaded;
}
public static Node createThreaded(Node root)
{
if (root == null )
{
return null ;
}
if (root.left == null && root.right == null )
{
return root;
}
if (root.left != null )
{
Node l = createThreaded(root.left);
l.right = root;
l.isThreaded = true ;
}
if (root.right == null )
{
return root;
}
return createThreaded(root.right);
}
public static Node leftMost(Node root)
{
while (root != null && root.left != null )
{
root = root.left;
}
return root;
}
public static void inOrder(Node root)
{
if (root == null )
{
return ;
}
Node cur = leftMost(root);
while (cur != null )
{
Console.Write(cur.key + " " );
if (cur.isThreaded)
{
cur = cur.right;
}
else
{
cur = leftMost(cur.right);
}
}
}
public static Node newNode( int key)
{
Node temp = new Node();
temp.left = temp.right = null ;
temp.key = key;
return temp;
}
public static void Main( string [] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
createThreaded(root);
Console.WriteLine( "Inorder traversal of created " + "threaded tree is\n" );
inOrder(root);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
this .isThreaded = false ;
}
}
function createThreaded(root)
{
if (root == null )
return null ;
if (root.left == null &&
root.right == null )
return root;
if (root.left != null )
{
var l = createThreaded(root.left);
l.right = root;
l.isThreaded = true ;
}
if (root.right == null )
return root;
return createThreaded(root.right);
}
function leftMost(root)
{
while (root != null && root.left != null )
root = root.left;
return root;
}
function inOrder(root)
{
if (root == null ) return ;
var cur = leftMost(root);
while (cur != null )
{
document.write(cur.key + " " );
if (cur.isThreaded)
cur = cur.right;
else
cur = leftMost(cur.right);
}
}
function newNode(key)
{
var temp = new Node();
temp.left = temp.right = null ;
temp.key = key;
return temp;
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
createThreaded(root);
document.write( "Inorder traversal of created " + "threaded tree is<br/>" );
inOrder(root);
</script>
|
OutputInorder traversal of created threaded tree is
4 2 5 1 6 3 7
Time complexity: O(n).
space complexity: O(1).
This article is contributed by Gopal Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.