Open In App

Deletion in a Binary Tree

Last Updated : 22 Oct, 2024
Comments
Improve
Suggest changes
195 Likes
Like
Report

Given a binary tree, the task is to delete a given node from it by making sure that the tree shrinks from the bottom (i.e. the deleted node is replaced by the bottom-most and rightmost node). This is different from BST deletion. Here we do not have any order among elements, so we replace them with the last element.

Examples :

Input : key = 10

deletion-in-a-binary-tree

Output:    

deletion-in-a-binary-tree-3


Explanation: As the bottom & rightmost node in the above binary tree is 30 , replace the key node ie. 10 with 30 and remove the bottom & rightmost node.

Input : key = 20

deletion-in-a-binary-tree-2

Output:  

deletion-in-a-binary-tree-4
Explanation: As the bottom & rightmost node in the above binary tree is 40, replace the key node ie. 20 with 40 and remove the bottom & rightmost node.

Approach:

The idea is to traverse the tree in level order manner. To perform the Deletion in a Binary Tree follow below:

  • Starting at the root, find the deepest and rightmost node in the binary tree and the node which we want to delete
  • Replace the deepest rightmost node’s data with the node to be deleted. 
  • Then delete the deepest rightmost node.

Below is the illustration of above approach:

deletion-in-a-binary-tree-5


Below is the implementation of the above approach: 

C++
// C++ program to delete a specific element
// in a binary tree
#include <bits/stdc++.h>
using namespace std;

class Node {
public:    
  	int data;
    Node *left, *right;
	Node(int x){
    	data = x;
      	left = right = nullptr;
    }
};

// Function to delete the deepest node in binary tree
void deletDeepest(Node* root, Node* dNode) {
    queue<Node*> q;
    q.push(root);

    Node* curr;
    while (!q.empty()) {
        curr = q.front();
        q.pop();

        // If current node is the deepest
      	// node, delete it
        if (curr == dNode) {  
            curr = nullptr;
            delete (dNode);
            return;
        }

        // Check the right child first
        if (curr->right) {
          
            // If right child is the deepest
          	// node, delete it
            if (curr->right == dNode) {
                curr->right = nullptr;
                delete (dNode);
                return;
            } 
            q.push(curr->right);
        }

        // Check the left child
        if (curr->left) {
          
            // If left child is the deepest 
          	// node, delete it
            if (curr->left == dNode) {
                curr->left = nullptr;
                delete (dNode);
                return;
            } 
            q.push(curr->left);
        }
    }
}

// Function to delete the node with the given key
Node* deletion(Node* root, int key) {
  
    // If the tree is empty, return null
    if (root == nullptr)
        return nullptr;

    // If the tree has only one node
    if (root->left == nullptr && root->right == nullptr) {
      
        // If the root node is the key, delete it
        if (root->data == key)
            return nullptr;
        else
            return root;
    }

    queue<Node*> q;
    q.push(root);

    Node* curr;
    Node* keyNode = nullptr;

    // Level order traversal to find the deepest
  	// node and the key node
    while (!q.empty()) {
        curr = q.front();
        q.pop();

        // If current node is the key node
        if (curr->data == key)
            keyNode = curr;

        if (curr->left)
            q.push(curr->left);

        if (curr->right)
            q.push(curr->right);
    }

    // If key node is found, replace its data
  	// with the deepest node
    if (keyNode != nullptr) {
      
      	// Store the data of the deepest node
        int x = curr->data;  
      
      	// Replace key node data with deepest
      	// node's data
        keyNode->data = x;  
      
      	// Delete the deepest node
        deletDeepest(root, curr);  
    }
    return root;
}

// Inorder traversal of a binary tree
void inorder(Node* curr) {
    if (!curr)
        return;
    inorder(curr->left);        
    cout << curr->data << " ";  
    inorder(curr->right);     
}

int main() {
  
    // Construct the binary tree
    //       10         
  	//      /  \       
  	//    11    9
  	//   / \   / \     
  	//  7  12 15  8     

    Node* root = new Node(10);
    root->left = new Node(11);
    root->right = new Node(9);
    root->left->left = new Node(7);
    root->left->right = new Node(12);
    root->right->left = new Node(15);
    root->right->right = new Node(8);

    int key = 11;
    root = deletion(root, key); 

    inorder(root);
    return 0;
}
Java Python C# JavaScript

Output
7 8 12 10 15 9 

Time complexity: O(n), where n is number of nodes.
Auxiliary Space: O(n)

Note: We can also replace the node's data that is to be deleted with any node whose left and right child points to NULL but we only use deepest node in order to maintain the Balance of a binary tree.


Article Tags :

Explore