AVL Trees 1
AVL Trees
6 3 8 4
AVL Trees AVL Tree Definition (9.2) AVL trees are 4 44 balanced. - - PDF document
1 8 6 z 4 3 v AVL Trees AVL Trees AVL Tree Definition (9.2) AVL trees are 4 44 balanced. 2 3 An AVL Tree is a 17 78 binary search tree 1 2 1 88 32 50 such that for every 1 1 internal node v of T, 48 62 the heights of
AVL Trees 1
6 3 8 4
2
88 44 17 78 32 50 48 62 2 4 1 1 2 3 1 1
An example of an AVL tree where the heights are shown next to the nodes
3
n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2in(h-2i)
3 4 n(1) n(2)
4
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
5
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 6 7
1
88 44 17 78 32 50 48 62 2 4 1 1 2 2 3 1 54 1
T0 T1 T2 T3 x y z
5 6 7
T1
6
let (a,b,c) be an 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
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)
7
44 17 78 32 50 88 48 62 54 44 17 78 50 88 48 62 54
before deletion of 32 after deletion
8
Let z be the first unbalanced node encountered while traveling 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. (!tie!) We perform restructure(x) 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
9
using a linked-structure binary tree
height of tree is O(log n), no restructures needed
initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n)
initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n)