Threaded Binary Tree | Insertion
Last Updated :
14 Sep, 2023
We have already discuss the Binary Threaded Binary Tree.
Insertion in Binary threaded tree is similar to insertion in binary tree but we will have to adjust the threads after insertion of each element.
C representation of Binary Threaded Node:
struct Node
{
struct Node *left, *right;
int info;
// false if left pointer points to predecessor
// in Inorder Traversal
boolean lthread;
// false if right pointer points to successor
// in Inorder Traversal
boolean rthread;
};
In the following explanation, we have considered Binary Search Tree (BST) for insertion as insertion is defined by some rules in BSTs.
Let tmp be the newly inserted node. There can be three cases during insertion:
Case 1: Insertion in empty tree
Both left and right pointers of tmp will be set to NULL and new node becomes the root.
root = tmp;
tmp -> left = NULL;
tmp -> right = NULL;
Case 2: When new node inserted as the left child
After inserting the node at its proper place we have to make its left and right threads points to inorder predecessor and successor respectively. The node which was inorder successor. So the left and right threads of the new node will be-
tmp -> left = par ->left;
tmp -> right = par;
Before insertion, the left pointer of parent was a thread, but after insertion it will be a link pointing to the new node.
par -> lthread = false;
par -> left = temp;
Following example show a node being inserted as left child of its parent.

After insertion of 13,

Predecessor of 14 becomes the predecessor of 13, so left thread of 13 points to 10.
Successor of 13 is 14, so right thread of 13 points to left child which is 13.
Left pointer of 14 is not a thread now, it points to left child which is 13.
Case 3: When new node is inserted as the right child
The parent of tmp is its inorder predecessor. The node which was inorder successor of the parent is now the inorder successor of this node tmp. So the left and right threads of the new node will be-
tmp -> left = par;
tmp -> right = par -> right;
Before insertion, the right pointer of parent was a thread, but after insertion it will be a link pointing to the new node.
par -> rthread = false;
par -> right = tmp;
Following example shows a node being inserted as right child of its parent.

After 15 inserted,

Successor of 14 becomes the successor of 15, so right thread of 15 points to 16
Predecessor of 15 is 14, so left thread of 15 points to 14.
Right pointer of 14 is not a thread now, it points to right child which is 15.
C++ implementation to insert a new node in Threaded Binary Search Tree:
Like standard BST insert, we search for the key value in the tree. If key is already present, then we return otherwise the new key is inserted at the point where search terminates. In BST, search terminates either when we find the key or when we reach a NULL left or right pointer. Here all left and right NULL pointers are replaced by threads except left pointer of first node and right pointer of last node. So here search will be unsuccessful when we reach a NULL pointer or a thread.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
struct Node *left, *right;
int info;
bool lthread;
bool rthread;
};
struct Node *insert( struct Node *root, int ikey)
{
Node *ptr = root;
Node *par = NULL;
while (ptr != NULL)
{
if (ikey == (ptr->info))
{
printf ( "Duplicate Key !\n" );
return root;
}
par = ptr;
if (ikey < ptr->info)
{
if (ptr -> lthread == false )
ptr = ptr -> left;
else
break ;
}
else
{
if (ptr->rthread == false )
ptr = ptr -> right;
else
break ;
}
}
Node *tmp = new Node;
tmp -> info = ikey;
tmp -> lthread = true ;
tmp -> rthread = true ;
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 = false ;
par -> left = tmp;
}
else
{
tmp -> left = par;
tmp -> right = par -> right;
par -> rthread = false ;
par -> right = tmp;
}
return root;
}
struct Node *inorderSuccessor( struct Node *ptr)
{
if (ptr -> rthread == true )
return ptr->right;
ptr = ptr -> right;
while (ptr -> lthread == false )
ptr = ptr -> left;
return ptr;
}
void inorder( struct Node *root)
{
if (root == NULL)
printf ( "Tree is empty" );
struct Node *ptr = root;
while (ptr -> lthread == false )
ptr = ptr -> left;
while (ptr != NULL)
{
printf ( "%d " ,ptr -> info);
ptr = inorderSuccessor(ptr);
}
}
int main()
{
struct Node *root = NULL;
root = insert(root, 20);
root = insert(root, 10);
root = insert(root, 30);
root = insert(root, 5);
root = insert(root, 16);
root = insert(root, 14);
root = insert(root, 17);
root = insert(root, 13);
inorder(root);
return 0;
}
|
Java
import java.util.*;
public class solution
{
static class Node
{
Node left, right;
int info;
boolean lthread;
boolean rthread;
};
static Node insert( Node root, int ikey)
{
Node ptr = root;
Node par = null ;
while (ptr != null )
{
if (ikey == (ptr.info))
{
System.out.printf( "Duplicate Key !\n" );
return root;
}
par = ptr;
if (ikey < ptr.info)
{
if (ptr . lthread == false )
ptr = ptr . left;
else
break ;
}
else
{
if (ptr.rthread == false )
ptr = ptr . right;
else
break ;
}
}
Node tmp = new Node();
tmp . info = ikey;
tmp . lthread = true ;
tmp . rthread = true ;
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 = false ;
par . left = tmp;
}
else
{
tmp . left = par;
tmp . right = par . right;
par . rthread = false ;
par . right = tmp;
}
return root;
}
static Node inorderSuccessor( Node ptr)
{
if (ptr . rthread == true )
return ptr.right;
ptr = ptr . right;
while (ptr . lthread == false )
ptr = ptr . left;
return ptr;
}
static void inorder( Node root)
{
if (root == null )
System.out.printf( "Tree is empty" );
Node ptr = root;
while (ptr . lthread == false )
ptr = ptr . left;
while (ptr != null )
{
System.out.printf( "%d " ,ptr . info);
ptr = inorderSuccessor(ptr);
}
}
public static void main(String[] args)
{
Node root = null ;
root = insert(root, 20 );
root = insert(root, 10 );
root = insert(root, 30 );
root = insert(root, 5 );
root = insert(root, 16 );
root = insert(root, 14 );
root = insert(root, 17 );
root = insert(root, 13 );
inorder(root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .info = key
self .left = None
self .right = None
self .lthread = True
self .rthread = True
def insert(root, ikey):
ptr = root
par = None
while ptr ! = None :
if ikey = = (ptr.info):
print ( "Duplicate Key !" )
return root
par = ptr
if ikey < ptr.info:
if ptr.lthread = = False :
ptr = ptr.left
else :
break
else :
if ptr.rthread = = False :
ptr = ptr.right
else :
break
tmp = newNode(ikey)
if par = = None :
root = tmp
tmp.left = None
tmp.right = None
elif ikey < (par.info):
tmp.left = par.left
tmp.right = par
par.lthread = False
par.left = tmp
else :
tmp.left = par
tmp.right = par.right
par.rthread = False
par.right = tmp
return root
def inorderSuccessor(ptr):
if ptr.rthread = = True :
return ptr.right
ptr = ptr.right
while ptr.lthread = = False :
ptr = ptr.left
return ptr
def inorder(root):
if root = = None :
print ( "Tree is empty" )
ptr = root
while ptr.lthread = = False :
ptr = ptr.left
while ptr ! = None :
print (ptr.info,end = " " )
ptr = inorderSuccessor(ptr)
if __name__ = = '__main__' :
root = None
root = insert(root, 20 )
root = insert(root, 10 )
root = insert(root, 30 )
root = insert(root, 5 )
root = insert(root, 16 )
root = insert(root, 14 )
root = insert(root, 17 )
root = insert(root, 13 )
inorder(root)
|
C#
using System;
public class solution
{
public class Node
{
public Node left, right;
public int info;
public bool lthread;
public bool rthread;
}
public static Node insert(Node root, int ikey)
{
Node ptr = root;
Node par = null ;
while (ptr != null )
{
if (ikey == (ptr.info))
{
Console.Write( "Duplicate Key !\n" );
return root;
}
par = ptr;
if (ikey < ptr.info)
{
if (ptr.lthread == false )
{
ptr = ptr.left;
}
else
{
break ;
}
}
else
{
if (ptr.rthread == false )
{
ptr = ptr.right;
}
else
{
break ;
}
}
}
Node tmp = new Node();
tmp.info = ikey;
tmp.lthread = true ;
tmp.rthread = true ;
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 = false ;
par.left = tmp;
}
else
{
tmp.left = par;
tmp.right = par.right;
par.rthread = false ;
par.right = tmp;
}
return root;
}
public static Node inorderSuccessor(Node ptr)
{
if (ptr.rthread == true )
{
return ptr.right;
}
ptr = ptr.right;
while (ptr.lthread == false )
{
ptr = ptr.left;
}
return ptr;
}
public static void inorder(Node root)
{
if (root == null )
{
Console.Write( "Tree is empty" );
}
Node ptr = root;
while (ptr.lthread == false )
{
ptr = ptr.left;
}
while (ptr != null )
{
Console.Write( "{0:D} " ,ptr.info);
ptr = inorderSuccessor(ptr);
}
}
public static void Main( string [] args)
{
Node root = null ;
root = insert(root, 20);
root = insert(root, 10);
root = insert(root, 30);
root = insert(root, 5);
root = insert(root, 16);
root = insert(root, 14);
root = insert(root, 17);
root = insert(root, 13);
inorder(root);
}
}
|
Javascript
<script>
class Node {
constructor(){
this .left = null , this .right = null ;
this .info = 0;
this .lthread = false ;
this .rthread = false ;
}
}
function insert(root , ikey) {
var ptr = root;
var par = null ;
while (ptr != null ) {
if (ikey == (ptr.info)) {
document.write( "Duplicate Key !\n" );
return root;
}
par = ptr;
if (ikey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
var tmp = new Node();
tmp.info = ikey;
tmp.lthread = true ;
tmp.rthread = true ;
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 = false ;
par.left = tmp;
} else {
tmp.left = par;
tmp.right = par.right;
par.rthread = false ;
par.right = tmp;
}
return root;
}
function inorderSuccessor(ptr) {
if (ptr.rthread == true )
return ptr.right;
ptr = ptr.right;
while (ptr.lthread == false )
ptr = ptr.left;
return ptr;
}
function inorder(root) {
if (root == null )
document.write( "Tree is empty" );
var ptr = root;
while (ptr.lthread == false )
ptr = ptr.left;
while (ptr != null ) {
document.write(ptr.info+ " " );
ptr = inorderSuccessor(ptr);
}
}
var root = null ;
root = insert(root, 20);
root = insert(root, 10);
root = insert(root, 30);
root = insert(root, 5);
root = insert(root, 16);
root = insert(root, 14);
root = insert(root, 17);
root = insert(root, 13);
inorder(root);
</script>
|
Output
5 10 13 14 16 17 20 30
Time Complexity: O(log N)
Space Complexity: O(1), since no extra space used.