1 / 26

Recitation 9: Tree Rotations and AVL Trees

This recitation covers the concepts of Tree Rotations and AVL Trees, including their definition, properties, and balancing techniques. It also discusses the benefits of using AVL Trees over Binary Search Trees and HashSets in certain scenarios. Tree Rotations code is not required for the exam.

allenen
Download Presentation

Recitation 9: Tree Rotations and 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. Recitation 9 Tree Rotations and AVL Trees

  2. BSTs Review: Binary Search Tree (BST) 1 ideal case 4 2 2 5 3 worst case: O(n) lookup O(n) insertion O(n) deletion 4 1 3 5 x < 4 x > 4

  3. BSTs Make BSTs balanced! Balanced BST worst case: O(log n) lookup O(log n) insertion O(log n) deletion 4 2 5 If a BST becomes unbalanced, we can rebalance it in O(log n). 1 3

  4. BSTs Review: definition of Height public static int getHeight(TreeNode t) { if (t == null) return -1; return 1 + Math.max(getHeight(t.left), getHeight(t.right)); } length of the longest path from a node to a leaf

  5. BSTs Definition of Balanced public static boolean isBalanced(TreeNode t) { return t == null || Math.abs(getHeight(t.left) - getHeight(t.right)) <= 1 && isBalanced(t.left) && isBalanced(t.right); } A tree is balanced if each of its subtrees is balanced and their heights differ by at most 1

  6. BSTs isBalanced: Recursion needed! 0 -1 1 All subtrees need to be balanced! 2 -2 3 -3 4 -4

  7. Tree Rotations

  8. Tree Rotations Notation k+2 x Inorder traversal: A x B y C Recall that the BST inorder traversal gives sorted order. k+1 k A y k k B C A subtree of height k

  9. Tree Rotations Rotations: Used to balance a BST k+2 The blue pointers are the only ones that change. k+2 x y k+1 k k k+1 A y C x k k k k B C A B Inorder traversals are the same

  10. Tree Rotations Rotations example 3 2 2 5 5 0 2 1 1 0 2 6 3 6 Rotate 0 1 0 0 3 2 4 unbalanced node 0 4

  11. Tree Rotations Rebalancing unbalanced node k+3 k+2 y x k+2 k+1 k k+1 A y C x k k+1 k k B C A B

  12. Tree Rotations Problem: Rotating a Zig-Zag! 3 3 unbalanced node 5 5 2 0 2 0 Rotate 4 2 6 6 left child taller 1 1 4 2 Zig-Zag: taller children on opposite sides right child taller 0 We get the opposite Zig-Zag! 0 3 3

  13. Tree Rotations Double rotate 3 3 unbalanced node 5 5 2 0 0 2 4 6 4 6 1 1 2 1st Rotation 3 still unbalanced node 0 0 2 3

  14. Tree Rotations Double rotate 3 5 2 5 2 0 4 6 1 0 3 6 1 2nd Rotation 3 0 0 2 4 0 2

  15. Tree Rotations Rebalancing with double rotate k+3 k+3 x x 1st Rotation k+2 k+2 k k A z A y k+1 k+1 k y z k B D k k k k C D C B

  16. Tree Rotations Rebalancing with double rotate k+3 k+2 y x 2nd Rotation k+2 k k+1 k+1 A y z x k+1 k z k B k k k A C D B k k C D

  17. Tree Rotations Summary of Rotations Double rotation necessary Only single rotation necessary Balanced! y x x A z A y z x y z B D B C D A C D C B Symmetry holds for the other cases

  18. Tree Rotations Question: What is the resulting tree? 3 5 0 2 2 6 0 1 1 3 0 4

  19. Tree Rotations Question: What is the resulting tree? 3 3 5 5 0 2 2 0 2 6 3 6 0 1 1 0 1 1st Rotation 3 2 4 0 0 4 1

  20. Tree Rotations Question: What is the resulting tree? 3 2 5 3 1 1 2 0 2 5 3 6 1 0 0 0 0 2nd Rotation 1 4 6 2 4 0 1

  21. AVL Trees

  22. AVL Trees AVL Trees Named after its two Soviet inventors, GeorgyAdelson-Velsky and E. M. Landis, who described AVL tres in a paper in 1962. First invention of self-balancing BSTs. Later: red-black trees, splay trees, and others

  23. AVL Trees AVL Tree AVL Tree: self-balancing BST AVL invariant: the height difference between its left and right children is at most 1 Lookup works the same as a normal BST lookup 4 2 5 worst case: O(log n) lookup O(log n) insertion O(log n) deletion 1 3

  24. AVL Trees Inserting an element Insert like a normal BST and if the AVL invariant is broken, do a single or double rotation to fix it Localizing the problem: • Imbalance will occur only on the path from the root to the newly inserted node • Rebalancing should occur at the deepest node • Must search for possible imbalance all the way up to root insert(E elem) 4 2 5 1 3 https://www.cs.usfca.edu/~galles/visualization/AVLtree.html

  25. AVL Trees Why use AVL Trees? If HashSets have a lookup of expected O(1), why use BSTs with an expected lookup time of O(log n)? Depends on the problem: • Binary Search Trees are great at keeping elements in sorted order. • Key Ranges: How many words in the set start with k and end in z? 3. findPredecessor(E elem) and findSuccessor(E elem) • O(log n) for AVL Tree, expected case O(n) for HashSet 4. Better worst case lookup and insertion times

  26. Prelim Information • Tree Rotations will not be tested on Prelim 2 • You don’t need to be able to write Tree Rotations code but can find it online if interested

More Related