Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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

4 It is always possible to scale the grades and to ask dumb questions. Surely, you would have an easier time that way. 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. Using bonus means having more work. But it gives you a chance to understand what you’re supposed to know.

5 Balanced trees: AVL

6 Rotations Examples How to use

7 Unbalanced trees Balanced trees: AVL This tree is unbalanced: its height h could be made smaller. This has an impact on performances, since operations are in O(h). lookup(67) → 4 steps Now, let’s imagine a world of prefect happiness in which trees are balanced.

8 Unbalanced trees Balanced trees: AVL lookup(67) → 4 steps Now, let’s imagine a world of prefect happiness in which trees are balanced. Before balancing After balancing lookup(67) → 3 steps

9 Unbalanced trees Balanced trees: AVL Since it’s a desirable feature, can we keep a tree balanced at all time? …and how do we do it?

10 AVL Tree Balanced trees: AVL A brand-new idea: Adelson-Velskii and Landis in 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. A B S1 S2 S3 S1S3 S2 B A rotateRight rotateLeft

11 RotateRight: Code Balanced trees: AVL A B S1 S2 S3 public void rotateRight(Node n){ Node tmp = n.getLeftChild(); n.setLeftChild(tmp.getRightChild()); tmp.setRightChild(n); } rotateRight(A): n tmp = B; tmp A.setLeftChild(S3); B.setRightChild(A);

12 RotateRight: How to remember Balanced trees: AVL A B S1 S2 S3 I rotate A and B clockwise (right). A B Then I inverse the order of the subtrees. S2 S3 S1 This is one way to remember. Practice and find your own way.

13 RotateRight: Example Balanced trees: AVL This tree is not balanced: the left part is very unbalanced. Let’s do a rotate right!

14 RotateRight: Example Balanced trees: AVL 6 B S2 8 7 S A S1

15 Balanced trees: AVL RotateLeft: Code public void rotateLeft(Node n){ Node tmp = n.getRightChild(); n.setRightChild(tmp.getLeftChild()); tmp.setLeftChild(n); } A S1 S3 B S2 tmp n

16 RotateLeft: Example Balanced trees: AVL B A S1 S3 S2

17 RotateLeft: Example Balanced trees: AVL 3 B 7 A 9 8 S S3 1 S2

18 How to use Balanced trees: AVL 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.

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

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

21 Balanced trees: AVL Single rotation Double rotation

22 How to use Balanced trees: AVL 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 } To correct the balance, find where it’s unbalanced and rotate. Note that this is really just the idea and it needs more for implementation.

23 Balanced trees: AVL Some more examples from

24 Balanced trees: AVL Some more examples from

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


Download ppt "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."

Similar presentations


Ads by Google