You are on page 1of 2

Dictionaries 4/5/2002 15:1

Ordered Dictionaries

Binary Search Trees Š Keys are assumed to come from a total


order.
< 6 Š New operations:
2 9 „ closestKeyBefore(k)
>
1 4 = 8 „ closestElemBefore(k)
„ closestKeyAfter(k)
„ closestElemAfter(k)

Binary Search Trees 1 Binary Search Trees 2

Binary Search (§3.1.1) Lookup Table (§3.1.1)


Š Binary search performs operation findElement(k) on a dictionary
implemented by means of an array-based sequence, sorted by key Š A lookup table is a dictionary implemented by means of a sorted
„ similar to the high-low game sequence
„ at each step, the number of candidate items is halved „ We store the items of the dictionary in an array-based sequence,
sorted by key
„ terminates after O(log n) steps
„ We use an external comparator for the keys
Š Example: findElement(7)
Š Performance:
„ findElement takes O(log n) time, using binary search
0 1 3 4 5 7 8 9 11 14 16 18 19
„ insertItem takes O(n) time since in the worst case we have to shift
l m h n/2 items to make room for the new item
0 1 3 4 5 7 8 9 11 14 16 18 19 „ removeElement take O(n) time since in the worst case we have to
l m h shift n/2 items to compact the items after the removal
0 1 3 4 5 7 8 9 11 14 16 18 19 Š The lookup table is effective only for dictionaries of small size or
l m h for dictionaries on which searches are the most common
0 1 3 4 5 7 8 9 11 14 16 18 19 operations, while insertions and removals are rarely performed
l=m =h (e.g., credit card authorizations)
Binary Search Trees 3 Binary Search Trees 4

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)

Binary Search Trees 5 Binary Search Trees 6


Dictionaries 4/5/2002 15:1

Insertion (§3.1.4) Deletion (§3.1.5)


6 6
Š To perform operation < Š To perform operation <
insertItem(k, o), we search 2 9 removeElement(k), we 2 9
> >
for key k search for key k
1 4 8 1 4 v 8
Š Assume k is not already in > Š Assume key k is in the tree,
w
the tree, and let let w be and let let v be the node 5
the leaf reached by the w storing k
search Š If node v has a leaf child w,
6
Š We insert k at node w and we remove v and w from the
expand w into an internal tree with operation 6
2 9
node removeAboveExternal(w)
2 9
Š Example: insert 5 1 4 8 Š Example: remove 4
w 1 5 8
5

Binary Search Trees 7 Binary Search Trees 8

Deletion (cont.) Performance (§3.1.6)


1
Š We consider the case where v Š Consider a dictionary
3
the key k to be removed is with n items
stored at a node v whose 2 8 implemented by means
children are both internal 6 9 of a binary search tree
„ we find the internal node w w of height h
that follows v in an inorder 5
„ the space used is O(n)
traversal z „ methods findElement ,
„ we copy key(w) into node v insertItem and
„ we remove node w and its 1 removeElement take O(h)
left child z (which must be a v time
leaf) by means of operation 5
removeAboveExternal(z) 2 8 Š The height h is O(n) in
the worst case and
Š Example: remove 3 6 9 O(log n) in the best
case
Binary Search Trees 9 Binary Search Trees 10

You might also like