Deletion

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:

images/binary_trees/bst_4.png

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 in one step:

images/binary_trees/bst_16.png

Well, that was simple. But let’s see what happens when we try to delete the 10:

images/binary_trees/bst_17.png

We end up with an ...

Get A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.