250 likes | 328 Views
Midterm. Still 20 left…. Average so far: 26.8. Percentage under 25: ≈ 40. Percentage under 22.5: ≈ 25. 1 bonus to pass 75% of the class. Highest: 46.5. If you remember, you’re paying to get an education. That means being good at a given topic, not just a meaningless piece of paper.
E N D
Still 20 left… Average so far: 26.8 Percentage under 25: ≈ 40 Percentage under 22.5: ≈ 25 1 bonus to pass 75% of the class Highest: 46.5
If you remember, you’re paying to get an education. That means being good at a given topic, not just a meaningless piece of paper. It is always possible to scale the grades and to ask dumb questions. Surely, you would have an easier time that way. Using bonus means having more work. But it gives you a chance to understand what you’re supposed to know.
Rotations How to use Examples
Unbalanced trees This tree is unbalanced: its height h could be made smaller. This has an impact on performances, since operations are in O(h). → 4 steps lookup(67) Now, let’s imagine a world of prefect happiness in which trees are balanced. Balanced trees: AVL
Unbalanced trees → 4 steps lookup(67) Before balancing Now, let’s imagine a world of prefect happiness in which trees are balanced. After balancing → 3 steps lookup(67) Balanced trees: AVL
Unbalanced trees Since it’s a desirable feature, can we keep a tree balanced at all time? …and how do we do it? Balanced trees: AVL
AVL Tree A brand-new idea: Adelson-Velskii and Landis in 1962. What’s the idea? Rotations. In an ArrayList you had shiftLeft and shiftRight to maintain some properties: to maintain the balance, we have rotateRight and rotateLeft. B A rotateRight B S1 S2 A rotateLeft S2 S3 S1 S3 Balanced trees: AVL
RotateRight: Code n A public void rotateRight(Node n){ Node tmp = n.getLeftChild(); n.setLeftChild(tmp.getRightChild()); tmp.setRightChild(n); } B S1 tmp S2 S3 rotateRight(A): tmp = B; A.setLeftChild(S3); B.setRightChild(A); Balanced trees: AVL
RotateRight: How to remember A A B B S1 S2 S3 S1 S2 S3 I rotate A and B clockwise (right). Then I inverse the order of the subtrees. This is one way to remember. Practice and find your own way. Balanced trees: AVL
RotateRight: Example 10 6 13 This tree is not balanced: the left part is very unbalanced. 3 8 5 7 1 Let’s do a rotate right! Balanced trees: AVL
RotateRight: Example 10 A S1 6 13 B S3 S2 3 8 5 7 1 Balanced trees: AVL
RotateLeft: Code B S2 n public void rotateLeft(Node n){ Node tmp = n.getRightChild(); n.setRightChild(tmp.getLeftChild()); tmp.setLeftChild(n); } tmp A S3 S1 Balanced trees: AVL
RotateLeft: Example 3 B 1 7 A S2 9 5 S1 S3 4 6 8 Balanced trees: AVL
RotateLeft: Example 3 B 1 7 A S2 9 5 S1 S3 4 6 8 Balanced trees: AVL
How to use An AVL keeps the following property: « For all node that is not a leaf, the heights of its children can differ by at most 1. » Information is inserted exactly as in a Binary Search Tree, but we check when the heights differ by more than 1 and then we perform rotations. There are different situations, and each require a particular rotation. Balanced trees: AVL
How to use Case L. (left) There are different situations, and each require a particular rotation. Balanced trees: AVL
How to use Case R. (right) There are different situations, and each require a particular rotation. Balanced trees: AVL
Double rotation Single rotation Balanced trees: AVL
How to use To correct the balance, find where it’s unbalanced and rotate. If the tree is unbalanced on the right side{ If the right subtree is unbalanced on the left side Double rotation Else Single left rotation }else if the tree is unbalanced on the left side{ If the left subtree is unbalanced on the right side Double rotation Else Single right rotation } Note that this is really just the idea and it needs more for implementation. Balanced trees: AVL
Some more examples from http://faculty.ksu.edu.sa/mhussain/CSC212/Lecture%20-%20AVL%20%20Tree.pdf Balanced trees: AVL
Some more examples from http://faculty.ksu.edu.sa/mhussain/CSC212/Lecture%20-%20AVL%20%20Tree.pdf Balanced trees: AVL
Some more examples Show the AVL resulting from the insertion of 12, 3, 2, 5, 4, 7, 9. Balanced trees: AVL