A binary search tree stores ordered data in a shape that makes search depend on height rather than total size.
- Use ordering to guide search.
- Understand traversal orders.
- Handle deletion cases without breaking ordering.
- Recursion, pointers, and comparisons.
Every node splits the remaining values into smaller values on the left and larger values on the right. Search follows one branch at each level.
5
/ \
3 8
/ \ /
2 4 7
| Term | Meaning |
|---|---|
| BST property | Left subtree values are smaller and right subtree values are larger. |
| Traversal | A systematic way to visit nodes. |
| Successor | The smallest value greater than a node. |
| Height | The longest path from a node to a leaf. |
| File or directory | What to read for |
|---|---|
| Tree.go | Insert, search, traversal, min/max, and deletion. |
| Tree_test.go | Search and traversal coverage. |
- Every subtree is a valid BST.
- Inorder traversal yields sorted values.
- Deletion reconnects children without losing ordered nodes.
Search compares the target with the current node and chooses exactly one branch. Deletion has three cases: leaf, one child, and two children. The two-child case replaces the node with its successor so inorder order remains valid.
| Operation | Time | Space | Why |
|---|---|---|---|
| Search | Average O(log n), worst O(n) | O(h) | Follows one branch per level. |
| Insert | Average O(log n), worst O(n) | O(h) | Searches for a nil child position. |
| Delete | Average O(log n), worst O(n) | O(h) | Search plus restructuring. |
| Traversal | O(n) | O(h) | Visits every node once. |
- Assuming a plain BST is always balanced.
- Deleting a two-child node without preserving successor order.
- Confusing traversal orders.
Inserting sorted values 1, 2, 3 produces a chain. Inserting 2, 1, 3 produces a balanced shape. Both are valid BSTs, but their heights differ.
-
Draw the state (Warm-up)
Use the diagram notation to trace search and deletion on a small input.
Hint
Write the state before the operation, after each important assignment or loop step, and after the invariant is restored.
Reference answer
A complete answer shows the same data before and after the operation, names the changed pointer, index, color, mark, or collection, and ends with the invariant visibly true.
-
Add edge-case coverage (Drill)
Add or inspect tests for empty tree, missing key, leaf deletion, one-child deletion, and two-child deletion.
Hint
Prefer table-driven tests. Give each case a name that explains the behavior under test.
Reference answer
The reference shape is a table with empty input, one minimal valid input, a normal case, and a failure or missing-value case when the module supports one.
-
Explain the complexity (Challenge)
Explain why Search has the complexity shown in the table.
Hint
Count the number of nodes, array cells, characters, or edges that can be visited. Then count extra storage.
Reference answer
A good answer separates input size from auxiliary state. It mentions whether the operation follows one path, scans all elements, visits all edges, or allocates a helper structure.
go test ./BinarySearch