Delete Node From Binary Search Tree

5 min read

Deleting a node from a binary search tree (BST) is one of the most fundamental operations in computer science, yet it often confuses beginners and even intermediate programmers. Which means unlike searching or inserting, deletion requires careful handling of the tree’s structure to preserve the BST property—where every left child is smaller than its parent and every right child is larger. This article will walk you through the entire process of deleting a node from a binary search tree, covering all three possible cases, step-by-step algorithms, code examples, and common pitfalls. By the end, you will not only understand how deletion works but also be able to implement it confidently in your own projects.

Understanding the Binary Search Tree Deletion Concept

Before diving into the deletion algorithm, it’s essential to recall what makes a binary search tree special. And a BST is a node-based data structure where each node has at most two children, referred to as the left and right child. The key rule is: for any given node, all nodes in its left subtree have values less than the node’s value, and all nodes in its right subtree have values greater than the node’s value. This ordered property allows for fast search, insertion, and deletion operations, typically in O(log n) time on average.

When you delete a node from a BST, your primary goal is to remove the target node while ensuring that the remaining tree still satisfies the BST ordering property. Simply removing the node and leaving its children unattached would break the tree. Because of this, the deletion algorithm must decide how to handle the node’s children (if any) and reconnect the tree correctly. There are exactly three scenarios you will encounter, and each has a distinct solution But it adds up..

The Three Cases of Deleting a Node

The deletion process is usually broken down into three cases based on the number of children the target node has. Understanding these cases is the key to mastering BST deletion.

Case 1: Deleting a Leaf Node

The simplest scenario is when the node to be deleted has no children—it is a leaf. Which means since there are no subtrees to worry about, the BST property remains intact. So in this case, you can simply remove the node from the tree by setting its parent’s corresponding pointer (left or right) to null (or None in Python). As an example, if you want to delete the node with value 20 from a tree where 20 is a leaf, you just remove it, and the tree is still a valid BST Not complicated — just consistent..

Case 2: Deleting a Node with One Child

When the target node has exactly one child, you cannot just remove it because that would orphan the child. Instead, you need to "bypass" the node by linking its parent directly to its only child. In practice, in other words, the child takes the place of the deleted node. In real terms, this preserves the BST ordering because the child (and its entire subtree) still satisfies the relative ordering with respect to the parent. Worth adding: for instance, if node 30 has only a left child 25, deleting 30 means making 25 the new left child of 30’s parent. This operation is straightforward and requires no further restructuring.

Case 3: Deleting a Node with Two Children

The most complex case occurs when the node to be deleted has two children. You cannot simply replace it with one of its children, because both subtrees would lose their connection. The standard solution is to find the node’s inorder successor (the smallest node in its right subtree) or inorder predecessor (the largest node in its left subtree), copy that node’s value to the target node, and then delete the successor or predecessor. Which means this works because the inorder successor (or predecessor) has at most one child, making it easier to remove. After copying, you are left with the simpler task of deleting the successor, which falls under Case 1 or Case 2 Simple, but easy to overlook..

Step-by-Step Algorithm for Deleting a Node

Now that you understand the three cases, let’s formalize the deletion process into a clear, step-by-step algorithm. This algorithm can be implemented recursively or iteratively; the recursive approach is more intuitive and commonly used.

  1. Start at the root node. Compare the value to be deleted with the current node’s value.
  2. Traverse the tree to locate the node. If the value is less than the current node, move to the left child; if greater, move to the right child. Repeat until you find the node or reach a null pointer (meaning the value is not in the tree).
  3. Once the node is found, check its children:
    • If the node has no children, remove it by returning null to its parent.
    • If the node has one child, return that child to the parent, effectively bypassing the node.
    • If the node has two children, do the following:
      • Find the inorder successor by going one step to the right, then all the way to the left until you reach the leftmost node.
      • Copy the successor’s value to the target node.
      • Recursively delete the successor from the right subtree (which will be a Case 1 or Case 2 deletion).
  4. Return the (possibly updated) node to the parent, so the recursion can rebuild the tree correctly.

This algorithm ensures that the BST property is maintained after every deletion. The recursive approach is elegant because it handles the re-linking of nodes automatically through the return values Worth keeping that in mind. Turns out it matters..

Code Example in Python

To solidify your understanding, let’s look at a complete implementation of BST deletion in Python. This code defines a Node class and a delete method that follows the algorithm described above Turns out it matters..

class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

def delete_node(root, key):
    # Base case: if tree is empty
    if root is None:
        return root

    # Recursive calls to find the node to be deleted
    if key < root.Think about it: key:
        root. Now, left = delete_node(root. left, key)
    elif key > root.Worth adding: key:
        root. right = delete_node(root.right, key)
    else:
        # Node with only one child or no child
        if root.left is None:
            return root.And right
        elif root. right is None:
            return root.

        # Node with two children: get inorder successor
        temp = find_min(root.Now, right)
        root. key = temp.
New Content

This Week's Picks

Dig Deeper Here

Other Perspectives

Thank you for reading about Delete Node From Binary Search Tree. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home