25
Midterm

Midterm

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Midterm

Midterm

Page 2: 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

Page 3: Midterm
Page 4: Midterm

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.

Page 5: Midterm

Balanced trees: AVL

Page 6: Midterm

Rotations

Examples

How to use

Page 7: Midterm

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.

Page 8: Midterm

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

Page 9: Midterm

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?

Page 10: Midterm

AVL Tree

Balanced trees: AVL

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.

A

B S1

S2 S3 S1S3

S2

B

A

rotateRight

rotateLeft

Page 11: Midterm

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);

Page 12: Midterm

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.

Page 13: Midterm

RotateRight: Example

Balanced trees: AVL

10

136

8

7

3

1 5

This tree is not balanced: the left part is

very unbalanced.

Let’s do a rotate right!

Page 14: Midterm

RotateRight: Example

Balanced trees: AVL

6B

3

1 5

S2

8

7

S3

10

13

A

S1

Page 15: Midterm

Balanced trees: AVL

RotateLeft: Codepublic void rotateLeft(Node n){

Node tmp = n.getRightChild();

n.setRightChild(tmp.getLeftChild());

tmp.setLeftChild(n);

}A

S1S3

B

S2tmp

n

Page 16: Midterm

RotateLeft: Example

Balanced trees: AVL

3

71

5 9

4 6 8

B

A

S1S3

S2

Page 17: Midterm

RotateLeft: Example

Balanced trees: AVL

3 B

7 A

9

8S1

5

4 6S3

1S2

Page 18: Midterm

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.

Page 19: Midterm

How to use

Balanced trees: AVL

There are different situations, and each require a particular rotation.

Case L.(left)

Page 20: Midterm

How to use

Balanced trees: AVL

There are different situations, and each require a particular rotation.

Case R.(right)

Page 21: Midterm

Balanced trees: AVL

Single rotation

Double rotation

Page 22: Midterm

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 rotationElse

Single left rotation}else if the tree is unbalanced on the left side{

If the left subtree is unbalanced on the right sideDouble rotation

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

Page 23: Midterm

Balanced trees: AVL

Some more examples

from http://faculty.ksu.edu.sa/mhussain/CSC212/Lecture%20-%20AVL%20%20Tree.pdf

Page 24: Midterm

Balanced trees: AVL

Some more examples

from http://faculty.ksu.edu.sa/mhussain/CSC212/Lecture%20-%20AVL%20%20Tree.pdf

Page 25: Midterm

Balanced trees: AVL

Some more examplesShow the AVL resulting from the insertion of 12, 3, 2, 5, 4, 7, 9.