1 / 33

Properties of AVL Tree

Properties of AVL Tree. The height of an AVL tree with n nodes is O(log n ) For every value of n , n  0, there exists an AVL tree An n -node AVL search tree can be searched in O(height) = O(log n ) time

tehya
Download Presentation

Properties of AVL Tree

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. Properties of AVL Tree • The height of an AVL tree with n nodes is O(log n) • For every value of n, n 0, there exists an AVL tree • An n-node AVL search tree can be searched in O(height) = O(log n) time • A new node can be inserted into an n-node AVL search tree so that the result is an n+1 node AVL tree and insertion can be done in O(log n) time • A node can be deleted from an n-node AVL search tree, n>0, so that the result is an n-1 node AVL tree and deletion can be done in O(log n) time

  2. Balancing Operations • We prefer to use balancing operations after each add or remove element operation • Semantics of balancing operations • Right rotation • Left rotation • Rightleft rotation • Leftright rotation

  3. Balancing Operations • Semantics of Right Rotation A. Make the left child of the root the new root B. Make former root the right child of the new root C. Make right child of the former left child of the former root the new left child of the former root 13 7 7 7 7 15 5 13 5 13 5 13 5 10 3 15 3 15 3 10 15 10 10 Initial Tree Step C 3 Step A Step B

  4. Balancing Operations • Semantics of Left Rotation A. Make the right child of the root the new root B. Make former root the left child of the new root C. Make left child of the former right child of the former root the new right child of the former root 5 10 10 10 3 10 5 13 5 13 5 13 7 13 3 15 3 15 3 7 15 7 7 Initial Tree Step A Step B Step C 15

  5. Balancing Operations • Semantics of Rightleft Rotation A. Right rotation around right child of root B. Left rotation around root 5 5 10 3 13 3 10 5 13 10 15 7 13 3 7 15 7 15 Initial Tree After Right Rotation After Left Rotation

  6. Balancing Operations • Semantics of Leftright Rotation A. Left rotation around left child of root B. Right rotation around root 13 13 7 5 15 7 15 5 13 3 7 5 10 3 10 15 10 3 Initial Tree After Left Rotation After Right Rotation

  7. Balance Factor • AVL trees are normally represented using the linked representation • To facilitate insertion and deletion, a balance factor (bf) is associated with each node • The balance factor bf(x) of a node x is defined asheight(xleftChild) – height(xrightChild) • Balance factor of each node in an AVL tree must be –1, 0, or 1

  8. Balanced and unbalanced BST 1 4 2 2 5 3 1 3 4 4 Is this “balanced”? 5 2 6 6 1 3 5 7 7 AVL Trees

  9. Perfect Balance • Want a complete tree after every operation • tree is full except possibly in the lower right • This is expensive • For example, insert 2 in the tree on the left and then rebuild as a complete tree 6 5 Insert 2 & complete tree 4 9 2 8 1 5 8 1 4 6 9 AVL Trees

  10. AVL - Good but not Perfect Balance • AVL trees are height-balanced binary search trees • Balance factor of a node • height(left subtree) - height(right subtree) • An AVL tree has balance factor calculated at every node • For every node, heights of left and right subtree can differ by no more than 1 • Store current heights in each node AVL Trees

  11. -1 10 1 1 7 40 0 0 1 -1 3 8 30 45 0 0 0 -1 0 1 5 20 35 60 0 25 AVL Tree with Balance Factors • Is this an AVL tree? • What is the balance factor for each node in this AVL tree? • Is this an AVL search tree?

  12. Searching an AVL Search Trees • What would be the search time complexity? • O(log n)

  13. Single and Double Rotations • Single rotations: the transformations done to correct LL and RR imbalances • Double rotations: the transformations done to correct LR and RL imbalances • The transformation to correct LR imbalance can be achieved by an RR rotation followed by an LL rotation • The transformation to correct RL imbalance can be achieved by an LL rotation followed by an RR rotation (do Exercise 15.13)

  14. A 15 9 Left Rotation 22 4 9 15 12 12 4 22 Left Rotation Definition • In a binary search tree, pushing a node A down and to the left to balance the tree. • A's right child replaces A, and the right child's left child becomes A's right child. Animated rotation example: http://www.cs.queensu.ca/home/jstewart/applets/bst/bst-rotation.html

  15. A 9 15 Right Rotation 4 22 15 9 12 12 22 4 Right Rotation Definition • In a binary search tree, pushing a node A down and to the right to balance the tree. • A's left child replaces A, and the left child's right child becomes A's left child.

  16. Example of Insertions in an AVL Tree 0 -1 20 20 1 0 1 -1 10 30 10 30 0 0 0 -1 0 0 5 35 5 35 25 25 0 40 Now Insert 45 AVL Trees

  17. Single rotation (outside case) -2 -1 20 20 1 -2 1 -1 10 30 10 30 0 -2 0 0 0 0 5 35 5 40 25 25 0 0 35 45 40 -1 Imbalance 0 45 Now Insert 34 AVL Trees

  18. Double rotation (inside case) -2 -1 20 20 1 -2 1 0 10 30 10 35 +1 0 0 0 0 -1 5 Imbalance 40 5 40 25 30 0 25 34 1 0 45 35 45 0 Insertion of 34 0 34 AVL Trees

  19. 2 2 2 3 3 4 4 2 1 1 2 1 3 3 Fig 1 3 5 3 Fig 4 Fig 2 Fig 3 1 Fig 5 Fig 6 An Extended Example Insert 3,2,1,4,5,6,7, 16,15,14 Single rotation Single rotation

  20. 2 4 2 4 4 Fig 8 Fig 7 6 5 5 7 6 3 7 3 3 1 2 2 1 2 4 6 5 4 5 6 Fig 10 Fig 9 5 Fig 11 3 3 1 1 1 Single rotation Single rotation

  21. 4 4 4 7 15 7 16 3 16 3 3 16 2 2 2 Fig 12 Fig 13 15 6 6 6 5 7 Fig 14 5 5 1 1 1 Double rotation

  22. x C y 3 A z 8 4 3.5 1 4 B 3.5 5 5 8 3 3 4 1 AVL Tree 5 8 Insert 3.5 After Rotation 1

  23. 4 4 6 14 15 16 15 14 3 3 16 2 2 Fig 15 Fig 16 7 6 5 7 5 1 1 Double rotation

  24. -1 -1 0 1 0 1 0 0 1 -1 0 1 0 0 0 0 0 0 0 no rotation required An Insertion #'s are balance factors

  25. -1 -1 1 0 0 0 2 0 0 0 1 0 1 0 1 0 0 0 0 0 0 simple right rotation required Another Insertion 0

  26. 0 -1 -1 -1 -2 -1 0 -1 0 0 1 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 simple left rotation required Another Insertion 0

  27. double rotation needed a. right rotation around right subtree of the unbalanced subtree b. left rotation around root of the unbalanced subtree 0 -1 -2 1 1 Another Insertion -1 1 0 0 0 -1 0 0 0

  28. Rotation case 1

  29. Rotation case 2

  30. Rotation case 3

  31. Rotation case 4

  32. Pros and Cons of AVL Trees • Arguments for AVL trees: • Search is O(log N) since AVL trees are always balanced. • Insertion and deletions are also O(logn) • The height balancing adds no more than a constant factor to the speed of insertion. • Arguments against using AVL trees: • Difficult to program & debug; more space for balance factor. • Asymptotically faster but rebalancing costs time. • Most large searches are done in database systems on disk and use other structures (e.g. B-trees). AVL Trees

More Related