Tree searching
Introduction / Definition
Tree searching locates a key in a hierarchical structure. In a binary search tree, comparisons guide the search left or right. The cost depends on tree height. Balanced trees keep the height small.
Syntax / Structure / Formula
BST search: compare → left/right
Detailed Explanation
At each node, the BST property lets the algorithm discard one subtree. Search takes O(h), where h is tree height.
Example
Example: Search 70 in a BST rooted at 50: move right, then continue comparisons.
Output / Result
Only nodes on the search path are visited.
Important Points / Notes
- BST search is O(h).
- An unbalanced tree may degrade to O(n).
Mini Practice Question
What determines BST search complexity?
CheckpointTest Yourself