In the binary search tree with root 6, children 2 and 9, 2's children 1 and 4, and 9's left child 8, which nodes does TreeSearch(4, root) visit, and where does TreeSearch(5, root) end?
TreeSearch(4) visits 6, 2 and 4: 4 < 6 goes left, 4 > 2 goes right, 4 = 4 is found. TreeSearch(5) follows the same path and then goes right from 4 (5 > 4), ending at 4's right external node: 5 is not in the tree.
* A search follows one root-to-leaf path; a miss ends at the external node where the key belongs. *
| Search | At 6 | At 2 | At 4 | Result |
|---|---|---|---|---|
| TreeSearch(4) | 4 < 6: left | 4 > 2: right | equal | the internal node 4 |
| TreeSearch(5) | 5 < 6: left | 5 > 2: right | 5 > 4: right | 4's right external node |
Both searches inspect three keys and never touch 1, 9 or 8: the first comparison at the root already excludes the entire right subtree.
The unsuccessful search is just as useful as the successful one. It names the external node where 5 would have to be, which is exactly where insert(5) will put it.