1 / 25

Midterm

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.

Download Presentation

Midterm

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

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

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

  4. Balanced trees: AVL

  5. Rotations How to use Examples

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

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

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

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

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

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

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

  13. RotateRight: Example 10 A S1 6 13 B S3 S2 3 8 5 7 1 Balanced trees: AVL

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

  15. RotateLeft: Example 3 B 1 7 A S2 9 5 S1 S3 4 6 8 Balanced trees: AVL

  16. RotateLeft: Example 3 B 1 7 A S2 9 5 S1 S3 4 6 8 Balanced trees: AVL

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

  18. How to use Case L. (left) There are different situations, and each require a particular rotation. Balanced trees: AVL

  19. How to use Case R. (right) There are different situations, and each require a particular rotation. Balanced trees: AVL

  20. Double rotation Single rotation Balanced trees: AVL

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

  22. Some more examples from http://faculty.ksu.edu.sa/mhussain/CSC212/Lecture%20-%20AVL%20%20Tree.pdf Balanced trees: AVL

  23. Some more examples from http://faculty.ksu.edu.sa/mhussain/CSC212/Lecture%20-%20AVL%20%20Tree.pdf Balanced trees: AVL

  24. Some more examples Show the AVL resulting from the insertion of 12, 3, 2, 5, 4, 7, 9. Balanced trees: AVL

More Related