1 / 26

AVL TREES

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.

ellisl
Télécharger la présentation

AVL TREES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 0

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

  5. 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

  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. 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

  8. 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)

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

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

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

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

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

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

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

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

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

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

  19. AVL Tree Example: • Balanced! 11 7 14 4 8 12 17 13

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

  21. AVL Tree Example: • Remove 8, unbalanced 7 4 14 12 17 13

  22. AVL Tree Example: • Remove 8, unbalanced 7 4 12 14 13 17

  23. AVL Tree Example: • Balanced!! 12 7 14 4 13 17

  24. 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

More Related