avl treesgoodrich/teach/cs260p/notes/avltrees.pdfavl tree definition avl trees are rank-balanced...
TRANSCRIPT
© 2015 Goodrich and Tamassia AVL Trees 1
AVL Trees 6
3 8
4
v
z
Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
© 2015 Goodrich and Tamassia AVL Trees 2
AVL Tree Definition AVL trees are rank-balanced trees. The rank, r(v), of each node, v, is its height. Rank-balance rule: An AVL Tree is a binary search tree such that for every internal node v of T, the heights (ranks) 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
An example of an AVL tree where the ranks are shown next to the nodes
© 2015 Goodrich and Tamassia AVL Trees 3
Height of an AVL Tree Fact: The height of an AVL tree storing n keys is O(log n). Proof (by induction): Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h.
We easily see that n(1) = 1 and n(2) = 2 For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2in(h-2i)
Solving the base case we get: n(h) > 2 h/2-1
Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n)
3
4 n(1)
n(2)
© 2015 Goodrich and Tamassia AVL Trees 4
Insertion Insertion is as in a binary search tree Always done by expanding an external node. Example:
44
17 78
32 50 88
48 62
54 w
b=x
a=y
c=z
44
17 78
32 50 88
48 62
before insertion
after insertion
© 2015 Goodrich and Tamassia AVL Trees 5
Trinode Restructuring Let (a,b,c) be the inorder listing of x, y, z Perform 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
T3 b=x
c=y a=z
T0 T1 T2 T3
Double rotation around c and a
Single rotation around b
© 2015 Goodrich and Tamassia AVL Trees 6
Insertion Example, continued
88
44
17 78
32 50
48 62
2
5
1
1
3
4
2
1
54
1
T0 T2
T3
x
y
z
2
3
4
5
67
1
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.
88
44
17 78 32 50
48
62 2
4
1 1
2 2
3
1 54 1
T 0 T 1
T 2
T 3
x y z
unbalanced...
...balanced 1
23
4
5
6
7
T 1
© 2015 Goodrich and Tamassia AVL Trees 7
Restructuring (as Single Rotations) Single Rotations:
T0T1
T2T3
c = xb = y
a = z
T0 T1 T2T3
c = xb = y
a = zsingle rotation
T3T2
T1T0
a = xb = y
c = z
T0T1T2T3
a = xb = y
c = zsingle rotation
© 2015 Goodrich and Tamassia AVL Trees 8
Restructuring (as Double Rotations) double rotations:
double rotationa = z
b = xc = y
T0T2
T1T3 T0
T2T3T1
a = zb = x
c = y
double rotationc = z
b = xa = y
T0T2
T1T3 T0
T2T3 T1
c = zb = x
a = y
© 2015 Goodrich and Tamassia
Pseudo-code Insertion.
AVL Trees 9
© 2015 Goodrich and Tamassia
Pseudo-code Rebalance at a node violating the rank rule.
AVL Trees 10
© 2015 Goodrich and Tamassia AVL Trees 11
Removal 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
17
78 32 50
88 48
62
54
44
17
78 50
88 48
62
54
before deletion of 32 after deletion
© 2015 Goodrich and Tamassia AVL Trees 12
Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height We perform a trinode restructuring to restore balance at z As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached
44
17
78 50
88 48
62
54
w
c=x
b=y
a=z
44
17
78
50 88
48
62
54
© 2015 Goodrich and Tamassia
Pseudo-code Removal
AVL Trees 13
© 2015 Goodrich and Tamassia AVL Trees 14
AVL Tree Performance AVL tree storing n items n The data structure uses O(n) space
n A single restructuring takes O(1) time w using a linked-structure binary tree
n Searching takes O(log n) time w height of tree is O(log n), no restructures needed
n Insertion takes O(log n) time w initial find is O(log n) w restructuring up the tree, maintaining heights is O(log n)
n Removal takes O(log n) time w initial find is O(log n) w restructuring up the tree, maintaining heights is O(log n)