CE 221 Data Structures and Algorithms
Chapter 4: Trees (AVL Trees)
Text: Read Weiss, §4.4
1 Izmir University of Economics
Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: - - PowerPoint PPT Presentation
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, 4.4 Izmir University of Economics 1 AVL Trees An AVL (Adelson-Velskii and Landis) tree is a binary search tree with a balance condition . It must be
1 Izmir University of Economics
Izmir University of Economics 2
Izmir University of Economics 3
Izmir University of Economics 4
Izmir University of Economics 5
h h h F 2 5 1 2 5 1 5 1 ) (
Izmir University of Economics 6
2 2 2 5 1 5 1 ) ( 1 1 2 2 5 1 5 1 ) ( 1 ) 2 ( ) ( 1 2 5 1 5 1 ) ( 2 5 1 2 5 1 5 1 ) (
h h S h h S h F h S h h F h h h F ) (log 327 . ) 2 log( 44 . 1 2 5 1 log * 2 5 log 2 2 5 1 log ) 2 log( 2 5 1 log ) 2 ( 5 log 2 1 ) 2 log( 2 2 5 1 5 1 2 2 2 2 5 1 5 1 ) ( n O h h N h N h N h N h h S N
Izmir University of Economics 7 6
1) An insertion into left subtree of the left child of α. (LL) 2) An insertion into right subtree of the left child of α. (LR) 3) An insertion into left subtree of the right child of α. (RL) 4) An insertion into right subtree of the right child of α. (RR)
Izmir University of Economics 8
Izmir University of Economics 9
Izmir University of Economics 10
11 Izmir University of Economics
Izmir University of Economics 12
Izmir University of Economics 13
Insert 6. Balance problem at the root. So a single rotation is performed. Finally, Insert 7 causing another rotation.
Izmir University of Economics 14
Izmir University of Economics 15
Izmir University of Economics 16
Izmir University of Economics 17
Izmir University of Economics 18
19 Izmir University of Economics
Izmir University of Economics 20
Izmir University of Economics 21
Izmir University of Economics 22
Izmir University of Economics 23
Izmir University of Economics 24
Izmir University of Economics 25
/* This function can be called only if k2 has a left */ /* child. Perform a rotate between k2 and its left */ /* child k1. Update heights, then return new root */
Izmir University of Economics 26
/* This function can be called only if k1 has a right */ /* child. Perform a rotate between k1 and its right */ /* child k2. Update heights, then return new root */ private AvlNode<AnyType> rotateWithRightChild(AvlNode<AnyType> k1) { AvlNode<AnyType> k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = Math.max(height(k1.left), height(k1.right))+1; k2.height = Math.max(height(k2.right), k1.height)+1; return k2; }
Izmir University of Economics 27
/* Double LR = Single RR (α->left)+ Single LL (α ) */ /* This function can be called only if k3 has a left */ /* child and k3's left child k1 has a right child k2 */ /* single rotation between k1 and k2 followed by */ /* single rotation between k3 and k2 */
Izmir University of Economics 28
/* Double RL = Single LL (α->right)+ Single RR (α) */ /* This function can be called only if k1 has a right */ /* child k3 and k1's right child has a left child k2 */ private AvlNode<AnyType> doubleWithRightChild(AvlNode<AnyType> k1) { k1.right = rotateWithLeftChild( k1.right ); return rotateWithRightChild( k1 ); }
Izmir University of Economics 29
Izmir University of Economics 30
Izmir University of Economics 31
Izmir University of Economics 32