Professional Documents
Culture Documents
Ordered Dictionaries
Binary Search
Tree (§3.1.2) Search (§3.1.3)
To search for a key k, Algorithm findElement(k, v)
A binary search tree is a
binary tree storing keys An inorder traversal of a we trace a downward if T.isExternal (v)
path starting at the root return NO_SUCH_KEY
(or key-element pairs) binary search trees if k < key(v)
visits the keys in The next node visited
at its internal nodes and depends on the return findElement(k, T.leftChild(v))
satisfying the following increasing order outcome of the else if k = key(v)
property: comparison of k with return element(v)
Let u, v, and w be three 6 else { k > key(v) }
the key of the current
nodes such that u is in return findElement(k, T.rightChild(v))
2 9 node
the left subtree of v and
w is in the right subtree If we reach a leaf, the < 6
1 4 8 key is not found and we
of v. We have 2 9
key(u) ≤ key(v) ≤ key(w) return NO_SUCH_KEY >
External nodes do not Example: 1 4 = 8
store items findElement(4)