AVLTrees
AVLTrees
• AVL Trees
1
AVL Tree
• AVL trees are 4
balanced. 44
2 3
• An AVL Tree is a 17 78
3
Height of an AVL Tree (cont)
• Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2)
n(h) > 2n(h-2)
n(h) > 4n(h-4)
…
n(h) > 2in(h-2i)
• Solving the base case we get: n(h) ≥ 2 h/2-1
• Taking logarithms: h < 2log n(h) +2
• Thus the height of an AVL tree is O(log n)
4
Insertion
• A binary search tree T is called balanced if for every node v, the
height of v’s children differ by at most one.
• Inserting a node into an AVL tree involves performing an
expandExternal(w) on T, which changes the heights of some of
the nodes in T.
• If an insertion causes T to become unbalanced, we travel up the
tree from the newly created node until we find the first node x
such that its grandparent z is unbalanced node.
• Since z became unbalanced by an insertion in the subtree rooted
at its child y, height(y) = height(sibling(y)) + 2
• Now to rebalance...
5
Insertion: rebalancing
6
Insertion (contd.)
unbalanced...
5
44
2 z 64
17
2y 78 7
1 3 1
32 1 50 4 88
1
2 x
13
48 62
5
54 T3
T0 T2 ...balanced
4
44
3
4 x
2
17
2y 62
z 6
1 2 2
32
1
1 50 3 5
78 7
1 1
48 54 88
T2
7
T0 T1 T3
Restructuring
• The four ways to rotate nodes in an AVL tree, graphically represented
-Single Rotations:
T0 T3
T1 T3 T0 T1 T2
T2
T3 T3
T0 T2 T2 T1 T0
T1 8
Restructuring (contd.)
• double rotations:
T0 T2
T2 T3 T0 T1 T3
T1
double rotation
c=z b=x
a=y a=y c=z
b=x
T0 T2
T3 T2 T3 T1 T0
T1
9
Restructure Algorithm
Algorithm restructure(x):
Input: A node x of a binary search tree T that has both a parent y and a
grandparent z
Output: Tree T restructured by a rotation (either
single or double) involving nodes x, y, and z.
T0 48 54 88
T2
T1 T3
11
Cut/Link Restructure Algorithm
44
y
17 62
x
50 78
T0 48 54 88
T2
T1 T3
• Make a new tree which is balanced and put the 7 parts from the
old tree into the new tree so that the numbering is still correct
when we do an in-order-traversal of the new tree.
• This works regardless of how the tree is originally unbalanced.
• Let’s see how it works!
12
Cut/Link Restructure Algorithm
• Number the 7 parts by doing an in-order-traversal. (note that x,y, and
z are now renamed based upon their order within the traversal)
2z (a)
1 44
y (b) 4
17 62
3 x (c) 6
50 78
T0 48 54
5 88
7
T2
13
T1 T3
Cut/Link Restructure Algorithm
• Now create an Array, numbered 1 to 7 (the 0th element can be
ignored with minimal waste of space)
1 2 3 4 5 6 7
•Cut() the 4 T trees and place them in their inorder rank in the array
T0 T1 T2 T3
1 2 3 4 5 6 7
14
Cut/Link Restructure Algorithm
• Now cut x,y, and z in that order (child,parent,grandparent) and
place them in their inorder rank in the array.
T0 a T1 b T2 c T3
44 62 78
1 2 3 4 5 6 7
•Now we can re-link these subtrees to the main tree.
•Link in rank 4 (b) where the subtree’s root formerly
4 b
62
15
Cut/Link Restructure Algorithm
• Link in ranks 2 (a) and 6 (c) as 4’s children.
4b
62
a c
2 44 6
78
16
Cut/Link Restructure Algorithm
• Finally, link in ranks 1,3,5, and 7 as the children of 2 and 6.
4y
62
2 44z x
6
78
3 5 7
17 50 88
T2
48 54
T0 T3
T1
• Now you have a balanced tree!
17
Cut/Link Restructure algorithm
• This algorithm for restructuring has the exact same
effect as using the four rotation cases discussed
earlier.
• Advantages: no case analysis, more elegant
• Disadvantage: can be more code to write
• Same time complexity
18
Removal
• We can easily see that performing a
removeAboveExternal(w) can cause T to become
unbalanced.
• Let z be the first unbalanced node encountered while
travelling up the tree from w. Also, let y be the child of z
with the larger height, and let x be the child of y with the
larger height.
• We can perform operation restructure(x) to restore
balance at the subtree rooted at z.
• As this restructuring may upset the balance of another
node higher in the tree, we must continue checking for
balance until the root of T is reached
19
Removal (contd.)
• example of deletion from an AVL tree:
4
Oh no, unbalanced!
44
1
y
3
17 62
x
2 2
50 78
1 1 0 1
T0 48 54 88
T2
32
T1 T3
y
4
62
3
z x
2
44 78
1
2
0 1
Whew,
50
17
1 1
88 balanced!
48 54 T2
20
T0 T3
Removal (contd.)
• example of deletion from an AVL tree:
z
4
44
1
y
3
17 62
2
x
2 Oh no,
50 78
T0
1 1 0 1 unbalanced!
48 54 88
32
T1 T2 T3
4 x
50
z y
2
44 62
3 Whew,
1
17
1
48
1
54 78
2 balanced!
0 1
88
21
T0 T1 T2
Implementation
• A Java-based implementation of an AVL tree requires the following
node class:
public class AVLItem extends Item {
int height;
AVLItem(Object k, Object e, int h) {
super(k, e);
height = h;}
public int height () {
return height;}
public int setHeight(int h) {
int oldHeight = height;
height = h;
return oldHeight;}
22
}
Implementation (contd.)
public class SimpleAVLTree
extends SimpleBinarySearchTree
implements Dictionary {
public SimpleAVLTree(Comparator c) {
super(c);
T = new RestructurableNodeBinaryTree();}
private int height(Position p) {
if (T .isExternal(p))
return 0;
else
return ((AVLItem) p.element()). height(); }
private void setHeight(Position p) { // called only if p is internal
((AVLItem) p. element()). setHeight
(1+Math.max(height(T .leftChild(p)),
23
height(T .rightChild(p)))); }
Implementation (contd.)
private boolean isBalanced(Position p) {
// test whether node p has balance factor
// between -1 and 1
int bf = height(T.leftChild(p)) - height(T.rightChild(p));
return ((-1 <= bf) && (bf <= 1));}
25
Implementation (contd.)
public void insertItem(Object key, Object element)
throws InvalidKeyException {
super.insertItem(key, element);// may throw an InvalidKeyException
Position zPos = actionPos; // start at the insertion position
T.replace(zPos, new AVLItem(key, element, 1));
rebalance(zPos);}
public Object remove(Object key) throws InvalidKeyException {
Object toReturn = super.remove(key);
// may throw an InvalidKeyException
if (toReturn != NO_SUCH_KEY) {
Position zPos = actionPos; // start at the removal position
rebalance(zPos);}
return toReturn;
}} 26