← Course home

Lookup and priority · Lesson 29 of 52

Binary search tree operations

Trace ordered lookup, insertion, deletion, and successor operations in a BST.

Full lessonAbout 10 min

You will learn to trace BST lookup, insertion and deletion and find a successor while preserving ordering.

Recommended first: Binary search.

On this page 6 sections

The idea

Insert 4, then 2, then 6, then 1. The inorder walk is 1, 2, 4, 6.

A binary search tree node holds a key, a left child, and a right child. Every key in the left subtree is < the node's key, and every key in the right subtree is > it. Insert walks that rule until it finds an empty child, then hangs the new key there. Inserting 4, 2, 6, 1 puts 4 at the root, 2 on its left, 6 on its right, and 1 on the left of 2. The library's inorder walk visits left, node, right, so it returns [1, 2, 4, 6]. A duplicate key is ignored.

A worked trace

StepStateWhat changed
rootparent = None4 becomes the root child of None.
leftparent = 42 becomes the left child of 4.
rightparent = 46 becomes the right child of 4.
leftparent = 21 becomes the left child of 2.

Why it is correct

The search invariant is that the target, if it exists, lies in the subtree still being walked. Going left throws away the node and its right subtree because those keys are ≥ the node and the target is smaller. Going right throws away the left subtree for the symmetric reason. Insert uses the same walk and writes the new node only at an empty child, so the parent comparison that led there is still true. Inorder returns sorted keys because it emits the whole left subtree, then the node, then the whole right subtree, and both subtrees are themselves ordered.

A common mistake

The successor of a node with no right child is an ancestor, not the left child.

What it costs

Each insert or lookup does one comparison per level, so the time is Θ(h) where h is the height. A balanced tree has h = Θ(log n). Inserting sorted keys makes a chain and h = n, so the same operations are Θ(n). The call in this library is a loop, so extra memory is O(1) besides the nodes. The nodes themselves are Θ(n). Deletion has the same height cost. The successor is the next inorder key, the leftmost node of the right subtree when a right child exists.

Run it

python
from bitsandbytes.trees.bst import BinarySearchTreetree = BinarySearchTree()for value in (4, 2, 6, 1):    tree.insert(value)print(tree.inorder())# prints: [1, 2, 4, 6] 

Practice and next step

Open Tree traversals next. Traversals visit that same tree in preorder, postorder, and level order.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.trees.bst

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Insert 4, 2, 6, 1

Each insert walks until it finds an empty child. The library inorder walk then returns [1, 2, 4, 6].

parent
None

State

  1. 04chosen

4 becomes the root child of None.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Node 2 has no right child, and its parent 4 is larger. Where is the successor of 2?
2Node 6 has a right child 7. Where is the successor of 6?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.