© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
-
Upload
percival-jones -
Category
Documents
-
view
217 -
download
3
Transcript of © 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Binary Search Trees 1© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
CSC 212
Lecture 18: Binary and AVL Trees
Binary Search Trees 2© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Ordered DictionariesKeys are assumed to come from a total order.New operations: first(): first entry in the dictionary ordering last(): last entry in the dictionary ordering successor(k): entry with smallest key
greater than or equal to k; increasing order predecessor(k): entry with largest key less
than or equal to k; decreasing order
Binary Search Trees 3© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Binary Search (§ 8.3.3)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)
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
0
0
0
0
ml h
ml h
ml h
lm h
Binary Search Trees 4© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Search Tables
A search table is a dictionary implemented by means of a sorted sequence
Store the items of the dictionary in an array-based sequence, sorted by key
Performance: find takes O(log n) time, using binary search insert takes O(n) time since in the worst case we have
to shift n2 items to make room for the new item remove take O(n) time since in the worst case we have
to shift n2 items to compact the items after the removal
Lookup table effective only for dictionaries of small size or for dictionaries on which searches are the most common operations
Binary Search Trees 5© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Binary Search Trees (§ 9.1)A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property:
Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w)
External nodes do not store items
An inorder traversal of a binary search trees visits the keys in increasing order
6
92
41 8
Binary Search Trees 6© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Search (§ 9.1.1)To search for a key k, we trace a downward path starting at the rootThe next node visited depends on the outcome of the comparison of k with the key of the current nodeIf we reach a leaf, the key is not found and we return nullExample: find(4):
Call TreeSearch(4,root)
Algorithm TreeSearch(k, v)if T.isExternal (v)
/* throw exception */if k key(v)
return TreeSearch(k, T.left(v))else if k key(v)
return velse { k key(v) }
return TreeSearch(k, T.right(v))
6
92
41 8
Binary Search Trees 7© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Search (§ 9.1.1)Example: find(5):
Call TreeSearch(5,root)
Algorithm TreeSearch(k, v)if T.isExternal (v)
/* throw exception */if k key(v)
return TreeSearch(k, T.left(v))else if k key(v)
return velse { k key(v) }
return TreeSearch(k, T.right(v))
6
92
41 8
Binary Search Trees 8© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
InsertionTo perform operation insert(k, o), we search for key k (using TreeSearch)Assume k is not already in the tree, and let let w be the leaf reached by the searchWe insert k at node w and expand w into an internal nodeExample: insert(5, o)
6
92
41 8
6
92
41 8
5
w
w
Binary Search Trees 9© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
DeletionTo perform operation remove(k), we search for key kAssume key k is in the tree, and let let v be the node storing kIf node v has a leaf child w, we remove v and w from the tree with operation removeExternal(w), which removes w and its parentExample: remove(4)
6
92
41 8
5
vw
6
92
51 8
Binary Search Trees 10© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Deletion (cont.)We consider the case where the key k to be removed is stored at a node v whose children are both internal
we find the internal node w that follows v in an inorder traversal
we copy key(w) into node v we remove node w and its
left child z (which must be a leaf) by means of operation removeExternal(z)
Example: remove(3)
3
1
8
6 9
5
v
w
z
2
5
1
8
6 9
v
2
Binary Search Trees 11© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
PerformanceConsider 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
Binary Search Trees 12© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Your Turn
Insert 4, 2, 6, 1, 3, 5, 7 into a treeThen remove nodes in this order:4, 2, 6, 1, 3, 5, 7
Binary Search Trees 13© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
AVL Tree Definition (§ 9.2)
AVL trees are balanced.An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1.
88
44
17 78
32 50
48 62
2
4
1
1
2
3
1
1
Example AVL tree where the heights are shown next to the nodes
Binary Search Trees 14© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Insertion in an AVL TreeInsertion is as in a binary search treeAlways done by expanding an external node.Consider insert(54, 0):
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
before insertion after insertion
Binary Search Trees 15© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Trinode Restructuringlet (a,b,c) be an inorder listing of x, y, zperform the rotations needed to make b the topmost node of the three
b=y
a=z
c=x
T0
T1
T2 T3
b=y
a=z c=x
T0 T1 T2 T3
c=y
b=x
a=z
T0
T1 T2
T3b=x
c=ya=z
T0 T1 T2 T3
case 1: single rotation(a left rotation about a)
case 2: double rotation(a right rotation about c, then a left rotation about a)
(other two cases are symmetrical)
Binary Search Trees 16© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Insertion Example, continued
88
44
17 78
32 50
48 62
2
5
1
1
3
4
2
1
54
1
T0T2
T3
x
y
z
2
3
4
5
67
1
88
44
17
7832 50
48
622
4
1
1
2 2
3
154
1
T0 T1
T2
T3
x
y z
unbalanced...
...balanced
1
2
3
4
5
6
7
T1
Binary Search Trees 17© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Restructuring (as Single Rotations)
Single Rotations:
T0T1
T2
T3
c = xb = y
a = z
T0 T1 T2
T3
c = xb = y
a = zsingle rotation
T3T2
T1
T0
a = xb = y
c = z
T0T1T2
T3
a = xb = y
c = zsingle rotation
Binary Search Trees 18© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Restructuring (as Double Rotations)
double rotations:
double rotationa = z
b = xc = y
T0T2
T1
T3 T0
T2T3T1
a = zb = x
c = y
double rotationc = z
b = xa = y
T0T2
T1
T3 T0
T2T3 T1
c = zb = x
a = y
Binary Search Trees 19© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Removal in an AVL TreeRemoval 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.Remove(32):
44
17
7832 50
8848
62
54
44
17
7850
8848
62
54
before deletion of 32 after deletion
Binary Search Trees 20© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Rebalancing after a Removal
Let z be the first unbalanced node encountered while travelling up tree after removing a node. Let y be the child of z with the larger height, and x be the child of y with the larger height.restructure(x) restores balance at z.Restructuring may unbalance node higher in the tree, so must continue checking for balance (and restructuring where needed) until root is reached44
17
7850
8848
62
54
w
c=x
b=y
a=z
44
17
78
50 88
48
62
54
Binary Search Trees 21© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Running Times for AVL Trees
Single restructure is O(1) Work is constant for height of tree
find is O(log n) height of tree is O(log n)
insert is _____________ initial find is O(log n) Restructuring up the tree – could do at most
_______________________ workremove is _______________ initial find is O(log n) Restructuring up the tree – could do at most
_______________________ work
Binary Search Trees 22© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Your Turn
Insert 1, 2, 3, 4, 5, 6, 7, 8 into an AVL treeThen remove 1, 2, 3, 4, 5, 6, 7, 8 into AVL tree
Binary Search Trees 23© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia
Daily Quiz
I have a “friend” who claims that the order nodes are entered into an AVL tree does not matter, the end tree is always the same Provide a small example proving my
“friend” wrong