Deletion is the least straightforward operation within a binary search tree and requires some careful maneuvering.
Let’s say we want to delete the 4 from this binary search tree:
First, we perform a search to find the 4. We won’t visualize this search again, since you’ve already got that down.
Once we find the 4, we can delete it one step:
Well, that was simple. But let’s see what happens when we try to delete the 10:
We end up with an 11 that isn’t connected to the tree anymore. And we can’t have that, because we’d lose the 11 forever.
However, there’s a solution to this problem: we can plug the 11 into where the 10 used to be:
So far, our deletion algorithm follows these rules:
- If the node being deleted has no children, simply delete it.
- If the node being deleted has one child, delete the node and plug the child into the spot where the deleted node was.
Deleting a Node with Two Children
Deleting a node that has two children is the most complex scenario. Let’s say we want to delete the 56 in this tree:
What are we going to do with its former children, 52 and 61? We cannot move both of them to where the 56 was. This is where the next rule of the deletion algorithm comes into play:
- When deleting a node with two children, replace the deleted node with the successor node. The successor node is the child node whose value is the least of all values that are greater than the deleted node.
That was a tricky sentence. To put in other words: if we were to put the deleted node and all of its descendants in ascending order, the successor node would be the next number after the one we just deleted.
In this case, it’s easy to figure out which node is the successor, since the deleted node had only two descendants. If we put the numbers 52-56-61 in ascending order, the next number after 56 is 61.
Once we find the successor node, we plug it into where the deleted node was. So, we replace the 56 with the 61:
Finding the Successor Node
How does the computer find the successor node? This can be tricky when we delete a node high up in the tree.
Here’s the algorithm for finding the successor node:
- Visit the right child of the deleted value, and then keep on visiting the left child of each subsequent child until there are no more left children. The bottom value is the successor node.
Let’s see this again in action in a more complex example. Let’s delete the root node:
We now need to plug the successor node into where the 50 was, and turn it into the root node. So, let’s find the successor node.
To do this, we first visit the right child of the deleted node, and then keep descending leftward until we reach a node that doesn’t have a left child:
It turns out that the 52 is the successor node.
Now that we’ve found the successor node, we plug it into the node we deleted:
And we’re done!
Successor Node with a Right Child
However, there is one case we haven’t accounted for yet, and that’s where the successor node has a right child of its own. Let’s recreate the preceding tree, but add a right child to the 52 as shown in the diagram .
In this case, we can’t simply plug the successor node—the 52—into the root, since we’d leave its child of 55 hanging. Because of this, there’s one more rule to our deletion algorithm:
- If the successor node has a right child, after plugging the successor node into the spot of the deleted node, take the former right child of the successor node and turn it into the left child of the former parent of the successor node.
That was another tricky sentence, so let’s walk through the steps.
First, we plug the successor node (52) into the root. This leaves the 55 dangling without a parent:
Next, we turn the 55 into the left child of the former parent of the successor node. In this case, 61 used to be the parent of the successor node, so we make the 55 the left child of the 61:
And now we’re really done.
The Complete Deletion Algorithm
Putting all the steps together, the algorithm for deletion from a binary search tree is:
-
If the node being deleted has no children, simply delete it.
-
If the node being deleted has one child, delete the node and plug the child into the spot where the deleted node was.
-
When deleting a node with two children, replace the deleted node with the successor node. The successor node is the child node whose value is the least of all values that are greater than the deleted node.
-
To find the successor node: visit the right child of the deleted value, and then keep on visiting the left child of each subsequent child until there are no more left children. The bottom value is the successor node.
-
If the successor node has a right child, after plugging the successor node into the spot of the deleted node, take the former right child of the successor node and turn it into the left child of the former parent of the successor node.
Code Implementation: Binary Search Tree Deletion
Here’s a recursive Python implementation of deletion from a binary search tree:
| | def delete (valueToDelete, node): |
| | |
| | # The base case is when we've hit the bottom of the tree, |
| | # and the parent node has no children: |
| | if node is None: |
| | return None |
| | |
| | # If the value we're deleting is less or greater than the current node, |
| | # we set the left or right child respectively to be |
| | # the return value of a recursive call of this |
| | # very method on the current |
| | # node's left or right subtree. |
| | elif valueToDelete < node.value: |
| | node.leftChild = delete(valueToDelete, node.leftChild) |
| | |
| | # We return the current node (and its subtree if existent) to |
| | # be used as the new value of its parent's left or right child: |
| | return node |
| | elif valueToDelete > node.value: |
| | node.rightChild = delete(valueToDelete, node.rightChild) |
| | return node |
| | |
| | # If the current node is the one we want to delete: |
| | elif valueToDelete == node.value: |
| | |
| | # If the current node has no left child, we delete it by |
| | # returning its right child (and its subtree if existent) |
| | # to be its parent's new subtree: |
| | if node.leftChild is None: |
| | return node.rightChild |
| | |
| | # (If the current node has no left OR right child, this ends up |
| | # being None as per the first line of code in this function.) |
| | |
| | elif node.rightChild is None: |
| | return node.leftChild |
| | |
| | # If the current node has two children, we delete the current node |
| | # by calling the lift function (below), |
| | # which changes the current node's |
| | # value to the value of its successor node: |
| | else : |
| | node.rightChild = lift(node.rightChild, node) |
| | return node |
| | |
| | |
| | def lift (node, nodeToDelete): |
| | |
| | # If the current node of this function has a left child, |
| | # we recursively call this function to continue down |
| | # the left subtree to find the successor node. |
| | if node.leftChild: |
| | node.leftChild = lift(node.leftChild, nodeToDelete) |
| | return node |
| | # If the current node has no left child, that means the current node |
| | # of this function is the successor node, and we take its value |
| | # and make it the new value of the node that we're deleting: |
| | else : |
| | nodeToDelete.value = node.value |
| | # We return the successor node's right child to be now used |
| | # as its parent's left child: |
| | return node.rightChild |
Admittedly, this code is a bit involved, so let’s break it down a bit.
The function accepts two arguments:
| | def delete (valueToDelete, node): |
The valueToDelete is the value we’re deleting from the tree, and the node is the root of the tree. When we first call this function, the node will be the actual root node, but as this function calls itself recursively, the node may be lower down in the tree and simply be the root of a smaller subtree. In any case, the node is the root of either the whole tree or one of its subtrees.
The base case is when the node does not actually exist:
| | if node is None: |
| | return None |
This will happen when a recursive call to this function tries to access a child node that does not exist. In this case, we return None .
Next, we check whether the valueToDelete is less than or greater than the value of the current node :
| | elif valueToDelete < node.value: |
| | node.leftChild = delete(valueToDelete, node.leftChild) |
| | return node |
| | elif valueToDelete > node.value: |
| | node.rightChild = delete(valueToDelete, node.rightChild) |
| | return node |
This snippet may not be immediately intuitive, but here’s how it works. If the valueToDelete is less than the value of the current node , we know that if the valueToDelete is to be found somewhere in the tree, it’ll be found among the current node ’s left descendants.
Here’s the clever part: we then overwrite the current node ’s left child to be the result of recursively calling the delete function on the current node ’s left child. The delete function itself eventually returns a node, so we take this result and make it the current node ’s left child.
Often, however, this “overwrite” doesn’t actually change the left child, because calling delete on the left child may return that same left child. To make sense of this, imagine we’re trying to delete the 4 from this example tree:
Originally, node is the root node, which has the value of 50. Since 4 (the valueToDelete ) is less than 50, we say that the 50’s left child should now be the result of calling delete on the 50’s current left child, which is 25.
So, what will the 50’s left child be? Let’s see.
When we recursively call delete on 25, we again determine that 4 is less than 25 (the current node ), so we continue the recursion with the 25’s left child, which is the 10. However, no matter what the 25’s left child will end up being, since we say return node , we return the 25 node at the end of the current function call.
This means that back when I said that the 50’s left child should be the result of calling delete on the 25, we ended getting back the 25 itself, so the 50’s left child actually remains unchanged.
The current node ’s left or right child will change, however, if the result of the next recursive call involves an actual deletion.
Let’s examine that bit of code next:
| | elif valueToDelete == node.value: |
This is to say, the current node is the one we want to delete. In order to delete it properly, we first need to determine whether the current node has any children, as this will affect the deletion algorithm.
We begin by checking whether the node we’re going to delete has any left children:
| | if node.leftChild is None: |
| | return node.rightChild |
If the current node has no left children, we can return the current node ’s right child as a result of this function. Remember, whichever node we return will become either the left or right child of the previous node in the call stack. So, let’s imagine that the current node has a right child. In this case, by returning the right child, we make it a child node of the previous node in the call stack, effectively deleting the current node from the tree.
Now, if the current node happens to not have a right child, that’s okay too, since we’ll end up passing None as a result of this function, and that will also effectively remove the current node from the tree.
Moving on in our code, if the current node does have a left child but doesn’t have a right child, we can still easily delete the current node:
| | elif node.rightChild is None: |
| | return node.leftChild |
In this case, we delete the current node by returning its left child to become the child node of the previous node in the call stack.
Finally, we encounter the most complex case, which is when the node we’re deleting has two children:
| | else : |
| | node.rightChild = lift(node.rightChild, node) |
| | return node |
In this case, we call the lift function, take its result, and make it the current node ’s right child.
What does the lift function do?
When we call it, we pass in the current node ’s right child in addition to the node itself. The lift function accomplishes four things:
-
It finds the successor node.
-
It overwrites the value of the nodeToDelete and makes it the value of the successor node. This is how we get the successor node into the correct spot. Note that we don’t move the actual successor node object anywhere; we simply copy its value into the node we’re “deleting.”
-
To eliminate the original successor node object, the function then turns the original successor node’s right child into its parent’s left child.
-
After all the recursion is said and done, it finally returns either the original rightChild passed into it in the first place, or None if the original rightChild ended up serving as the successor node (which would happen if it had no left children of its own).
We then take return value of lift and make it the current node ’s right child. This would either leave the right child alone, or change it to None if the right child was used as the successor node.
It’s not just you; the delete function is one of the most complex pieces of code in the book. It may take careful study to follow the entire process, even after this walk-through.
The Efficiency of Binary Search Tree Deletion
Like search and insertion, deleting from trees is also typically O(log N). This is because deletion requires a search plus a few extra steps to deal with any hanging children. Contrast this with deleting a value from an ordered array, which is O(N) due to shifting elements to the left to close the gap of the deleted value.