Recitation 9 Tree Rotations and AVL Trees

advertisement
Recitation 9
Tree Rotations and AVL Trees
BSTs
Review: Binary Search Tree (BST)
1
ideal case
4
2
1
x < 4
2
5
3
worst case:
O(n) lookup
O(n) insertion
O(n) deletion
3
x > 4
4
5
BSTs
Make BSTs balanced!
Balanced BST
4
2
1
worst case:
O(log n) lookup
O(log n) insertion
O(log n) deletion
5
3
If a BST becomes unbalanced,
we can rebalance it in O(log n).
BSTs
Review: definition of Height
public static int getHeight(TreeNode t) {
if (t == null)
return -1;
return 1 + Math.max(getHeight(t.left),
getHeight(t.right));
}
length of the longest path from a node to a leaf
BSTs
Definition of Balanced
public static boolean isBalanced(TreeNode t) {
return t == null ||
Math.abs(getHeight(t.left) getHeight(t.right)) <= 1 &&
isBalanced(t.left) &&
isBalanced(t.right);
}
A tree is balanced if each of its subtrees is
balanced and their heights differ by at most 1
BSTs
isBalanced: Recursion needed!
0
-1
All subtrees need to
be balanced!
-2
-3
-4
1
2
3
4
Tree Rotations
Tree Rotations
Notation
k+2
x
Inorder traversal:
A x B y C
k+1
k
y
A
k
B
k
C
Recall that the BST inorder
traversal gives sorted order.
A subtree of height k
Tree Rotations
Rotations: Used to balance a BST
k+2
x
The blue pointers are the only
ones that change.
k+2
y
k+1
k
y
x
A
B
C
k
k
C
k
k+1
k
k
A
Inorder traversals are the same
B
Tree Rotations
Rotations example
2 3
2
5
5
0
1 2
2
6
0 1
unbalanced
node
1
3
Rotate
3
4
6
0
2
0
0
0
4
Tree Rotations
Rebalancing
unbalanced
node
k+3
k+2
y
x
k+2
k
y
x
A
k
k+1
B
k
k
A
C
k+1
k+1
B
C
Tree Rotations
Problem: Rotating a Zig-Zag!
3
unbalanced
node
3
5
5
2
4
0
6
Rotate
2
2
0
6
left child taller
1
1
2
right child taller
0
3
Zig-Zag: taller
children on
opposite sides
4
0
3
We get the
opposite Zig-Zag!
Tree Rotations
Double rotate
3
unbalanced
node
3
5
5
2
4
0
0
2
6
4
1
6
1
2
1st Rotation
3
0
3
0
2
still
unbalanced
node
Tree Rotations
Double rotate
3
5
2
5
2
4
0
6
1
0
3
1
3
0
2
2nd Rotation
6
0
2
0
4
Tree Rotations
Rebalancing with double rotate
k+3
k+3
x
x
1st Rotation
k+2
k
z
A
y
A
k+1
y
k
C
D
k+1
k
k
k
B
k+2
k
z
B
k
C
k
D
Tree Rotations
Rebalancing with double rotate
k+3
k+2
y
x
2nd Rotation
k+2
k
k+1
k+1
y
z
x
A
k+1
k
z
B
k
C
k
k
k
D
A
B
k
C
k
D
Tree Rotations
Summary of Rotations
Double rotation
necessary
Only single rotation
necessary
Balanced!
x
y
x
z
A
y
A
y
z
D
B
z
x
C
B
A
C
D
Symmetry holds for the other cases
B
C
D
Tree Rotations
Question: What is the resulting tree?
3
5
0
2
2
6
0
1
1
3
0
4
Tree Rotations
Question: What is the resulting tree?
3
3
5
5
0
2
2
6
0
1
2
0
3
1
6
1
3
1st Rotation
2
0
4
1
0
0
4
Tree Rotations
Question: What is the resulting tree?
3
2
5
3
2
0
3
1
2
1
0
2
6
0
4
2nd Rotation
1
1
5
0
1
0
4
0
6
AVL Trees
AVL Trees
AVL Trees
Named after its two Soviet inventors, Georgy AdelsonVelsky and E. M. Landis, who described AVL tres in a
paper in 1962.
First invention of self-balancing BSTs.
Later: red-black trees, splay trees, and others
AVL Trees
AVL Tree
AVL Tree: self-balancing BST
4
2
1
5
3
AVL invariant:
the height difference between its left
and right children is at most 1
Lookup works the same as a normal
BST lookup
worst case:
O(log n) lookup
O(log n) insertion
O(log n) deletion
AVL Trees
Inserting an element
insert(E elem)
4
2
1
5
3
https://www.cs.usfca.edu/~galles/visualization/A
VLtree.html
Insert like a normal BST and if the
AVL invariant is broken, do a single
or double rotation to fix it
Localizing the problem:
1. Imbalance will occur only on the
path from the root to the newly
inserted node
2. Rebalancing should occur at the
deepest node
3. Must search for possible
imbalance all the way up to root
AVL Trees
Why use AVL Trees?
If HashSets have a lookup of expected O(1), why use BSTs with an expected
lookup time of O(log n)?
Depends on the problem:
1. Binary Search Trees are great at keeping elements in sorted order.
2. Key Ranges: How many words in the set start with k and end in z?
3. findPredecessor(E elem) and findSuccessor(E elem)
● O(log n) for AVL Tree, expected case O(n) for HashSet
4. Better worst case lookup and insertion times
Prelim Information
1. Tree Rotations will not be tested on Prelim 2
1. You don’t need to be able to write Tree Rotations code
but can find it online if interested
Download