280 likes | 335 Views
Dive into the world of AVL trees - self-balancing binary search trees that streamline data organization, insertion, and search operations. Learn how balance factors and rotations maintain equilibrium, and explore practical applications in memory management and compiler design.
E N D
AVL TREES Introduction Applications Balance Factor Rotations Deletion Example
INTRODUCTION • AVL trees are self balancing binary search trees (BST) • To determine whether the tree is balanced, the height of left and right sub tree is checked for each node in the tree. • BST cannot control the order in which data comes for insertion • search in BST highly depends on height of the tree • AVL trees also cannot control the order in which data comes for insertion, but they can re-arrange data in tree • searching times can be reduced • By rearrangement, AVL trees reduce the height of tree 0
APPLICATIONS • AVL trees are used for frequent insertion. • It is used in Memory management subsystem • Compiler Design • AVL trees are applied in the following situations: • There are few insertion and deletion operations • Short search time is needed • Input data is sorted or nearly sorted
BALANCE FACTOR • Balance factor of a node • height(left subtree) - height(right subtree) Tree A (AVL) Tree B (AVL) BF=2-1=1 BF=2-2=0 2 6 6 BF=1-1=0 BF=1-0=1 1 4 9 4 9 0 0 0 0 0 1 5 1 5 8 BF=0-0=0 BF=0-0=0
BALANCE FACTOR Tree A Tree B BF=3-1=2 BF= 2-4= -2 8 8 BF=1-2= -1 0 0 BF= 3-0=3 4 9 4 9 0 0 1 6 1 5 7 0 BF=2-0=2 0 BF=1-0=1 1 -1 5 0 5 0
SINGLE ROTATION • LL means when new node inserted in left of left subtree – rotate right • RR means when new node inserted in right of right subtree – rotate left
DOUBLE ROTATION Left Right Rotation • new node is inserted in right of left sub tree • rotate left • rotate right Right Left Rotation • new node is inserted in left of right sub tree • rotate right • rotate left
DELETION • Similar but more complex than insertion • Deletion of a node x from an AVL tree requires the same basic ideas, including single and double rotations, that are used for insertion. • Steps: • To delete x • If X is a leaf Remove X • If X has one child Replace X by its child • If X has two children Replace X with its Largest Left Child (Left Sub Tree)
AVL Tree Example: • Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 11 17 7 53 4
AVL Tree Example: • Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 7 17 4 11 53 13
AVL Tree Example: • Now insert 12 14 7 17 4 11 53 13 12
AVL Tree Example: • Now insert 12 14 7 17 4 11 53 12 13
AVL Tree Example: • Now the AVL tree is balanced. 14 7 17 4 12 53 11 13
AVL Tree Example: • Now insert 8 14 7 17 4 12 53 11 13 8
AVL Tree Example: • Now insert 8 14 7 17 4 11 53 8 12 13
AVL Tree Example: • Now the AVL tree is balanced. 14 11 17 7 12 53 4 8 13
AVL Tree Example: • Now remove 53 14 11 17 7 12 53 4 8 13
AVL Tree Example: • Now remove 53, unbalanced 14 11 17 7 12 4 8 13
AVL Tree Example: • Balanced! 11 7 14 4 8 12 17 13
AVL Tree Example: • Remove 11, replace it with the largest in its left branch 8 7 14 4 12 17 13
AVL Tree Example: • Remove 8, unbalanced 7 4 14 12 17 13
AVL Tree Example: • Remove 8, unbalanced 7 4 12 14 13 17
AVL Tree Example: • Balanced!! 12 7 14 4 13 17
Remove 24 and 20 from the AVL tree. 13 13 10 24 10 20 7 20 30 7 15 30 15 25 36 25 36 13 13 10 30 10 15 7 15 36 7 30 25 25 36