AVL Trees
Section 4.4
39
AVL Trees Section 4.4 39 AVL Tree A balanced tree Ensures log - - PowerPoint PPT Presentation
AVL Trees Section 4.4 39 AVL Tree A balanced tree Ensures log running time for search, insert, and delete A simple and relaxed definition for balance log log : Too restrictive 40 Balanced Tree Height(null) =
Section 4.4
39
40
41
4 3 5 2 6 1 7
Height(root->left) = Height(root->right)
Too weak Height(null) = -1
42
4 2 6 1 7
Height(node->left) = Height(node->right)
Could be impossible to satisfy
3 5
Height(null) = -1
43
5 3 6 2 7 1 4
πΌπππβπ’ ππππ β ππππ’ β πΌπππβπ’ ππππ β π ππβπ’ β€ 1
Height(null) = -1
44
5 2 7 1 6 3 4
Is this an AVL Tree? Yes
45
5 4 6 2 7 1 3
Is this an AVL Tree? No
Violating node
46
Is this an AVL Tree?
Yes
47
48
49
Violating Node Case 1 Case 2 Case 3 Case 4 + New Node Cases 1 and 4 are symmetric Cases 2 and 3 are symmetric
50
k2 k1 Violating Node X Y Z Status upon insertion in X k2 is in violation k2 k1 X Y Z Rotation Is this a BST? Is this an AVL Tree? Yes Yes
51
k2 k1 Violating Node X Y Z Status upon insertion in Y k2 is in violation k2 k1 X Y Z Single Rotation Is this a BST? Is this an AVL Tree? Yes No
52
k3 k1 Violating Node A B D k2 C k2 k1 A B C k3 D