Removing From A Binary Search Tree

4 min read

Removing from a Binary Search Tree

Removing from a binary search tree (BST) is the process of deleting a node while preserving the binary search tree property: every value in the left subtree is less than the node, and every value in the right subtree is greater. A correct deletion algorithm matters because careless removal can break search, insertion, and ordering guarantees, causing slower operations or incorrect results.

Introduction to Binary Search Tree Deletion

A binary search tree is a tree-based data structure where each node has at most two children: a left child and a right child. On the flip side, the left subtree contains values smaller than the node, while the right subtree contains values larger than the node. This structure allows efficient searching, insertion, and deletion when the tree is reasonably balanced.

Deleting a value from a BST may seem simple at first, but it has several important cases. Now, the operation depends on how many children the node being removed has. Some deletions are easy, while others require careful restructuring to maintain the BST’s ordering rules Practical, not theoretical..

The three main deletion cases are:

  1. Deleting a leaf node
  2. Deleting a node with one child
  3. Deleting a node with two children

Understanding these cases is the foundation of BST deletion Worth keeping that in mind..

The Binary Search Tree Property

Don't overlook before deleting anything, it. It carries more weight than people think. For every node in a BST:

  • All values in the node’s left subtree are smaller.
  • All values in the node’s right subtree are larger.
  • The left and right subtrees must also be valid BSTs.

Take this: consider this tree:

        50
       /  \
     30    70
    / \    /
  20  40  60

If we remove 30, we cannot simply delete it and leave 20 and 40 disconnected. We must replace the node in a way that keeps 20 on the left and 40 on the right.

Case 1: Removing a Leaf Node

A leaf node has no children. This is the easiest deletion case because removing the node does not affect the structure of the rest of the tree.

For example:

        50
       /  \
     30    70
    / \    /
  20  40  60

If we delete 20, the tree simply removes that node:

        50
       /  \
     30    70
        /
      40

The BST property still holds because removing a leaf does not change the relative order of the remaining values.

Steps for Deleting a Leaf Node

  1. Search for the target value.
  2. Confirm that the node has no left or right child.
  3. Remove the node from its parent.
  4. Set the parent’s corresponding child pointer to null.

This operation is straightforward and efficient once the node is found.

Case 2: Removing a Node with One Child

A node with one child is also relatively simple to remove. The idea is to bypass the node by connecting its parent directly to its child.

For example:

        50
       /  \
     30    70
    /
  20

If we delete 30, the node has one child, 20. We can remove 30 and move 20 into its place:

        50
       /  \
     20    70

This preserves the BST property because 20 is smaller than 50, so it belongs in the left subtree Surprisingly effective..

Steps for Deleting a Node with One Child

  1. Find the node to delete.
  2. Identify whether the node has a left child or a right child.
  3. Connect the node’s parent directly to the node’s only child.
  4. If the node is the root, make its child the new root.

This method avoids leaving gaps in the tree and keeps the ordering intact Small thing, real impact..

Case 3: Removing a Node with Two Children

The most complex case occurs when the node being removed has both a left and a right child.

For example:

        50
       /  \
     30    70
    / \   /
  20  40 60

If we delete 50, we cannot simply remove it and attach both subtrees to one node. We need a replacement value that maintains the BST property Easy to understand, harder to ignore..

There are two common choices:

  • Inorder successor
  • Inorder predecessor

Using the Inorder Successor

The inorder successor of a node is the smallest value in its right subtree. It is the next larger value after the node in sorted order.

In this tree:

        50
       /  \
     30    70
    / \   /
  20  40 60

The inorder successor of 50 is 60, because 60 is the smallest value in the right subtree But it adds up..

To delete 50, we can:

  1. Find the inorder successor, 60.
  2. Copy the value 60 into the node being deleted.
  3. Delete the original successor node from the right subtree.

After copying:

        60
       /  \
     30    70
    / \
  20  40
What's New

This Week's Picks

Others Explored

While You're Here

Thank you for reading about Removing From A 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