You are on page 1of 16

AVL Trees

AVL (Adel`son-Vel`skii and Landis) tree =


A BST
With the property: For every node, the
heights of the left and right subtrees
differ at most by one
Each node contains a value (-1, 1, 0)
indicating which subtree is "heavier
Balance Factor
Each node is marked with a balance factor
indicating which subtree is "heavier
Balance Factor: height (right subtree) minus height (left
subtree) 1
A balanced tree
(-1, 1, 0) -1 1

0 0 1

0
AVL Implementation issues:

Insert and Delete are modified. They


restructure the tree to make it balanced
(if necessary)
Fixing Imbalances
An imbalance is detected when the height difference
between two subtrees of a node becomes greater
than 1 or smaller than -1
There are two cases of imbalances:

-2 2 -2 2

-1 1 1 -1

or
or 0 0 0
0
CASE 1 CASE 2
Imbalance caused by inserting node Imbalance caused by inserting node
in left subtree of left child or right in right subtree of left child or left
subtree of right child (i.e., insertion subtree of right child (i.e., insertion
occurs on the outside) occurs on the inside)
Fixing Imbalances (contd)
Fixing an imbalance is done by rotating the tree
There are two types of rotation:
single rotation
for CASE 1 imbalances
double rotation
for CASE 2 imbalances
consists of two single rotations
The rotations must always preserve the BST property
Fixing Imbalances: Case 1
node with imbalance
6
D
4
4
C right rotate
2 node 4 about 6 2 6

A B A B C D

This is a single right rotation. A single left rotation is symmetric.


Fixing Imbalances: Case 1
(contd)

left rotate node Q about P


This is a single left rotation
Fixing Imbalances: Case 2
node with imbalance node with imbalance
6
6 4
D
D
4
2 2 6
C STEP2:
A 4 2 right rotate A B C D
B node 4 about 6
C A
B

STEP 1: left rotate


node 4 about 2
Fixing Imbalances: Case 2
(contd)

STEP 1: Right rotate R about Q


STEP 2: Left rotate R about P Note that P can be part of a larger
AVL tree; it can be a child of some
other node in the tree
AVL Trees: Insert
1. Insert the node as in a BST
2. Starting at the newly inserted node,
travel up the tree (towards the
root), updating the balances along
the way
The first node for which the balance
factor becomes +/- 2 (if any) is the root
P of a subtree
EXAMPLE: Insert thethat
itemsneeds to be
rebalanced
4, 2, 1, 5, 6, 7, 8, 18, 17, 16, 13, 12
into an initially empty AVL tree
AVL Trees: Insert, rebalance
locally
If a node is entered into the larger AVL
tree and P becomes imbalanced, after
restoring the balance of P, does extra
work need to be done to the
predecessor(s) of P?
Fortunately not. The new height of P (after the
rotation) is exactly the same as the original
height of P prior to the insertion that caused
Ps imbalance. Thus no further updating of
heights on the path to the root is needed, and
consequently no further rotations are needed
Does it mean that the tree can no grow in the
height at all?
AVL Trees: Delete
1. Delete the node as in a BST
2. Starting at the parent of the deleted
node, travel up the tree (towards the
root), updating the balances along the
way
a. If the tree becomes unbalanced, decide
which rotation needs to be performed,
rotate the tree and update the balances
b. Keep traveling towards the root, checking
the balances. You may need to rotate again
AVL Trees: Efficiency
It can be shown that the worst case
height of an AVL tree is at most 44%
larger than the minimum possible
for a BST (i.e. approximately
1.44lgn)
Time Complexity of Basic AVL Tree
Operations
Insert
Maximum possible number of rotations = 1
Delete
Maximum possible number of rotations =
lg(n)
Worst case times
Search: O(lgn)
Insert: O(lgn)
Delete: O(lgn)
Other Methods
AVL trees maintain balance of BSTs
while they are being created via
insertions of data
An alternative approach is to have
trees that readjust themselves when
data is accessed, making often
accessed data items move to the top
of the tree (splay trees)

You might also like