Ordered Trees for Efficient Search¶
root = {
"value": 10,
"left": {"value": 5, "left": None, "right": None},
"right": {"value": 15, "left": None, "right": None},
}
print(root["value"], root["left"]["value"], root["right"]["value"])
A binary search tree uses ordering rules to decide where to go next¶
Tree traversals taught visit order. Binary search trees add a stronger structure rule: every decision to go left or right is based on value comparison. That makes search more directed than visiting every node blindly.
Core Explanation¶
In a binary search tree, smaller values go left and larger values go right.
This ordering property makes search more efficient than scanning an unordered collection in many cases.
Continuity from tree traversals
Traversal asked “which node should I visit next?” A BST adds another question: “based on the value, should I go left or right?”
Worked Example and Practice Lab¶
Expected output
10
6
14This first example highlights the BST ordering idea: smaller values appear on the left side of the node, and larger values appear on the right side.
Expected output
4
14Reading BST Decisions¶
These short examples focus on how comparisons guide the next step.
Compare against the root¶
A search starts by comparing the target value with the current node.
Follow a left decision¶
Values smaller than the current node move into the left subtree.
Follow a right decision¶
Values larger than the current node move into the right subtree.
Exercises¶
Explain the BST ordering rule in your own words.
Describe why 6 belongs on the left of 10 in the example.
Give one reason BST structure can support faster search than an unordered collection.
Explain how BST search differs from a traversal that visits every node.
Hint
At each node, ask one comparison question: is the target smaller, larger, or equal?
Guided Practice¶
What property helps a binary search tree support efficient search?¶
In the example values, which number would typically appear to the right of 10 in a BST?¶
Key Takeaway¶
Binary search trees turn value comparison into structure. That makes search more focused than generic traversal and prepares you for later graph-search notebooks where navigation strategy becomes even more important.