Double Threaded Binary Search Tree: is a binary search tree in which the nodes are not every left NULL pointer points to its inorder predecessor and the right NULL pointer points to the inorder successor.
The threads are also useful for fast accessing the ancestors of a node.
Double Threaded Binary Search Tree is one of the most used types of Advanced data structures used in many real-time applications like places where there are recent insertion and traversal of all elements of the search tree.

Creation algorithm for Double Threaded Binary Search Tree:
- In Double Threaded Binary search tree, there are five fields namely, data fields, left, right pointers, lbit, and rbit where lbit and rbit are boolean value stored to denote the right pointer points to an inorder successor or a new child node. Similarly, lbit denotes that the left pointer points to an inorder predecessor or a new child node.
- Base condition for the creation of the Double Threaded binary search tree is that the root node exists or not, If it doesn’t exist then create a new node and store it.
- Otherwise, compare the data of the current node to the new Data to be inserted, If the new data is less than the current data then traverse to the left child node. Otherwise, traverse to the right child node.
- If the left child or right child doesn’t exist then insert the node to its left and point its left and right child to the inorder predecessor and successor respectively.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class Node {
int lbit, rbit;
int value;
Node *left, *right;
public :
Node()
{
lbit = rbit = 0;
value = 0;
left = right = NULL;
}
friend class DTBT;
};
class DTBT {
Node* root;
public :
DTBT()
{
root = new Node();
root->value = 9999;
root->rbit = 1;
root->lbit = 0;
root->left = root;
root->right = root;
}
void create();
void insert( int value);
void preorder();
Node* preorderSuccessor(Node*);
void inorder();
Node* inorderSuccessor(Node*);
};
void DTBT::create()
{
int n = 9;
this ->insert(6);
this ->insert(3);
this ->insert(1);
this ->insert(5);
this ->insert(8);
this ->insert(7);
this ->insert(11);
this ->insert(9);
this ->insert(13);
}
void DTBT::insert( int data)
{
if (root->left == root
&& root->right == root) {
Node* p = new Node();
p->value = data;
p->left = root->left;
p->lbit = root->lbit;
p->rbit = 0;
p->right = root->right;
root->left = p;
root->lbit = 1;
return ;
}
Node* cur = new Node;
cur = root->left;
while (1) {
if (cur->value < data) {
Node* p = new Node();
p->value = data;
if (cur->rbit == 0) {
p->right = cur->right;
p->rbit = cur->rbit;
p->lbit = 0;
p->left = cur;
cur->rbit = 1;
cur->right = p;
return ;
}
else
cur = cur->right;
}
if (cur->value > data) {
Node* p = new Node();
p->value = data;
if (cur->lbit == 0) {
p->left = cur->left;
p->lbit = cur->lbit;
p->rbit = 0;
p->right = cur;
cur->lbit = 1;
cur->left = p;
return ;
}
else
cur = cur->left;
}
}
}
void DTBT::preorder()
{
Node* c = root->left;
while (c != root) {
cout << " " << c->value;
c = preorderSuccessor(c);
}
}
Node* DTBT::preorderSuccessor(Node* c)
{
if (c->lbit == 1) {
return c->left;
}
while (c->rbit == 0) {
c = c->right;
}
return c->right;
}
void DTBT::inorder()
{
Node* c;
c = root->left;
while (c->lbit == 1)
c = c->left;
while (c != root) {
cout << " " << c->value;
c = inorderSuccessor(c);
}
}
Node* DTBT::inorderSuccessor(Node* c)
{
if (c->rbit == 0)
return c->right;
else
c = c->right;
while (c->lbit == 1) {
c = c->left;
}
return c;
}
int main()
{
DTBT t1;
t1.create();
cout << "Inorder Traversal of DTBST\n" ;
t1.inorder();
cout << "\nPreorder Traversal of DTBST\n" ;
t1.preorder();
return 0;
}
|
Python3
class Node:
def __init__( self ):
self .lbit = None
self .rbit = None
self .value = None
self .left = None
self .right = None
class DTBT:
root = None
def __init__( self ):
self .root = Node()
self .root.value = 9999
self .root.rbit = 1
self .root.lbit = 0
self .root.left = self .root
self .root.right = self .root
def create( self ):
n = 9
self .insert( 6 )
self .insert( 3 )
self .insert( 1 )
self .insert( 5 )
self .insert( 8 )
self .insert( 7 )
self .insert( 11 )
self .insert( 9 )
self .insert( 13 )
def insert( self , data):
if ( self .root.left = = self .root and
self .root.right = = self .root):
p = Node()
p.value = data
p.left = self .root.left
p.lbit = self .root.lbit
p.rbit = 0
p.right = self .root.right
self .root.left = p
self .root.lbit = 1
return
cur = self .root.left
while ( 1 ):
if (cur.value < data):
p = Node()
p.value = data
if (cur.rbit = = 0 ):
p.right = cur.right
p.rbit = cur.rbit
p.lbit = 0
p.left = cur
cur.rbit = 1
cur.right = p
return
else :
cur = cur.right
if (cur.value > data):
p = Node()
p.value = data
if (cur.lbit = = 0 ):
p.left = cur.left
p.lbit = cur.lbit
p.rbit = 0
p.right = cur
cur.lbit = 1
cur.left = p
return
else :
cur = cur.left
def preorder( self ):
c = self .root.left
while (c ! = self .root):
print (c.value, end = ' ' )
c = self .preorderSuccessor(c)
def preorderSuccessor( self , c):
if (c.lbit = = 1 ):
return c.left
while (c.rbit = = 0 ):
c = c.right
return c.right
def inorder( self ):
c = self .root.left
while (c.lbit = = 1 ):
c = c.left
while (c ! = self .root):
print (c.value, end = ' ' )
c = self .inorderSuccessor(c)
def inorderSuccessor( self , c):
if (c.rbit = = 0 ):
return c.right
else :
c = c.right
while (c.lbit = = 1 ):
c = c.left
return c
if __name__ = = '__main__' :
t1 = DTBT()
t1.create()
print ( "Inorder Traversal of DTBST" )
t1.inorder()
print ( "\nPreorder Traversal of DTBST" )
t1.preorder()
|
Javascript
class Node {
constructor() {
this .lbit = null ;
this .rbit = null ;
this .value = null ;
this .left = null ;
this .right = null ;
}
}
class DTBT {
constructor() {
this .root = new Node();
this .root.value = 9999;
this .root.rbit = 1;
this .root.lbit = 0;
this .root.left = this .root;
this .root.right = this .root;
}
create() {
const n = 9;
this .insert(6);
this .insert(3);
this .insert(1);
this .insert(5);
this .insert(8);
this .insert(7);
this .insert(11);
this .insert(9);
this .insert(13);
}
insert(data) {
if (
this .root.left === this .root &&
this .root.right === this .root
) {
const p = new Node();
p.value = data;
p.left = this .root.left;
p.lbit = this .root.lbit;
p.rbit = 0;
p.right = this .root.right;
this .root.left = p;
this .root.lbit = 1;
return ;
}
let cur = this .root.left;
while ( true ) {
if (cur.value < data) {
const p = new Node();
p.value = data;
if (cur.rbit === 0) {
p.right = cur.right;
p.rbit = cur.rbit;
p.lbit = 0;
p.left = cur;
cur.rbit = 1;
cur.right = p;
return ;
} else {
cur = cur.right;
}
}
if (cur.value > data) {
const p = new Node();
p.value = data;
if (cur.lbit === 0) {
p.left = cur.left;
p.lbit = cur.lbit;
p.rbit = 0;
p.right = cur;
cur.lbit = 1;
cur.left = p;
return ;
} else {
cur = cur.left;
}
}
}
}
preorder() {
let c = this .root.left;
while (c != this .root) {
process.stdout.write(c.value + " " );
c = this .preorderSuccessor(c);
}
}
preorderSuccessor(c) {
if (c.lbit == 1) {
return c.left;
}
while (c.rbit == 0) {
c = c.right;
}
return c.right;
}
inorder() {
let c = this .root.left;
while (c.lbit == 1) {
c = c.left;
}
while (c != this .root) {
process.stdout.write(c.value + " " );
c = this .inorderSuccessor(c);
}
}
inorderSuccessor(c) {
if (c.rbit == 0) {
return c.right;
} else {
c = c.right;
}
while (c.lbit == 1) {
c = c.left;
}
return c;
}
}
let t1 = new DTBT();
t1.create();
console.log( "Inorder Traversal of DTBST" );
t1.inorder();
console.log( "\nPreorder Traversal of DTBST" );
t1.preorder();
|
OutputInorder Traversal of DTBST
1 3 5 6 7 8 9 11 13
Preorder Traversal of DTBST
6 3 1 5 8 7 11 9 13