Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Applications Balance Factor Rotations Deletion Example

Similar presentations


Presentation on theme: "Introduction Applications Balance Factor Rotations Deletion Example"— Presentation transcript:

1 Introduction Applications Balance Factor Rotations Deletion Example
AVL TREES Introduction Applications Balance Factor Rotations Deletion Example

2 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

3 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 

4 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 1 5 1 5 8 BF=0-0=0 BF=0-0=0

5 BALANCE FACTOR 8 8 4 9 4 9 1 6 1 5 7 5 1 5 Tree A Tree B BF=3-1=2
BF= 3-0=3 4 9 4 9 1 6 1 5 7 BF=2-0=2 BF=1-0=1 5 1 -1 5

6 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

7

8 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

9

10 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)

11 AVL Tree Example: Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 11 17 7 53 4

12 AVL Tree Example: Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 7 17 4 11 53 13

13 AVL Tree Example: Now insert 12 14 7 17 4 11 53 13 12

14 AVL Tree Example: Now insert 12 14 7 17 4 11 53 12 13

15 AVL Tree Example: Now the AVL tree is balanced. 14 7 17 4 12 53 11 13

16 AVL Tree Example: Now insert 8 14 7 17 4 12 53 11 13 8

17 AVL Tree Example: Now insert 8 14 7 17 4 11 53 8 12 13

18 AVL Tree Example: Now the AVL tree is balanced. 14 11 17 7 12 53 4 8 13

19 AVL Tree Example: Now remove 53 14 11 17 7 12 53 4 8 13

20 AVL Tree Example: Now remove 53, unbalanced 14 11 17 7 12 4 8 13

21 AVL Tree Example: Balanced! 11 7 14 4 8 12 17 13

22 AVL Tree Example: Remove 11, replace it with the largest in its left branch 8 7 14 4 12 17 13

23 AVL Tree Example: Remove 8, unbalanced 7 4 14 12 17 13

24 AVL Tree Example: Remove 8, unbalanced 7 4 12 14 13 17

25 AVL Tree Example: Balanced!! 12 7 14 4 13 17

26 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


Download ppt "Introduction Applications Balance Factor Rotations Deletion Example"

Similar presentations


Ads by Google