Lecture07_SearchTrees
Lecture07_SearchTrees
Algorithms
"
Search Trees!
!
Outline"
• Binary Search Trees!
• AVL Trees!
• (2,4) Trees!
2
Binary Search Trees!
!
"
<
6
2 9
>
1 4 =
8
Ordered Dictionaries"
• Keys are assumed to come from a total
order.!
• New operations: !
– first(): first entry in the dictionary ordering!
– last(): last entry in the dictionary ordering!
– 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!
4
Binary Search"
• Binary search can perform operation find(k) on a dictionary
implemented by means of an array-based sequence, sorted by
key!
– similar to the high-low game!
– at each step, the number of candidate items is halved!
– terminates after O(log n) steps!
• Example: find(7)!
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l=m =h 5
Search Tables"
• A search table is a dictionary implemented by means of a sorted
sequence!
– We store the items of the dictionary in an array-based sequence,
sorted by key!
– We use an external comparator for the keys!
• Performance:!
– find takes O(log n) time, using binary search!
– insert takes O(n) time since in the worst case we have to shift n
items to make room for the new item!
– remove take O(n) time since in the worst case we have to shift n-1
items to compact the items after the removal!
• The lookup table is effective only for dictionaries of small size or
for dictionaries on which searches are the most common
operations, while insertions and removals are rarely performed
(e.g., credit card authorizations)!
6
Binary Search
Trees"
• A binary search tree is a
binary tree storing keys • An inorder traversal of a
(or key-value entries) at binary search trees
its internal nodes and visits the keys in
satisfying the following increasing order!
property:!
– Let u, v, and w be three 6
nodes such that u is in
2 9
the left subtree of v and w
is in the right subtree of 1 4 8
v. We have
key(u) ≤ key(v) ≤ key(w)
• External nodes do not
store items!
7
Search"
• To search for a key k, Algorithm TreeSearch(k, v)
we trace a downward if T.isExternal (v)
path starting at the root! return v // v has null value
• The next node visited if k < key(v)
depends on the return TreeSearch(k, T.left(v))
outcome of the else if k = key(v)
comparison of k with the return v
key of the current node! else { k > key(v) }
• If we reach a leaf, the return TreeSearch(k, T.right(v))
key is not found and we
return null! <
6
• Example: find(4):!
– Call TreeSearch(4,root)! 2 9
>
1 4 =
8
8
Insertion"
6
• To perform operation <
insert(k, o), we search for 2 9
>
key k (using TreeSearch)!
1 4 8
• Assume k is not already in >
the tree, and let w be the
leaf reached by the search! w
• We insert k at node w and
expand w into an internal 6
node! 2 9
• Example: insert 5!
1 4 8
w
5
9
Deletion"
6
• To perform operation <
remove(k), we search for 2 9
key k >
1 4 v 8
• Assume key k is in the tree,
w
and let let v be the node 5
storing k
• If node v has a leaf child w,
we remove v and w from the
tree with operation 6
removeExternal(w), which
2 9
removes w and its parent!
• Example: remove 4! 1 5 8
10
Deletion (cont.)"
1
• We consider the case where v
3
the key k to be removed is
stored at a node v whose 2 8
children are both internal! 6 9
– we find the internal node w w
that follows v in an inorder 5
traversal! z
– we copy key(w) into node v!
– we remove node w and its 1
left child z (which must be a v
leaf) by means of operation 5
removeExternal(z)! 2 8
• Example: remove 3! 6 9
11
Performance"
• Consider a dictionary
with n items
implemented by means
of a binary search tree
of height h!
– the space used is O(n)!
– methods find, insert and
remove take O(h) time!
• The height h is O(n) in
the worst case and
O(log n) in the best
case!
12
AVL Trees"
6
v
3 8
z
4
AVL Tree Definition"
• AVL trees are 4
44
balanced.!
2 3
• An AVL Tree is a 17 78
binary search tree 1 2 1
32 50 88
such that for every
1 1
internal node v of T, 48 62
the heights of the
children of v can differ
by at most 1.! An example of an AVL tree where the
heights are shown next to the nodes:
14
n(2) 3
4 n(1)
c=z
17 78 17 78
a=y
32 50 88 32 50 88
48 62 48 62
b=x
54
w
T3
T1 b=y b=x
T1 T2
T2 T3
a=z c=x a=z c=y
54 88
image, or the image may
T2
T0 T1 18T3
Restructuring " " "
"(as Single Rotations)"
• Single Rotations:!
a =z single rotation b =y
b =y a =z c= x
c= x
T0 T3
T1 T3 T0 T1 T2
T2
T3 T3
T0 T2 T2 T1 T0
T1 19
Restructuring " " " "
"(as Double Rotations)"
• double rotations:!
a =z double rotation b =x
c=y a =z c=y
b =x
T0 T2
T2 T3 T0 T1 T3
T1
T0 T2
T3 T2 T3 T1 T0
T1
20
Removal in an AVL Tree"
• Removal begins as in a binary search tree, which
means the node removed will become an empty
external node. Its parent, w, may cause an imbalance.!
• Example: !
44 44
17 62 17 62
32 50 78 50 78
48 54 88 48 54 88
44 78
w 17 62 b=y
17 50 88
50 78 c=x
48 54
48 54 88
22
Running Times for
AVL Trees"
• a single restructure is O(1)!
– using a linked-structure binary tree!
• find is O(log n)!
– height of tree is O(log n), no restructures needed!
• insert is O(log n)!
– initial find is O(log n)!
– Restructuring up the tree, maintaining heights is O(log n)!
• remove is O(log n)!
– initial find is O(log n)!
– Restructuring up the tree, maintaining heights is O(log n)!
23
(2,4) Trees"
2 5 7 10 14
Multi-Way Search Tree"
• A multi-way search tree is an ordered tree such that !
– Each internal node has at least two children and stores d -1
key-element items (ki, oi), where d is the number of children !
– For a node with children v1 v2 … vd storing keys k1 k2 … kd-1!
• keys in the subtree of v1 are less than k1
• keys in the subtree of vi are between ki-1 and ki (i = 2, …, d - 1)
• keys in the subtree of vd are greater than kd-1
– The leaves store no items and serve as placeholders!
11 24
2 6 8 15 27 32
30
25
Multi-Way Inorder Traversal"
• We can extend the notion of inorder traversal from binary trees
to multi-way search trees!
• Namely, we visit item (ki, oi) of node v between the recursive
traversals of the subtrees of v rooted at children vi and vi + 1
• An inorder traversal of a multi-way search tree visits the keys in
increasing order!
11 24
8 12
2 6 8 15 27 32
2 4 6 10 14 18
30
1 3 5 7 9 11 13 16 19
15 17
26
Multi-Way Searching"
• Similar to search in a binary search tree!
• A each internal node with children v1 v2 … vd and keys k1 k2 … kd-1!
– k = ki (i = 1, …, d - 1): the search terminates successfully
– k < k1: we continue the search in child v1
– ki-1 < k < ki (i = 2, …, d - 1): we continue the search in child vi
– k > kd-1: we continue the search in child vd!
• Reaching an external node terminates the search unsuccessfully!
• Example: search for 30!
11 24
2 6 8 15 27 32
30
27
(2,4) Trees"
• A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-way
search with the following properties!
– Node-Size Property: every internal node has at most four children!
– Depth Property: all the external nodes have the same depth!
• Depending on the number of children, an internal node of a (2,4)
tree is called a 2-node, 3-node or 4-node!
10 15 24
2 8 12 18 27 32
28
Height of a (2,4) Tree"
• Theorem: A (2,4) tree storing n items has height O(log n)
!Proof:!
– Let h be the height of a (2,4) tree with n items!
– Since there are at least 2i items at depth i = 0, … , h - 1 and no
items at depth h, we have
! ! n ≥ 1 + 2 + 4 + … + 2h-1 = 2h - 1
– Thus, h ≤ log (n + 1)
• Searching in a (2,4) tree with n items takes O(log n) time!
depth items
0 1
1 2
h-1 2h-1
h 0
29
Insertion"
• We insert a new item (k, o) at the parent v of the leaf reached by
searching for k
– We preserve the depth property but !
– We may cause an overflow (i.e., node v may become a 5-node)!
• Example: inserting key 30 causes an overflow!
10 15 24
v
2 8 12 18 27 32 35
10 15 24
v
2 8 12 18 27 30 32 35
30
Overflow and Split"
• We handle an overflow at a 5-node v with a split operation:!
– let v1 … v5 be the children of v and k1 … k4 be the keys of v!
– node v is replaced nodes v' and v"
• v' is a 3-node with keys k1 k2 and children v1 v2 v3
• v" is a 2-node with key k4 and children v4 v5
– key k3 is inserted into the parent u of v (a new root may be created)!
• The overflow may propagate to the parent node u!
u u
15 24 15 24 32
v v' v"
12 18 27 30 32 35 12 18 27 30 35
v1 v2 v3 v4 v5 v1 v2 v3 v4 v5
31
Analysis of Insertion"
Algorithm insert(k, o) • Let T be a (2,4) tree with
n items!
– Tree T has O(log n)
1. We search for key k to locate the height !
insertion node v
– Step 1 takes O(log n)
time because we visit
2. We add the new entry (k, o) at node v O(log n) nodes!
– Step 2 takes O(1) time!
– Step 3 takes O(log n)
3. while overflow(v)
time because each split
if isRoot(v) takes O(1) time and we
create a new empty root above v perform O(log n) splits!
• Thus, an insertion in a
v ← split(v)
(2,4) tree takes O(log n)
time!
32
Deletion"
• We reduce deletion of an entry to the case where the item is at the
node with leaf children!
• Otherwise, we replace the entry with its inorder successor (or,
equivalently, with its inorder predecessor) and delete the latter entry!
• Example: to delete key 24, we replace it with 27 (inorder successor)!
10 15 24
2 8 12 18 27 32 35
10 15 27
2 8 12 18 32 35
33
Underflow and Fusion"
• Deleting an entry from a node v may cause an underflow, where
node v becomes a 1-node with one child and no keys!
• To handle an underflow at node v with parent u, we consider two
cases!
• Case 1: the adjacent siblings of v are 2-nodes!
– Fusion operation: we merge v with an adjacent sibling w and move an
entry from u to the merged node v'!
– After a fusion, the underflow may propagate to the parent u
u u
9 14 9
w v v'
2 5 7 10 2 5 7 10 14
34
Underflow and Transfer"
• Case 2: an adjacent sibling w of v is a 3-node or a 4-node!
– Transfer operation:!
! !1. we move a child of w to v !
! !2. we move an item from u to v
3. we move an item from w to u!
– After a transfer, no underflow occurs!
u u
4 9 4 8
w v w v
2 6 8 2 6 9
35
Analysis of Deletion"
• Let T be a (2,4) tree with n items!
– Tree T has O(log n) height !
• In a deletion operation!
– We visit O(log n) nodes to locate the node from
which to delete the entry!
– We handle an underflow with a series of O(log n)
fusions, followed by at most one transfer!
– Each fusion and transfer takes O(1) time!
• Thus, deleting an item from a (2,4) tree takes
O(log n) time!
36
References"
• Chapter 10: Data structures and
Algorithms by Goodrich and Tamassia. !
37