( ) 16-avl-trees (1)
( ) 16-avl-trees (1)
( ) 16-avl-trees (1)
AVL trees
read: Weiss Ch. 4, section 4.1 - 4.4
slides created by Marty Stepp
http://www.cs.washington.edu/373/
1
Trees and balance
• balanced tree: One whose subtrees differ in height by at most 1 and
are themselves balanced.
A balanced tree of N nodes has a height of ~ log2 N.
A very unbalanced tree can have a height close to N.
height = 4 7
(balanced)
2
Some height numbers
• Observation: The shallower the BST the better.
Average case height is O(log N)
Worst case height is O(N)
Simple cases such as adding (1, 2, 3, ..., N), or the opposite order,
lead to the worst case scenario: height O(N).
root
• For binary tree of height h:
max # of leaves: 2h-1 15
max # of nodes: 2h - 1
min # of leaves: 1 8 20
min # of nodes:h
2 14 18 21
3
Calculating tree height
• Height is max number of nodes in path from root to any leaf.
height(null) = 0
height(a leaf) = ?
height(A) = ?
A
4
AVL trees
• AVL tree: a binary search tree that uses modified add and remove
operations to stay balanced as its elements change
one of several kinds of auto-balancing trees (others in book)
invented in 1962 by two Russian mathematicians
• (Adelson-Velskii and Landis)
• A-V & L proved that an AVL tree's height is always O(log N).
basic idea: When nodes are added to / removed from the tree,
if the tree becomes unbalanced, repair the tree until balance is
restored.
• rebalancing operations are relatively efficient (O(1))
• overall tree maintains a balanced O(log N) height, fast to add/search
5
Balance factor
• balance factor, for a tree node T :
= height of T's right subtree minus height of T's left subtree.
BF(T) = Height(T.right) - Height(T.left)
-2
• (the tree at right shows BF of each node)
6
AVL tree examples
• Two binary search trees:
(a) an AVL tree
(b) not an AVL tree (unbalanced nodes are darkened)
7
Which are valid AVL trees?
• What is the balance factor of each tree node?
14
5 4
9 28
2 7 2 5
6 9
31
4 -1
8
4 20
44
3 -2 3
3
0 15 22
-4
1
11 33
8
Tracking subtree height
• Many of the AVL tree operations depend on height.
Height can be computed recursively by walking the tree; too slow.
Instead, each node can keep track of its subtree height as a field:
private class TreeNode {
private E data;
10 data
private int height; 3 3 height
private TreeNode left; 10
private TreeNode right; left/
}
2 1 right
5 20
0 1 0 0
2 9 15 30
0
7
9
AVL add operation
• For all AVL operations, we assume the tree was balanced before the
operation began.
Adding a new node begins the same as with a typical BST, traversing
left and right to find the proper location and attaching the new node.
But adding this new node may unbalance the tree by 1:
set.add(49);
55
29 87
-3 42
49
10
AVL add cases
• Consider the lowest node k2 that has now become unbalanced.
The new offending node could be in one of the four following
grandchild subtrees, relative to k2:
11
Key idea: rotations
• If a node has become out of balanced in a given direction, rotate it
in the opposite direction.
rotation: A swap between parent and left or right child,
maintaining proper BST ordering.
25 8
rotate right
8 3 25
3 11 11
12
Right rotation
• right rotation (clockwise): (fixes Case 1 (LL))
left child k1 becomes parent
original parent k2 demoted to right
k1's original right subtree B (if any) is attached to k2 as left subtree
13
Right rotation example
• What is the balance factor of k2 before and after rotating?
14
Right rotation steps
1. Detach left child (11)'s right subtree (27) (don't lose it!)
2. Consider left child (11) be the new parent.
3. Attach old parent (43) onto right of new parent (11).
4. Attach new parent (11)'s old right subtree (27)
as left subtree of old parent (43).
43 43 11
11 65 11
65
8 43
8 27 8 27
3 27 65
3 3
15
Right rotation code
private TreeNode rightRotate(TreeNode oldParent) {
// 1. detach left child's right subtree
TreeNode orphan = oldParent.left.right;
return newParent;
}
16
Right rotation code
private int height(TreeNode node) {
if (node == null) {
return 0;
}
int left = (node.left == null) ? 0 : node.left.height;
int right = (node.right == null) ? 0 : node.right.height;
return Math.max(left, right) + 1;
}
17
Left rotation
• left rotation (counter-clockwise): (fixes Case 4 (RR))
right child k2 becomes parent
original parent k1 demoted to left
k2's original left subtree B (if any) is attached to k1 as left subtree
18
Left rotation steps
1. Detach right child (65)'s left subtree (51) (don't lose it!)
2. Consider right child (65) be the new parent.
3. Attach old parent (43) onto left of new parent (65).
4. Attach new parent (65)'s old left subtree (51)
as right subtree of old parent (43).
43 43 65
21 65 65
21 43 87
51 87 51 87
21 51 73
73 73
19
Left rotation code
private TreeNode leftRotate(TreeNode oldParent) {
// 1. detach right child's left subtree
TreeNode orphan = oldParent.right.left;
return newParent;
}
20
Problem cases
• A single right rotation does not fix Case 2 (LR).
22
Left-right rotation example
• What is the balance factor of k1, k2, k3 before and after rotating?
23
Right-left double rotation
• right-left double rotation: (fixes Case 3 (RL))
1) right-rotate k1's right child ... reduces Case 3 into Case 4
2) left-rotate k1 to fix Case 4
24
AVL add example
• Draw the AVL tree that would result if the following numbers were
added in this order to an initially empty tree:
20, 45, 90, 70, 10, 40, 35, 30, 99, 60, 50, 80
45
20 70
10 35 60 90
30 40 50 80 99
25