Getting started
Sorting
Bubble sort
Algorithm complexity
Graphs
Dynamic programming
Kruskals algorithm
Greedy algorithms
Searching
Big O notation
Bellman-For Algorithm
Merge Sort
Binary Search Trees
Binary Search Trees
Binary Search Tree - Insertion (Python)
Binary Search Tree - Deletion (C++)
Lowest common ancestor in a BST
Binary Search Tree - Python
Trees
Insertion Sort
Hash Functions
Traveling Salesman
Substring Search
Dijkstras Algorithm
Floyd-Warshall Algorithm
Breadth-First Search
Bucket Sort
Quicksort
Depth-First Search
Knapsack Problem
Counting Sort
Cycle Sort
Heap Sort
Prims Algorithm
Matrix Exponentiation
Pigeonhole Sort
Radix Sort
Equation Solving
Odd-Even Sort
Pseudocode
Catalan Number Algorithm
Integer Partition Algorithm
A* Pathfinding
Shell Sort
Selection Sort
Pancake Sort
Longest Common Subsequence
Longest Increasing Subsequence
Maximum Path Sum Algorithm
Maxiumum Subarray Algorithm
Dynamic Time Warping
Pascal Triangle
Line drawing
Shortest Common Supersequence
Sliding Window Algorithm
Application of greedy techniqe
Online algorithms
Fast Fourier Transform
A* Path-finding Algorithm
Check if tree is BST
Binary tree traversal
Lowest common ancestor of a binary tree
Graph Traversal
Minimum Vertex Cover
Multi-threaded Algorithms
Print MxN matrix in square wise
Check two strings are anagrams
Edit Distance Dynamic Algorithm
Applications of Dynamic Programming
Knuth Moriss Pratt
Contributors

Binary Search Tree - Deletion (C++)

suggest change

Before starting with deletion I just want to put some lights on what is a Binary search tree(BST), Each node in a BST can have maximum of two nodes(left and right child).The left sub-tree of a node has a key less than or equal to its parent node’s key. The right sub-tree of a node has a key greater than to its parent node’s key.

Deleting a node in a tree while maintaining its Binary search tree property.

There are three cases to be considered while deleting a node.

Explanation of cases:

  1. When the node to be deleted is a leaf node then simply delete the node and pass nullptr to its parent node.
  2. When a node to be deleted is having only one child then copy the child value to the node value and delete the child (Converted to case 1)
  3. When a node to be delete is having two childs then the minimum from its right sub tree can be copied to the node and then the minimum value can be deleted from the node’s right subtree (Converted to Case 2)

Note: The minimum in the right sub tree can have a maximum of one child and that too right child if it’s having the left child that means it’s not the minimum value or it’s not following BST property.

The structure of a node in a tree and the code for Deletion:

struct node
{
    int data;
    node *left, *right;
};

node* delete_node(node *root, int data)
{
  if(root == nullptr) return root;
  else if(data < root->data) root->left  = delete_node(root->left, data);
  else if(data > root->data) root->right = delete_node(root->right, data);

  else
  {
    if(root->left == nullptr && root->right == nullptr) // Case 1
    {
      free(root);
      root = nullptr;
    }
    else if(root->left == nullptr)       // Case 2
    {
       node* temp = root;
       root= root->right;
       free(temp);
    }
    else if(root->right == nullptr)      // Case 2
    {
       node* temp = root;
       root = root->left;
       free(temp);
    }
    else                                 // Case 3
    {
       node* temp = root->right;

       while(temp->left != nullptr) temp = temp->left;

       root->data = temp->data;
       root->right = delete_node(root->right, temp->data);
    }
  }
  return root;
}

Time complexity of above code is O(h), where h is the height of the tree.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents