A threaded binary tree node looks like following.
struct Node {
struct Node *left, *right;
int info;
// false if left pointer points to predecessor
// in Inorder Traversal
bool lthread;
// false if right pointer points to predecessor
// in Inorder Traversal
bool rthread;
}; |
static class Node {
Node left, right;
int info;
// True if left pointer points to predecessor
// in Inorder Traversal
boolean lthread;
// True if right pointer points to predecessor
// in Inorder Traversal
boolean rthread;
}; // This code contributed by aashish1995 |
class Node:
def __init__( self ):
self .info = 0 ;
self .left = None ;
self .right = None ;
# True if left pointer points to predecessor
# in Inorder Traversal
self .lthread = False ;;
# True if right pointer points to predecessor
# in Inorder Traversal
self .rthread = False ;
# This code contributed by umadevi9616 |
public class Node
{
public
Node left,
right;
public
int info;
// True if left pointer points to predecessor
// in Inorder Traversal
public
bool lthread;
// True if right pointer points to predecessor
// in Inorder Traversal
public
bool rthread;
};
// This code is contributed by aashish1995 |
<script> class Node {
constructor(){
this .left = null , this .right = null ;
this .info = 0;
// True if left pointer points to predecessor
// in Inorder Traversal
this .lthread = false ;
// True if right pointer points to predecessor
// in Inorder Traversal
this .rthread = false ;
} } // This code contributed by aashish1995 </script> |
We have already discussed Insertion of Threaded Binary Search Tree
In deletion, first the key to be deleted is searched, and then there are different cases for deleting the Node in which key is found.
// Deletes a key from threaded BST with given root and // returns new root of BST. struct Node* delThreadedBST( struct Node* root, int dkey)
{ // Initialize parent as NULL and ptrent
// Node as root.
struct Node *par = NULL, *ptr = root;
// Set true if key is found
int found = 0;
// Search key in BST : find Node and its
// parent.
while (ptr != NULL) {
if (dkey == ptr->info) {
found = 1;
break ;
}
par = ptr;
if (dkey < ptr->info) {
if (ptr->lthread == false )
ptr = ptr->left;
else
break ;
}
else {
if (ptr->rthread == false )
ptr = ptr->right;
else
break ;
}
}
if (found == 0)
printf ( "dkey not present in tree\n" );
// Two Children
else if (ptr->lthread == false && ptr->rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr->lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr->rthread == false )
root = caseB(root, par, ptr);
// No child
else
root = caseA(root, par, ptr);
return root;
} |
// Deletes a key from threaded BST with given root and // returns new root of BST. Node delThreadedBST(Node root, int dkey)
{ // Initialize parent as null and ptrent
// Node as root.
Node par = null , ptr = root;
// Set true if key is found
int found = 0 ;
// Search key in BST : find Node and its
// parent.
while (ptr != null ) {
if (dkey == ptr.info) {
found = 1 ;
break ;
}
par = ptr;
if (dkey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
if (found == 0 )
System.out.printf( "dkey not present in tree\n" );
// Two Children
else if (ptr.lthread == false && ptr.rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr.lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr.rthread == false )
root = caseB(root, par, ptr);
// No child
else
root = caseA(root, par, ptr);
return root;
} // This code is contributed by gauravrajput1 |
# Deletes a key from threaded BST with given root and # returns new root of BST. def delThreadedBST(root, dkey):
# Initialize parent as None and ptrent
# Node as root.
par = None ;
ptr = root;
# Set True if key is found
found = 0 ;
# Search key in BST : find Node and its
# parent.
while (ptr ! = None ):
if (dkey = = ptr.info):
found = 1 ;
break ;
par = ptr;
if (dkey < ptr.info):
if (ptr.lthread = = False )
ptr = ptr.left;
else
break ;
else :
if (ptr.rthread = = False )
ptr = ptr.right;
else
break ;
if (found = = 0 ):
print ( "dkey not present in tree" );
# Two Children
else if (ptr.lthread = = False and ptr.rthread = = False ):
root = caseC(root, par, ptr);
# Only Left Child
else if (ptr.lthread = = False ):
root = caseB(root, par, ptr);
# Only Right Child
else if (ptr.rthread = = False ):
root = caseB(root, par, ptr);
# No child
else :
root = caseA(root, par, ptr);
return root;
# This code is contributed by Rajput-Ji |
// Deletes a key from threaded BST with given root and // returns new root of BST. Node delThreadedBST(Node root, int dkey)
{ // Initialize parent as null and ptrent
// Node as root.
Node par = null , ptr = root;
// Set true if key is found
int found = 0;
// Search key in BST : find Node and its
// parent.
while (ptr != null ) {
if (dkey == ptr.info) {
found = 1;
break ;
}
par = ptr;
if (dkey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
if (found == 0)
Console.Write( "dkey not present in tree\n" );
// Two Children
else if (ptr.lthread == false && ptr.rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr.lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr.rthread == false )
root = caseB(root, par, ptr);
// No child
else
root = caseA(root, par, ptr);
return root;
} // This code is contributed by gauravrajput1 |
<script> // Deletes a key from threaded BST with given root and // returns new root of BST. function delThreadedBST(root , dkey)
{ // Initialize parent as null and ptrent
// Node as root.
var par = null , ptr = root;
// Set true if key is found
var found = 0;
// Search key in BST : find Node and its
// parent.
while (ptr != null ) {
if (dkey == ptr.info) {
found = 1;
break ;
}
par = ptr;
if (dkey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
if (found == 0)
document.write( "dkey not present in tree\n" );
// Two Children
else if (ptr.lthread == false && ptr.rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr.lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr.rthread == false )
root = caseB(root, par, ptr);
// No child
else
root = caseA(root, par, ptr);
return root;
} // This code is contributed by gauravrajput1 </script> |
Case A: Leaf Node need to be deleted
In BST, for deleting a leaf Node the left or right pointer of parent was set to NULL. Here instead of setting the pointer to NULL it is made a thread.
If the leaf Node is to be deleted is left child of its parent then after deletion, left pointer of parent should become a thread pointing to its predecessor of the parent Node after deletion.
par -> lthread = true; par -> left = ptr -> left;
If the leaf Node to be deleted is right child of its parent then after deletion, right pointer of parent should become a thread pointing to its successor. The Node which was inorder successor of the leaf Node before deletion will become the inorder successor of the parent Node after deletion.
par -> rthread = true; par -> right = ptr -> right;
// Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. struct Node* caseA( struct Node* root, struct Node* par,
struct Node* ptr)
{ // If Node to be deleted is root
if (par == NULL)
root = NULL;
// If Node to be deleted is left
// of its parent
else if (ptr == par->left) {
par->lthread = true ;
par->left = ptr->left;
}
else {
par->rthread = true ;
par->right = ptr->right;
}
// Free memory and return new root
free (ptr);
return root;
} |
// Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. Node caseA(Node root, Node par, Node ptr)
{ // If Node to be deleted is root
if (par == null )
root = null ;
// If Node to be deleted is left
// of its parent
else if (ptr == par.left) {
par.lthread = true ;
par.left = ptr.left;
}
else {
par.rthread = true ;
par.right = ptr.right;
}
return root;
} // This code is contributed by gauravrajput1 |
# Here 'par' is pointer to parent Node and 'ptr' is # pointer to current Node. def caseA(root,par,ptr):
# If Node to be deleted is root
if (par = = None ):
root = None
# If Node to be deleted is left
# of its parent
elif (ptr = = par.left):
par.lthread = true
par.left = ptr.left
else :
par.rthread = true
par.right = ptr.right
return root
# This code is contributed by Patel2127. |
// Here 'par' is pointer to parent Node and // 'ptr' is pointer to current Node. Node caseA(Node root, Node par, Node ptr) { // If Node to be deleted is root
if (par == null )
root = null ;
// If Node to be deleted is left
// of its parent
else if (ptr == par.left)
{
par.lthread = true ;
par.left = ptr.left;
}
else
{
par.rthread = true ;
par.right = ptr.right;
}
return root;
} // This code is contributed by rutvik_56 |
<script> // Here 'par' is pointer to parent Node and // 'ptr' is pointer to current Node. function caseA(root, par, ptr)
{ // If Node to be deleted is root
if (par == null )
root = null ;
// If Node to be deleted is left
// of its parent
else if (ptr == par.left)
{
par.lthread = true ;
par.left = ptr.left;
}
else
{
par.rthread = true ;
par.right = ptr.right;
}
return root;
} // This code is contributed by rag2127 </script> |
Case B: Node to be deleted has only one child
After deleting the Node as in a BST, the inorder successor and inorder predecessor of the Node are found out.
s = inSucc(ptr); p = inPred(ptr);
If Node to be deleted has left subtree, then after deletion right thread of its predecessor should point to its successor.
p->right = s;
Before deletion 15 is predecessor and 2 is successor of 16. After deletion of 16, the Node 20 becomes the successor of 15, so right thread of 15 will point to 20.
If Node to be deleted has right subtree, then after deletion left thread of its successor should point to its predecessor.
s->left = p;
Before deletion of 25 is predecessor and 34 is successor of 30. After deletion of 30, the Node 25 becomes the predecessor of 34, so left thread of 34 will point to 25.
// Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. struct Node* caseB( struct Node* root, struct Node* par,
struct Node* ptr)
{ struct Node* child;
// Initialize child Node to be deleted has
// left child.
if (ptr->lthread == false )
child = ptr->left;
// Node to be deleted has right child.
else
child = ptr->right;
// Node to be deleted is root Node.
if (par == NULL)
root = child;
// Node is left child of its parent.
else if (ptr == par->left)
par->left = child;
else
par->right = child;
// Find successor and predecessor
Node* s = inSucc(ptr);
Node* p = inPred(ptr);
// If ptr has left subtree.
if (ptr->lthread == false )
p->right = s;
// If ptr has right subtree.
else {
if (ptr->rthread == false )
s->left = p;
}
free (ptr);
return root;
} |
// Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. static Node caseB(Node root, Node par,
Node ptr)
{ Node child;
// Initialize child Node to be deleted has
// left child.
if (ptr.lthread == false )
child = ptr.left;
// Node to be deleted has right child.
else
child = ptr.right;
// Node to be deleted is root Node.
if (par == null )
root = child;
// Node is left child of its parent.
else if (ptr == par.left)
par.left = child;
else
par.right = child;
// Find successor and predecessor
Node s = inSucc(ptr);
Node p = inPred(ptr);
// If ptr has left subtree.
if (ptr.lthread == false )
p.right = s;
// If ptr has right subtree.
else {
if (ptr.rthread == false )
s.left = p;
}
return root;
} // This code is contributed by gauravrajput1 |
# Here 'par' is pointer to parent Node and 'ptr' is # pointer to current Node. def caseB(root, par, ptr):
child = None ;
# Initialize child Node to be deleted has
# left child.
if (ptr.lthread = = False ):
child = ptr.left;
# Node to be deleted has right child.
else :
child = ptr.right;
# Node to be deleted is root Node.
if (par = = None ):
root = child;
# Node is left child of its parent.
elif (ptr = = par.left):
par.left = child;
else :
par.right = child;
# Find successor and predecessor
s = inSucc(ptr);
p = inPred(ptr);
# If ptr has left subtree.
if (ptr.lthread = = False ):
p.right = s;
# If ptr has right subtree.
else :
if (ptr.rthread = = False ):
s.left = p;
return root;
# This code is contributed by umadevi9616 |
// Here 'par' is pointer to parent Node and // 'ptr' is pointer to current Node. static Node caseB(Node root, Node par,
Node ptr)
{ Node child;
// Initialize child Node to be deleted
// has left child.
if (ptr.lthread == false )
child = ptr.left;
// Node to be deleted has right child.
else
child = ptr.right;
// Node to be deleted is root Node.
if (par == null )
root = child;
// Node is left child of its parent.
else if (ptr == par.left)
par.left = child;
else
par.right = child;
// Find successor and predecessor
Node s = inSucc(ptr);
Node p = inPred(ptr);
// If ptr has left subtree.
if (ptr.lthread == false )
p.right = s;
// If ptr has right subtree.
else
{
if (ptr.rthread == false )
s.left = p;
}
return root;
} // This code is contributed by gauravrajput1 |
<script> // Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. function caseB(root,par,ptr)
{ let child;
// Initialize child Node to be deleted has
// left child.
if (ptr.lthread == false )
child = ptr.left;
// Node to be deleted has right child.
else
child = ptr.right;
// Node to be deleted is root Node.
if (par == null )
root = child;
// Node is left child of its parent.
else if (ptr == par.left)
par.left = child;
else
par.right = child;
// Find successor and predecessor
let s = inSucc(ptr);
let p = inPred(ptr);
// If ptr has left subtree.
if (ptr.lthread == false )
p.right = s;
// If ptr has right subtree.
else {
if (ptr.rthread == false )
s.left = p;
}
return root;
} // This code is contributed by avanitrachhadiya2155 </script> |
Case C: Node to be deleted has two children
We find inorder successor of Node ptr (Node to be deleted) and then copy the information of this successor into Node ptr. After this inorder successor Node is deleted using either Case A or Case B.
// Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. struct Node* caseC( struct Node* root, struct Node* par,
struct Node* ptr)
{ // Find inorder successor and its parent.
struct Node* parsucc = ptr;
struct Node* succ = ptr->right;
// Find leftmost child of successor
while (succ->left != NULL) {
parsucc = succ;
succ = succ->left;
}
ptr->info = succ->info;
if (succ->lthread == true && succ->rthread == true )
root = caseA(root, parsucc, succ);
else
root = caseB(root, parsucc, succ);
return root;
} |
// Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node.
static Node caseC(Node root, Node par,
Node ptr)
{
// Find inorder successor and its parent.
Node parsucc = ptr;
Node succ = ptr.right;
// Find leftmost child of successor
while (succ.lthread == false ) {
parsucc = succ;
succ = succ.left;
}
ptr.info = succ.info;
if (succ.lthread == true && succ.rthread == true )
root = caseA(root, parsucc, succ);
else
root = caseB(root, parsucc, succ);
return root;
}
// This code is contributed by umadevi9616 |
// Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node.
static Node caseC(Node root, Node par,
Node ptr)
{
// Find inorder successor and its parent.
Node parsucc = ptr;
Node succ = ptr.right;
// Find leftmost child of successor
while (succ.lthread == false ) {
parsucc = succ;
succ = succ.left;
}
ptr.info = succ.info;
if (succ.lthread == true && succ.rthread == true )
root = caseA(root, parsucc, succ);
else
root = caseB(root, parsucc, succ);
return root;
}
// This code is contributed by umadevi9616 |
<script> // Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
function caseC(root, par, ptr)
{
// Find inorder successor and its parent.
var parsucc = ptr;
var succ = ptr.right;
// Find leftmost child of successor
while (succ.lthread == false ) {
parsucc = succ;
succ = succ.left;
}
ptr.info = succ.info;
if (succ.lthread == true && succ.rthread == true )
root = caseA(root, parsucc, succ);
else root = caseB(root, parsucc, succ);
return root;
}
// This code is contributed by gauravrajput1
</script> |
# Here 'par' is pointer to parent Node and 'ptr' is # pointer to current Node. def caseC(root, par, ptr):
# Find inorder successor and its parent.
parsucc = ptr;
succ = ptr.right;
# Find leftmost child of successor
while (succ.lthread = = False ):
parsucc = succ;
succ = succ.left;
ptr.info = succ.info;
if (succ.lthread = = True and succ.rthread = = True ):
root = caseA(root, parsucc, succ);
else :
root = caseB(root, parsucc, succ);
return root;
# This code contributed by umadevi9616 |
Below is Complete code:
// Complete C++ program to demonstrate deletion // in threaded BST #include <bits/stdc++.h> using namespace std;
struct Node {
struct Node *left, *right;
int info;
// false if left pointer points to predecessor
// in Inorder Traversal
bool lthread;
// false if right pointer points to predecessor
// in Inorder Traversal
bool rthread;
}; // Insert a Node in Binary Threaded Tree struct Node* insert( struct Node* root, int ikey)
{ // Searching for a Node with given value
Node* ptr = root;
Node* par = NULL; // Parent of key to be inserted
while (ptr != NULL) {
// If key already exists, return
if (ikey == (ptr->info)) {
printf ( "Duplicate Key !\n" );
return root;
}
par = ptr; // Update parent pointer
// Moving on left subtree.
if (ikey < ptr->info) {
if (ptr->lthread == false )
ptr = ptr->left;
else
break ;
}
// Moving on right subtree.
else {
if (ptr->rthread == false )
ptr = ptr->right;
else
break ;
}
}
// Create a new Node
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;
} // Returns inorder successor using left // and right children (Used in deletion) struct Node* inSucc( struct Node* ptr)
{ if (ptr->rthread == true )
return ptr->right;
ptr = ptr->right;
while (ptr->lthread == false )
ptr = ptr->left;
return ptr;
} // Returns inorder successor using rthread // (Used in inorder) struct Node* inorderSuccessor( struct Node* ptr)
{ // If rthread is set, we can quickly find
if (ptr->rthread == true )
return ptr->right;
// Else return leftmost child of right subtree
ptr = ptr->right;
while (ptr->lthread == false )
ptr = ptr->left;
return ptr;
} // Printing the threaded tree void inorder( struct Node* root)
{ if (root == NULL)
printf ( "Tree is empty" );
// Reach leftmost Node
struct Node* ptr = root;
while (ptr->lthread == false )
ptr = ptr->left;
// One by one print successors
while (ptr != NULL) {
printf ( "%d " , ptr->info);
ptr = inorderSuccessor(ptr);
}
} struct Node* inPred( struct Node* ptr)
{ if (ptr->lthread == true )
return ptr->left;
ptr = ptr->left;
while (ptr->rthread == false )
ptr = ptr->right;
return ptr;
} // Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. struct Node* caseA( struct Node* root, struct Node* par,
struct Node* ptr)
{ // If Node to be deleted is root
if (par == NULL)
root = NULL;
// If Node to be deleted is left
// of its parent
else if (ptr == par->left) {
par->lthread = true ;
par->left = ptr->left;
}
else {
par->rthread = true ;
par->right = ptr->right;
}
// Free memory and return new root
free (ptr);
return root;
} // Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. struct Node* caseB( struct Node* root, struct Node* par,
struct Node* ptr)
{ struct Node* child;
// Initialize child Node to be deleted has
// left child.
if (ptr->lthread == false )
child = ptr->left;
// Node to be deleted has right child.
else
child = ptr->right;
// Node to be deleted is root Node.
if (par == NULL)
root = child;
// Node is left child of its parent.
else if (ptr == par->left)
par->left = child;
else
par->right = child;
// Find successor and predecessor
Node* s = inSucc(ptr);
Node* p = inPred(ptr);
// If ptr has left subtree.
if (ptr->lthread == false )
p->right = s;
// If ptr has right subtree.
else {
if (ptr->rthread == false )
s->left = p;
}
free (ptr);
return root;
} // Here 'par' is pointer to parent Node and 'ptr' is // pointer to current Node. struct Node* caseC( struct Node* root, struct Node* par,
struct Node* ptr)
{ // Find inorder successor and its parent.
struct Node* parsucc = ptr;
struct Node* succ = ptr->right;
// Find leftmost child of successor
while (succ->lthread== false ) {
parsucc = succ;
succ = succ->left;
}
ptr->info = succ->info;
if (succ->lthread == true && succ->rthread == true )
root = caseA(root, parsucc, succ);
else
root = caseB(root, parsucc, succ);
return root;
} // Deletes a key from threaded BST with given root and // returns new root of BST. struct Node* delThreadedBST( struct Node* root, int dkey)
{ // Initialize parent as NULL and ptrent
// Node as root.
struct Node *par = NULL, *ptr = root;
// Set true if key is found
int found = 0;
// Search key in BST : find Node and its
// parent.
while (ptr != NULL) {
if (dkey == ptr->info) {
found = 1;
break ;
}
par = ptr;
if (dkey < ptr->info) {
if (ptr->lthread == false )
ptr = ptr->left;
else
break ;
}
else {
if (ptr->rthread == false )
ptr = ptr->right;
else
break ;
}
}
if (found == 0)
printf ( "dkey not present in tree\n" );
// Two Children
else if (ptr->lthread == false && ptr->rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr->lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr->rthread == false )
root = caseB(root, par, ptr);
// No child
else
root = caseA(root, par, ptr);
return root;
} // Driver Program 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);
root = delThreadedBST(root, 20);
inorder(root);
return 0;
} |
// Complete Java program to demonstrate deletion // in threaded BST import java.util.*;
class solution {
static class Node {
Node left, right;
int info;
// True if left pointer points to predecessor
// in Inorder Traversal
boolean lthread;
// True if right pointer points to predecessor
// in Inorder Traversal
boolean rthread;
};
// Insert a Node in Binary Threaded Tree
static Node insert(Node root, int ikey)
{
// Searching for a Node with given value
Node ptr = root;
Node par = null ; // Parent of key to be inserted
while (ptr != null ) {
// If key already exists, return
if (ikey == (ptr.info)) {
System.out.printf( "Duplicate Key !\n" );
return root;
}
par = ptr; // Update parent pointer
// Moving on left subtree.
if (ikey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
// Moving on right subtree.
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
// Create a new Node
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;
}
// Returns inorder successor using left
// and right children (Used in deletion)
static Node inSucc(Node ptr)
{
if (ptr.rthread == true )
return ptr.right;
ptr = ptr.right;
while (ptr.lthread == false )
ptr = ptr.left;
return ptr;
}
// Returns inorder successor using rthread
// (Used in inorder)
static Node inorderSuccessor(Node ptr)
{
// If rthread is set, we can quickly find
if (ptr.rthread == true )
return ptr.right;
// Else return leftmost child of right subtree
ptr = ptr.right;
while (ptr.lthread == false )
ptr = ptr.left;
return ptr;
}
// Printing the threaded tree
static void inorder(Node root)
{
if (root == null )
System.out.printf( "Tree is empty" );
// Reach leftmost Node
Node ptr = root;
while (ptr.lthread == false )
ptr = ptr.left;
// One by one print successors
while (ptr != null ) {
System.out.printf( "%d " , ptr.info);
ptr = inorderSuccessor(ptr);
}
}
static Node inPred(Node ptr)
{
if (ptr.lthread == true )
return ptr.left;
ptr = ptr.left;
while (ptr.rthread == false )
ptr = ptr.right;
return ptr;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
static Node caseA(Node root, Node par,
Node ptr)
{
// If Node to be deleted is root
if (par == null )
root = null ;
// If Node to be deleted is left
// of its parent
else if (ptr == par.left) {
par.lthread = true ;
par.left = ptr.left;
}
else {
par.rthread = true ;
par.right = ptr.right;
}
return root;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
static Node caseB(Node root, Node par,
Node ptr)
{
Node child;
// Initialize child Node to be deleted has
// left child.
if (ptr.lthread == false )
child = ptr.left;
// Node to be deleted has right child.
else
child = ptr.right;
// Node to be deleted is root Node.
if (par == null )
root = child;
// Node is left child of its parent.
else if (ptr == par.left)
par.left = child;
else
par.right = child;
// Find successor and predecessor
Node s = inSucc(ptr);
Node p = inPred(ptr);
// If ptr has left subtree.
if (ptr.lthread == false )
p.right = s;
// If ptr has right subtree.
else {
if (ptr.rthread == false )
s.left = p;
}
return root;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
static Node caseC(Node root, Node par,
Node ptr)
{
// Find inorder successor and its parent.
Node parsucc = ptr;
Node succ = ptr.right;
// Find leftmost child of successor
while (succ.lthread == false ) {
parsucc = succ;
succ = succ.left;
}
ptr.info = succ.info;
if (succ.lthread == true && succ.rthread == true )
root = caseA(root, parsucc, succ);
else
root = caseB(root, parsucc, succ);
return root;
}
// Deletes a key from threaded BST with given root and
// returns new root of BST.
static Node delThreadedBST(Node root, int dkey)
{
// Initialize parent as null and ptrent
// Node as root.
Node par = null , ptr = root;
// Set true if key is found
int found = 0 ;
// Search key in BST : find Node and its
// parent.
while (ptr != null ) {
if (dkey == ptr.info) {
found = 1 ;
break ;
}
par = ptr;
if (dkey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
if (found == 0 )
System.out.printf( "dkey not present in tree\n" );
// Two Children
else if (ptr.lthread == false && ptr.rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr.lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr.rthread == false )
root = caseB(root, par, ptr);
// No child
else
root = caseA(root, par, ptr);
return root;
}
// Driver Program
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 );
root = delThreadedBST(root, 20 );
inorder(root);
}
} // This code is contributed by Arnab Kundu |
# Complete Python program to demonstrate deletion # in threaded BST class Node:
def __init__( self ):
self .info = 0 ;
self .left = None ;
self .right = None ;
# True if left pointer points to predecessor
# in Inorder Traversal
self .lthread = False ;
# True if right pointer points to predecessor
# in Inorder Traversal
self .rthread = False ;
# Insert a Node in Binary Threaded Tree def insert(root, ikey):
# Searching for a Node with given value
ptr = root;
par = None ; # Parent of key to be inserted
while (ptr ! = None ):
# If key already exists, return
if (ikey = = (ptr.info)):
print ( "Duplicate Key !" );
return root;
par = ptr; # Update parent pointer
# Moving on left subtree.
if (ikey < ptr.info):
if (ptr.lthread = = False ):
ptr = ptr.left;
else :
break ;
# Moving on right subtree.
else :
if (ptr.rthread = = False ):
ptr = ptr.right;
else :
break ;
# Create a new Node
tmp = Node();
tmp.info = ikey;
tmp.lthread = True ;
tmp.rthread = True ;
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;
# Returns inorder successor using left # and right children (Used in deletion) def inSucc(ptr):
if (ptr.rthread = = True ):
return ptr.right;
ptr = ptr.right;
while (ptr.lthread = = False ):
ptr = ptr.left;
return ptr;
# Returns inorder successor using rthread # (Used in inorder) def inorderSuccessor(ptr):
# If rthread is set, we can quickly find
if (ptr.rthread = = True ):
return ptr.right;
# Else return leftmost child of right subtree
ptr = ptr.right;
while (ptr.lthread = = False ):
ptr = ptr.left;
return ptr;
# Printing the threaded tree def inorder(root):
if (root = = None ):
print ( "Tree is empty" );
# Reach leftmost Node
ptr = root;
while (ptr.lthread = = False ):
ptr = ptr.left;
# One by one print successors
while (ptr ! = None ):
print ( ptr.info, end = " " );
ptr = inorderSuccessor(ptr);
def inPred(ptr):
if (ptr.lthread = = True ):
return ptr.left;
ptr = ptr.left;
while (ptr.rthread = = False ):
ptr = ptr.right;
return ptr;
# Here 'par' is pointer to parent Node and 'ptr' is # pointer to current Node. def caseA(root, par, ptr):
# If Node to be deleted is root
if (par = = None ):
root = None ;
# If Node to be deleted is left
# of its parent
elif (ptr = = par.left):
par.lthread = True ;
par.left = ptr.left;
else :
par.rthread = True ;
par.right = ptr.right;
return root;
# Here 'par' is pointer to parent Node and 'ptr' is # pointer to current Node. def caseB(root, par, ptr):
child;
# Initialize child Node to be deleted has
# left child.
if (ptr.lthread = = False ):
child = ptr.left;
# Node to be deleted has right child.
else :
child = ptr.right;
# Node to be deleted is root Node.
if (par = = None ):
root = child;
# Node is left child of its parent.
elif (ptr = = par.left):
par.left = child;
else :
par.right = child;
# Find successor and predecessor
s = inSucc(ptr);
p = inPred(ptr);
# If ptr has left subtree.
if (ptr.lthread = = False ):
p.right = s;
# If ptr has right subtree.
else :
if (ptr.rthread = = False ):
s.left = p;
return root;
# Here 'par' is pointer to parent Node and 'ptr' is # pointer to current Node. def caseC(root, par, ptr):
# Find inorder successor and its parent.
parsucc = ptr;
succ = ptr.right;
# Find leftmost child of successor
while (succ.lthread = = False ):
parsucc = succ;
succ = succ.left;
ptr.info = succ.info;
if (succ.lthread = = True and succ.rthread = = True ):
root = caseA(root, parsucc, succ);
else :
root = caseB(root, parsucc, succ);
return root;
# Deletes a key from threaded BST with given root and # returns new root of BST. def delThreadedBST(root, dkey):
# Initialize parent as None and ptrent
# Node as root.
par = None ;
ptr = root;
# Set True if key is found
found = 0 ;
# Search key in BST : find Node and its
# parent.
while (ptr ! = None ):
if (dkey = = ptr.info):
found = 1 ;
break ;
par = ptr;
if (dkey < ptr.info):
if (ptr.lthread = = False ):
ptr = ptr.left;
else :
break ;
else :
if (ptr.rthread = = False ):
ptr = ptr.right;
else :
break ;
if (found = = 0 ):
print ( "dkey not present in tree" );
# Two Children
elif (ptr.lthread = = False and ptr.rthread = = False ):
root = caseC(root, par, ptr);
# Only Left Child
elif (ptr.lthread = = False ):
root = caseB(root, par, ptr);
# Only Right Child
elif (ptr.rthread = = False ):
root = caseB(root, par, ptr);
# No child
else :
root = caseA(root, par, ptr);
return root;
# Driver Program 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 );
root = delThreadedBST(root, 20 );
inorder(root);
# This code is contributed by Rajput-Ji |
// Complete C# program to demonstrate deletion // in threaded BST using System;
class GFG {
public class Node {
public Node left, right;
public int info;
// True if left pointer points to predecessor
// in Inorder Traversal
public bool lthread;
// True if right pointer points to predecessor
// in Inorder Traversal
public bool rthread;
};
// Insert a Node in Binary Threaded Tree
static Node insert(Node root, int ikey)
{
// Searching for a Node with given value
Node ptr = root;
Node par = null ; // Parent of key to be inserted
while (ptr != null ) {
// If key already exists, return
if (ikey == (ptr.info)) {
Console.Write( "Duplicate Key !\n" );
return root;
}
par = ptr; // Update parent pointer
// Moving on left subtree.
if (ikey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
// Moving on right subtree.
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
// Create a new Node
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;
}
// Returns inorder successor using left
// and right children (Used in deletion)
static Node inSucc(Node ptr)
{
if (ptr.rthread == true )
return ptr.right;
ptr = ptr.right;
while (ptr.lthread == false )
ptr = ptr.left;
return ptr;
}
// Returns inorder successor using rthread
// (Used in inorder)
static Node inorderSuccessor(Node ptr)
{
// If rthread is set, we can quickly find
if (ptr.rthread == true )
return ptr.right;
// Else return leftmost child of right subtree
ptr = ptr.right;
while (ptr.lthread == false )
ptr = ptr.left;
return ptr;
}
// Printing the threaded tree
static void inorder(Node root)
{
if (root == null )
Console.Write( "Tree is empty" );
// Reach leftmost Node
Node ptr = root;
while (ptr.lthread == false )
ptr = ptr.left;
// One by one print successors
while (ptr != null ) {
Console.Write( "{0} " , ptr.info);
ptr = inorderSuccessor(ptr);
}
}
static Node inPred(Node ptr)
{
if (ptr.lthread == true )
return ptr.left;
ptr = ptr.left;
while (ptr.rthread == false )
ptr = ptr.right;
return ptr;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
static Node caseA(Node root, Node par,
Node ptr)
{
// If Node to be deleted is root
if (par == null )
root = null ;
// If Node to be deleted is left
// of its parent
else if (ptr == par.left) {
par.lthread = true ;
par.left = ptr.left;
}
else {
par.rthread = true ;
par.right = ptr.right;
}
return root;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
static Node caseB(Node root, Node par,
Node ptr)
{
Node child;
// Initialize child Node to be deleted has
// left child.
if (ptr.lthread == false )
child = ptr.left;
// Node to be deleted has right child.
else
child = ptr.right;
// Node to be deleted is root Node.
if (par == null )
root = child;
// Node is left child of its parent.
else if (ptr == par.left)
par.left = child;
else
par.right = child;
// Find successor and predecessor
Node s = inSucc(ptr);
Node p = inPred(ptr);
// If ptr has left subtree.
if (ptr.lthread == false )
p.right = s;
// If ptr has right subtree.
else {
if (ptr.rthread == false )
s.left = p;
}
return root;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
static Node caseC(Node root, Node par,
Node ptr)
{
// Find inorder successor and its parent.
Node parsucc = ptr;
Node succ = ptr.right;
// Find leftmost child of successor
while (succ.lthread == false ) {
parsucc = succ;
succ = succ.left;
}
ptr.info = succ.info;
if (succ.lthread == true && succ.rthread == true )
root = caseA(root, parsucc, succ);
else
root = caseB(root, parsucc, succ);
return root;
}
// Deletes a key from threaded BST with given root and
// returns new root of BST.
static Node delThreadedBST(Node root, int dkey)
{
// Initialize parent as null and ptrent
// Node as root.
Node par = null , ptr = root;
// Set true if key is found
int found = 0;
// Search key in BST : find Node and its
// parent.
while (ptr != null ) {
if (dkey == ptr.info) {
found = 1;
break ;
}
par = ptr;
if (dkey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else
break ;
}
else {
if (ptr.rthread == false )
ptr = ptr.right;
else
break ;
}
}
if (found == 0)
Console.Write( "dkey not present in tree\n" );
// Two Children
else if (ptr.lthread == false && ptr.rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr.lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr.rthread == false )
root = caseB(root, par, ptr);
// No child
else
root = caseA(root, par, ptr);
return root;
}
// Driver code
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);
root = delThreadedBST(root, 20);
inorder(root);
}
} // This code has been contributed by 29AjayKumar |
<script> // Complete JavaScript program to demonstrate deletion
// in threaded BST
class Node {
constructor() {
this .info = 0;
// True if left pointer points to predecessor
// in Inorder Traversal
this .lthread = false ;
// True if right pointer points to predecessor
// in Inorder Traversal
this .rthread = false ;
this .left = null ;
this .right = null ;
}
}
// Insert a Node in Binary Threaded Tree
function insert(root, ikey) {
// Searching for a Node with given value
var ptr = root;
var par = null ; // Parent of key to be inserted
while (ptr != null ) {
// If key already exists, return
if (ikey == ptr.info) {
document.write( "Duplicate Key !<br>" );
return root;
}
par = ptr; // Update parent pointer
// Moving on left subtree.
if (ikey < ptr.info) {
if (ptr.lthread == false ) ptr = ptr.left;
else break ;
}
// Moving on right subtree.
else {
if (ptr.rthread == false ) ptr = ptr.right;
else break ;
}
}
// Create a new Node
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;
}
// Returns inorder successor using left
// and right children (Used in deletion)
function inSucc(ptr) {
if (ptr.rthread == true ) return ptr.right;
ptr = ptr.right;
while (ptr.lthread == false ) ptr = ptr.left;
return ptr;
}
// Returns inorder successor using rthread
// (Used in inorder)
function inorderSuccessor(ptr) {
// If rthread is set, we can quickly find
if (ptr.rthread == true ) return ptr.right;
// Else return leftmost child of right subtree
ptr = ptr.right;
while (ptr.lthread == false ) ptr = ptr.left;
return ptr;
}
// Printing the threaded tree
function inorder(root) {
if (root == null ) document.write( "Tree is empty" );
// Reach leftmost Node
var ptr = root;
while (ptr.lthread == false ) ptr = ptr.left;
// One by one print successors
while (ptr != null ) {
document.write(ptr.info + " " );
ptr = inorderSuccessor(ptr);
}
}
function inPred(ptr) {
if (ptr.lthread == true ) return ptr.left;
ptr = ptr.left;
while (ptr.rthread == false ) ptr = ptr.right;
return ptr;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
function caseA(root, par, ptr) {
// If Node to be deleted is root
if (par == null ) root = null ;
// If Node to be deleted is left
// of its parent
else if (ptr == par.left) {
par.lthread = true ;
par.left = ptr.left;
} else {
par.rthread = true ;
par.right = ptr.right;
}
return root;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
function caseB(root, par, ptr) {
var child;
// Initialize child Node to be deleted has
// left child.
if (ptr.lthread == false ) child = ptr.left;
// Node to be deleted has right child.
else child = ptr.right;
// Node to be deleted is root Node.
if (par == null ) root = child;
// Node is left child of its parent.
else if (ptr == par.left) par.left = child;
else par.right = child;
// Find successor and predecessor
var s = inSucc(ptr);
var p = inPred(ptr);
// If ptr has left subtree.
if (ptr.lthread == false ) p.right = s;
// If ptr has right subtree.
else {
if (ptr.rthread == false ) s.left = p;
}
return root;
}
// Here 'par' is pointer to parent Node and 'ptr' is
// pointer to current Node.
function caseC(root, par, ptr) {
// Find inorder successor and its parent.
var parsucc = ptr;
var succ = ptr.right;
// Find leftmost child of successor
while (succ.lthread == false ) {
parsucc = succ;
succ = succ.left;
}
ptr.info = succ.info;
if (succ.lthread == true && succ.rthread == true )
root = caseA(root, parsucc, succ);
else root = caseB(root, parsucc, succ);
return root;
}
// Deletes a key from threaded BST with given root and
// returns new root of BST.
function delThreadedBST(root, dkey) {
// Initialize parent as null and ptrent
// Node as root.
var par = null ,
ptr = root;
// Set true if key is found
var found = 0;
// Search key in BST : find Node and its
// parent.
while (ptr != null ) {
if (dkey == ptr.info) {
found = 1;
break ;
}
par = ptr;
if (dkey < ptr.info) {
if (ptr.lthread == false )
ptr = ptr.left;
else break ;
} else {
if (ptr.rthread == false )
ptr = ptr.right;
else break ;
}
}
if (found == 0)
document.write( "dkey not present in tree<br>" );
// Two Children
else if (ptr.lthread == false && ptr.rthread == false )
root = caseC(root, par, ptr);
// Only Left Child
else if (ptr.lthread == false )
root = caseB(root, par, ptr);
// Only Right Child
else if (ptr.rthread == false )
root = caseB(root, par, ptr);
// No child
else root = caseA(root, par, ptr);
return root;
}
// Driver code
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);
root = delThreadedBST(root, 20);
inorder(root);
</script> |
5 10 13 14 16 17 30
Time Complexity: The time complexity of the operations are –
Operation Name | Time Complexity |
---|---|
Insertion | O(1) |
Deletion | O(1) |
Auxiliary Space Complexity: The auxiliary space complexity of every operation is O(1). As it doesn’t use recursion or stack