Tutorialspoint.dev

Threaded Binary Search Tree | Deletion

A threaded binary tree node looks like following.

struct Node
{
    struct Node *left, *right;
    int info;
  
    // True if left pointer points to predecessor
    // in Inorder Traversal
    bool lthread;
  
    // True if right pointer points to predecessor
    // in Inorder Traversal
    bool rthread;
};

We have already discussed Insertion of Threaded Binary Search Tree
In deletion, first the key to be deleted is searched, and then there are differenct 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 ");
  
    // 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;
}

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.

// 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;
}

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->left = 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 prredecessor.

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;
}

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;
}

Below is Complete code:

C++

// Complete C++ program to demonstrate deletion
// in threaded BST
#include<bits/stdc++.h>
using namespace std;
  
struct Node
{
    struct Node *left, *right;
    int info;
  
    // True if left pointer points to predecessor
    // in Inorder Traversal
    bool lthread;
  
    // True 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 ! ");
            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->right)
        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->right;
  
    ptr = ptr->left;
    while (ptr->rthread);
        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->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;
}
  
// 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 ");
  
    // 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;
}

Java

// 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 ! "); 
            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.right!=null
        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.right; 
    
    ptr = ptr.left; 
    while (ptr.rthread); 
        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.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; 
    
// 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 "); 
    
    // 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); 
    
}
//contributed by Arnab Kundu


Output :

10 13 14 16 17 5 30 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter