Search down the branches.
Maintain a search-tree invariant and account for a tree’s actual height.
01 Model the idea
A binary search tree places smaller keys in the left subtree and larger keys in the right subtree under a chosen duplicate policy. Search compares with a node and chooses one branch. Its cost is proportional to height, not automatically logarithmic: a chain-shaped tree behaves like a linear scan. This example ignores duplicate insertions, treating the tree as a set.
Left keys are smaller and right keys larger than the node’s key.
02 State the strategy
Read the steps independently of syntax. Explain what each step preserves before translating it into code.
COMPARE with the current key
FOLLOW left for smaller, right for larger
INSERT at an empty child
IGNORE duplicates under this set policy03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
from dataclasses import dataclass
@dataclass
class Node:
value: int
left: "Node | None" = None
right: "Node | None" = None
def insert(node, value):
if node is None: return Node(value)
if value < node.value: node.left = insert(node.left, value)
elif value > node.value: node.right = insert(node.right, value)
return node
def contains(node, value):
while node is not None:
if value == node.value: return True
node = node.left if value < node.value else node.right
return False
root = None
for value in [4, 2, 6, 1, 3]: root = insert(root, value)
print(contains(root, 3), contains(root, 9))True False
- 1
insert returns the subtree root so callers can reconnect it, including the very first insertion into None.
- 2
The strict comparisons make the duplicate policy explicit. A record index might instead update a value or store a collection for duplicate keys.
- 3
Balanced search trees keep height logarithmic through extra invariants and rebalancing. For ordinary Python application key lookup, a dictionary is usually simpler unless ordered operations are specifically needed.
04 Count the cost
O(h); O(log n) balanced and O(n) worst case
O(h) recursive insertion stack; O(n) nodes
Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.
05 Your next move
Test repeated inserts and confirm searching for an absent key remains false.
Reveal one possible solution +
from dataclasses import dataclass
@dataclass
class Node:
value: int
left: "Node | None" = None
right: "Node | None" = None
def insert(node, value):
if node is None: return Node(value)
if value < node.value: node.left = insert(node.left, value)
elif value > node.value: node.right = insert(node.right, value)
return node
def contains(node, value):
while node is not None:
if value == node.value: return True
node = node.left if value < node.value else node.right
return False
root = None
for value in [4, 2, 6, 2]: root = insert(root, value)
assert contains(root, 2) is True
assert contains(root, 9) is False
assert root.left.value == 2 and root.left.left is None and root.left.right is None
print("Duplicate and absent-key checks passed")06 Check your reasoning
What determines search cost in an unbalanced BST?
Understand it. Then own it.
Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.