Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree Rotations and AVL Trees

Similar presentations


Presentation on theme: "Tree Rotations and AVL Trees"— Presentation transcript:

1 Tree Rotations and AVL Trees
Recitation 9 Tree Rotations and AVL Trees

2 Review: Binary Search Tree (BST)
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 Review the concept of Binary Search Tree Ask for the runtimes of lookup, insertion, and deletion. Remind them that a BST can also turn into a linked list if inserted in sorted order, giving O(n) operations x < 4 x > 4

3 Make BSTs balanced! worst case: O(log n) lookup O(log n) insertion
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 However, if we can keep a BST balanced in O(log n), then we can achieve O(log n) operations

4 Review: definition of Height
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 Definition of Balanced
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 Note that the recursion here is necessary! Example on next slide.

6 isBalanced: Recursion needed!
BSTs isBalanced: Recursion needed! -1 1 All subtrees need to be balanced! -2 2 -3 3 4 Explain why the recursion is necessary. Simply because the root node has equally tall subtrees does not mean that the subtrees are balanced. Looking something up in this tree like this with n = 2m+1 nodes takes up to m tests, so its still O(n) -4

7 Tree Rotations

8 Notation x A y B C k+2 Inorder traversal: A x B y C
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 We’re going to introduce a bit of notation here: The triangles represent a balanced subtree. They represent many nodes. All nodes in A will be less than x. All nodes in B will be greater than x and less than y. All nodes in C will be greater than y. The superscripts near the shapes represent the heights of that node. Recall that inorder is Left / Node / Right. In terms of a BST, that means that the inorder traversal will be in sorted order. As long as the inorder traversal is in sorted order, we will have a valid BST.

9 Rotations: Used to balance a BST
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 A y k+1 x C k k k B C k A B Give your students enough time with this slide. These rotations is the basis for keep trees balanced. Both have in order traversal A x B y C. left to right: the root’s right ptr is swapped with its right ptr’s left ptr right to left: the root’s left ptr is swapped with its left ptr’s right ptr Can x ever point to C? Impossible. If x pointed to C and x < y, then C would be positioned to be less than y when every node in C is greater than y. Inorder traversals are the same

10 Rotations example 5 5 2 6 3 6 3 2 4 4 2 3 2 1 2 1 Rotate 1 unbalanced
Tree Rotations Rotations example 2 3 2 5 5 1 2 1 2 6 3 6 Rotate 1 3 2 4 unbalanced node 4 Let’s insert the element 4 to the tree. The heights of nodes with values 5, 2, 3 change, tree no longer balanced at node (2) and node (5). When we have multiple unbalanced nodes, we will always work with the deepest unbalanced node. A single rotate on (2), the unbalanced node, does the job.

11 Rebalancing x y A y x C B C A B unbalanced node k+3 k+2 k+2 k k+1 k+1
Tree Rotations Rebalancing unbalanced node k+3 k+2 x y k+2 k k+1 k+1 A y x C k k+1 k k B C A B This is the abstract case for a single rotation. Here we have C being a little heavier on the right hand side. So, if we rotate x, the deepest unbalanced node, then we’ll have a balanced tree!

12 Problem: Rotating a Zig-Zag!
Tree Rotations Problem: Rotating a Zig-Zag! 3 3 unbalanced node 5 5 2 2 4 6 Rotate 2 6 left child taller 1 1 2 4 right child taller Zig-Zag: taller children on opposite sides We get the opposite Zig-Zag! 3 3 If we apply a rotation to the unbalanced node (4), we don’t make any progress toward a balanced BST! This is the zig-zag case (also known as left-right or right-left), where the nodes aren’t in a straight line, like the left-left case or the right-right case in the previous examples. The original problem is the left-right case.

13 Double rotate 5 5 4 6 4 6 2 3 3 2 3 3 unbalanced node 2 2 1 1
Tree Rotations Double rotate 3 3 unbalanced node 5 5 2 2 4 6 4 6 1 1 2 1st Rotation 3 still unbalanced node 3 2 To fix the Zig-Zag case, we need to apply a rotation to the taller subtree underneath the unbalanced node. Through this rotation of node (2) we get the left-left case that we’ve seen before! We still have an unbalanced node at (4), but that’s okay because we know how to solve this unbalanced BST.

14 Double rotate 5 5 4 6 3 6 3 2 4 2 3 2 2 1 1 2nd Rotation
Tree Rotations Double rotate 3 5 2 5 2 4 6 1 3 6 1 2nd Rotation 3 2 4 2 For the second rotation, this was the simple rotation that we saw with the first examples. We now rotate the unbalanced node (4). This will give us a nice balanced tree!

15 Rebalancing with double rotate
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 D B k k k k B C C D Give your students enough time with this slide. Explain the Zig-Zag case again and that we need to rotate node (z), the tallest node underneath the unbalanced node (x). After the first rotation, we get the nice straight line of right-right!

16 Rebalancing with double rotate
Tree Rotations Rebalancing with double rotate k+3 k+2 x y 2nd Rotation k+2 k k+1 k+1 A y x z k+1 k z k B k k k A B C D k k C D Give your students enough time with this slide. This is another example of the right-right / left-left case. A simple single rotation on node (x) solves this problem. node (y) that used to be a few levels deep has now been rotated to the top with two rotations.

17 Summary of Rotations x x y A z A y x z y z D B A B C D B C C D
Tree Rotations Summary of Rotations Double rotation necessary Only single rotation necessary Balanced! x x y A z A y x z y z D B A B C D B C C D For double rotation, we rotate (z) first to bring (y) up. For single rotation, we rotate (x). Explain that symmetry holds! Instead of the right-left case and right-right case above, these rules also apply for the left-right case and left-left case. Symmetry holds for the other cases

18 Question: What is the resulting tree?
Tree Rotations Question: What is the resulting tree? 3 5 2 2 6 1 1 3 4 After inserting 4, how does this tree rebalance? Give your students some time to figure out what the resulting tree after the rotations is like and then go over the solution.

19 Question: What is the resulting tree?
Tree Rotations Question: What is the resulting tree? 3 3 5 5 2 2 2 6 3 6 1 1 1 3 1st Rotation 2 4 4 1 Solution: This is a Zig-Zag case. Rotate (2) first.

20 Question: What is the resulting tree?
Tree Rotations Question: What is the resulting tree? 3 2 5 3 1 1 2 2 5 3 6 1 2nd Rotation 2 4 1 4 6 1 This is the left-left case now. Rotate (5)

21 AVL Trees

22 AVL Trees AVL Trees Named after its two Soviet inventors, Georgy Adelson-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 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 Inserting an element insert(E elem) 4 2 5 1 3
AVL Trees Inserting an element insert(E elem) 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 4 2 5 1 3 Review the process that we described through the rotations slides like the rebalancing must occur at the deepest node. If you have time, play around with the link on the page! It’s a neat visualization for AVL trees. Try inserting the elements in sorted order to see how AVL trees exercise the worst case that we saw on the first few slides. We don’t need to go over deletions.

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 Point 2: Key Ranges If you wanted to answer this question with a HashSet, you would need iterate through the entire HashSet, O(n). With a balanced BST, you can find the start and end of the K-words in O(log n) time. Then you would perform an inorder traversal from start to end to get all K-words. Then solving this problem would be O(k) where k is the number of words that start with k. Point 3: findPredecessor and findSuccessor are the elements that come before and after the given element in their sorted order. For findSuccessor(E elem): Locate the element, then go up to a non left subtree and traverse the left hand side of that subtree in O(log n) time. For a HashSet, you would need to iterate through the entire set in order to find the successor. Point 4: HashSet: Lookup & insertion - worst case O(n) due to rehashing BalaneBST: Lookup & insertion - worst case O(log n)

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


Download ppt "Tree Rotations and AVL Trees"

Similar presentations


Ads by Google