View
241
Download
0
Category
Tags:
Preview:
Citation preview
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
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.
Balanced trees: AVL
Rotations
Examples
How to use
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.
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
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?
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
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);
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.
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!
RotateRight: Example
Balanced trees: AVL
6B
3
1 5
S2
8
7
S3
10
13
A
S1
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
RotateLeft: Example
Balanced trees: AVL
3
71
5 9
4 6 8
B
A
S1S3
S2
RotateLeft: Example
Balanced trees: AVL
3 B
7 A
9
8S1
5
4 6S3
1S2
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.
How to use
Balanced trees: AVL
There are different situations, and each require a particular rotation.
Case L.(left)
How to use
Balanced trees: AVL
There are different situations, and each require a particular rotation.
Case R.(right)
Balanced trees: AVL
Single rotation
Double rotation
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.
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 examplesShow the AVL resulting from the insertion of 12, 3, 2, 5, 4, 7, 9.
Recommended