AVL Tree Notes
AVL Tree Notes
AVL Trees
Atul Gupta
Atul Gupta
Balance Factor
The balance factor of a node x is the height of
its left sub-tree minus the height of its right
sub-tree
balance factor (x) = h(left-sub-tree) h(rightsub-tree)
A node with a balance factor 1, 0, or -1 is
considered balanced
Atul Gupta
Node Heights
Tree A (AVL)
height=2 BF=1-0=1
Tree B (AVL)
2
height of node = h
balance factor = hleft-hright
empty height = -1
Atul Gupta
balance factor
1-(-1) = 2
-1
0
height of node = h
balance factor = hleft-hright
empty height = -1
AVL Trees
Atul Gupta
root
Z
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}
rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}
Atul Gupta
7
root
2
1
9
5
10
rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}
root
7
10
2
1
rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}
Atul Gupta
root
7
10
2
1
rotate_left(z)
node* rotate_left(z)
{
node* root = z->right;
z->right = root->left;
root->left = z;
return root;
}
root
7
10
2
1
rotate_right(z)
node* rotate_right(z)
{
node root = z->left;
z->left = root->right;
root->right = z;
return root;
}
7
2
1
8
5
root
9
10
Atul Gupta
Insert
Insert can cause the balance factor of a node
to become 2 or -2
Only nodes on the path from the insertion
point to the root might have changed
After Insert, traverse up the tree to the root,
updating heights
If a new balance factor is 2 or -2, adjust the
tree by rotation around the node
L
P
if (balance_factor(L) == 2) { //The left column
let P=left_child(L)
if (balance_factor(P) == -1) { //The "Left Right Case"
rotate_left(P //reduce to "Left Left Case"
}
//Left Left Case
rotate_right(L);
} else { // balance_factor(L) == -2, the right column
let P=right_child(L)
if (balance_factor(P) == 1) { //The "Right Left Case"
rotate_right(P) //reduce to "Right Right Case"
}
//Right Right Case
rotate_left(L);
}
Atul Gupta
Atul Gupta
Consider a valid
AVL subtree
j
k
Inserting into X
destroys the AVL
property at node j
h
h+1
Y
X
Atul Gupta
10
j
k
Do a right rotation
h+1
Y
X
j
k
Do a right rotation
h+1
Y
X
Atul Gupta
11
k
j
h+1
Consider a valid
AVL subtree
Atul Gupta
12
Inserting into Y
destroys the
AVL property
at node j
h+1
k
j
Right rotation
does not restore
balance now k is
out of balance
h
h+1
Z
Y
Atul Gupta
13
h+1
Y = node i and
subtrees V and W
k
i
h+1
h or h-1
Atul Gupta
14
We will do a left-right
double rotation . . .
k
Z
i
Z
k
W
X
Atul Gupta
15
i
Z
k
W
X
i
j
k
h
Atul Gupta
h or h-1
16
j
k
h+1
AVL Tree
Also known as height-balance trees has
average and worse case height as O(log n)
Accordingly, search, insertion and deletion will
be O(log n) [Guaranteed]
Requires more work during insertion, i.e.
check for balancing and possible left and right
rotations. However, this helps in longer run as
the resulting search tree will always be
balanced.
Atul Gupta
17