You are on page 1of 3

Ordered Dictionaries

Keys are assumed to come from a total


Binary Search Trees order.
New operations:
< 6 „ first(): first entry in the dictionary ordering
2
>
9 „ last(): last entry in the dictionary ordering
1 4 = 8 „ successors(k): iterator of entries with keys
greater than or equal to k; increasing order
„ predecessors(k): iterator of entries with keys
less than or equal to k; decreasing order
© 2004 Goodrich, Tamassia Binary Search Trees 1 © 2004 Goodrich, Tamassia Binary Search Trees 2

Binary Search (§ 8.3.3) Search Tables


Binary search can perform operation find(k) on a dictionary
implemented by means of an array-based sequence, sorted by key A search 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: find(7)
Performance:
„ find takes O(log n) time, using binary search
0 1 3 4 5 7 8 9 11 14 16 18 19
„ insert takes O(n) time since in the worst case we have to shift n/2
l m h items to make room for the new item
0 1 3 4 5 7 8 9 11 14 16 18 19 „ remove take O(n) time since in the worst case we have to shift n/2
l m h 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)
© 2004 Goodrich, Tamassia Binary Search Trees 3 © 2004 Goodrich, Tamassia Binary Search Trees 4
Binary Search
Trees (§ 9.1) Search (§ 9.1.1)
To search for a key k, Algorithm TreeSearch(k, v)
A binary search tree is a we trace a downward
binary tree storing keys An inorder traversal of a if T.isExternal (v)
path starting at the root return v
(or key- value entries) at binary search trees The next node visited if k < key(v)
its internal nodes and visits the keys in depends on the return TreeSearch(k, T.left(v))
satisfying the following increasing order outcome of the else if k = key(v)
comparison of k with
property: the key of the current
return v
Let u, v, and w be three 6 else { k > key(v) }
„ node
nodes such that u is in return TreeSearch(k, T.right(v))
2 9 If we reach a leaf, the
the left subtree of v and
key is not found and we < 6
w is in the right subtree 1 4 8 return nukk
of v. We have 2 9
key(u) ≤ key(v) ≤ key(w) Example: find(4): >
Call TreeSearch(4,root) 4 =
External nodes do not 8
„
1
store items
© 2004 Goodrich, Tamassia Binary Search Trees 5 © 2004 Goodrich, Tamassia Binary Search Trees 6

Insertion Deletion
6 6
To perform operation < To perform operation <
inser(k, o), we search for 2 9 remove(k), we search for 2 9
> >
key k (using TreeSearch) 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 removeExternal(w), which
2 9
Example: insert 5 1 4 8 removes w and its parent
w Example: remove 4 1 5 8
5

© 2004 Goodrich, Tamassia Binary Search Trees 7 © 2004 Goodrich, Tamassia Binary Search Trees 8
Deletion (cont.) Performance
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 find, insert and
„ we copy key(w) into node v remove take O(h) time
we remove node w and its
„
left child z (which must be a
1
v The height h is O(n) in
leaf) by means of operation 5 the worst case and
removeExternal(z) 2 8 O(log n) in the best
Example: remove 3 6 9
case

© 2004 Goodrich, Tamassia Binary Search Trees 9 © 2004 Goodrich, Tamassia Binary Search Trees 10

You might also like