Escolar Documentos
Profissional Documentos
Cultura Documentos
AVL TREES
Submitted to
Dr. V.K. Pathak
Submitted by
Anjali (220/07)
III B.Tech IT
AVL Trees
§ An AVL tree is a binary search tree with a balance
condition.
§ AVL is named for its inventors: Adel’son-Vel’skii and
Landis
§ AVL tree approximates the ideal tree (completely
balanced tree).
§ AVL Tree maintains a height close to the minimum.
§ Defenition :An AVL tree is a binary search tree such
that for any node in the tree, the height of the left
and right subtrees can differ by at most 1.
•
Balance Factors
50 BF = -1
BF = 0 17 67 BF = 1
61 BF = 0
Example
Two binary search trees: (a) an AVL tree; (b) not an AVL tree
(unbalanced nodes are darkened)
Properties
§ The depth of a typical node in an AVL tree is very
close to the optimal log N.
§ Consequently, all searching operations in an AVL
tree takes O(log N) time.
§ An update (insert or remove) in an AVL tree could
destroy the balance. It must then be rebalanced
before the operation can be considered
complete.
§ After an insertion, only nodes that are on the path
from the insertion point to the root can have
their balances altered.
Rebalancing
§ Suppose the node to be rebalanced is X. It is considered
as pivot node. There are 4 cases that we might have
to fix.
§ Balance is restored by tree rotations. There are four
types of tree rotation:
1. LL rotation: An insertion in the left subtree of
the left child of X.
2. LR rotation: An insertion in the right subtree of
the left child of X.
3. RL rotation:An insertion in the left subtree of the
right child of X, or
4. RR rotation: An insertion in the right subtree of
the right child of X.
Balancing Operations: Rotations
§ LL and RR rotations are symmetric and requires
the same operation for balance.
– LL and RR rotations are handled by single
rotation.
§ LR and RL rotations are symmetric and requires
the same operation for balance.
– LR and RL rotations are handled by double
rotation.
•
Single Rotation
§ A single rotation switches the roles of the parent
and child while maintaining the search order.
§ Single rotation handles the outside cases (i.e. LL
and RR).
§ We rotate between a parent node and its child.
– Child becomes parent.
– Parent becomes right child in case 1 i.e. LL rotation
– Parent become left child in case 4 i.e. RR rotation.
§ The result is a binary search tree that satisfies the
AVL property.
Single rotation to fix case 1: LL Rotation
2 0
Symmetric single rotation to fix case 4 :RR Rotation
0 2
Example
•
Double Rotation
§ Single rotation does not fix the inside cases
(LR and RL).
§ These cases require a double rotation,
involving three nodes and four subtrees.
Single rotation does not fix case 2.
2 2
Left–right double rotation to fix case 2
Lift this up:
first rotate left between (k1, k2),
Left–right double rotation to fix case 2
Lift this up:
first rotate left between (k1, k2),
then rotate right between (k3,k2)
Left–right double rotation to fix case 2
Lift this up:
first rotate left between (k1, k2),
then rotate right between (k3,k2)
Left-Right Double Rotation
§ A left-right double rotation is equivalent to
a sequence of two single rotations:
– 1st rotation on the original tree:
a left rotation between X’s left-child and
grandchild
– 2nd rotation on the new tree:
a right rotation between X and its new left
child.
•
Example
Example
Example
k h
h h Z
X Y
AVL Insertion: Outside Case
j Inserting into X
destroys the AVL
property at node j
k h
h+1 h Z
Y
X
AVL Insertion: Outside Case
j Do a “right rotation”
k h
h+1 h Z
Y
X
Single right rotation
j Do a “right rotation”
k h
h+1 h Z
Y
X
Outside Case Completed
“Right rotation” done!
k
h+1
j
h h
X Y Z
AVL property has been restored!
AVL Insertion: Inside Case
Consider a valid
AVL subtree
j
k h
h h Z
X Y
AVL Insertion: Inside Case
Y = node i and
subtrees V and W
j
k h
h i h+1 Z
X h or h-1
V W
AVL Insertion: Inside Case
j We will do a left-right
“double rotation” . . .
k
i Z
X
V W
Double rotation : first rotation
j left rotation complete!
i
k Z
W
X V
Double rotation : second rotation
i
k Z
W
X V
Double rotation : second rotation
right rotation complete!
i
k j
h h or h-1 h
X V W Z
Example of Insertions in an AVL Tree
2-1
01 0 Insert 5, 40
30
0 0 0 0
2 3
5 5
Example of Insertions in an AVL Tree
20 2 -1
0 0
11 30 11 3 -1
0 0 0 0 0 0
0 3 0 02 3 -1
5 2 5
5 5 5 5
40
Now Insert 0
45
Single rotation (outside case)
2-2 2 -1
11 0 0
3-2 11 3 -1
0 0 0 -2 0 0
0 3 0 02 4 0
5 2 5
5 5 5 0
03 0
4 -1 4
Imbalance 0 5 5
40
5 Now Insert
34
Double rotation (inside case)
2 -2
11 0
3-2
0 0 1
0 0 4
5 Imbalance 2
5 0
1 3 4 0
5 5
Insertion of 0 3
34 4
There occurs RL rotation
Double rotation (inside case)
2 -2 2 -1
11 0 0
3-2 11 3 0
0 0 1 0 5
0 0 3 0 03 4 -1
5 Imbalance 2 5
5 5 0 0 0 0
1 3 4 0 0 2 3 4
4 0 5 4 5
Insertion of 0 3
34 4
There occurs RL rotation Now it is an AVL tree
Node declaration for AVL trees
template <class Comparable>
class AvlTree;
{
Comparable element;
AvlNode *left;
AvlNode *right;
int height;
AvlTree<Comparable>::height( AvlNode<Comparable
> *t) const
{
Single right rotation
/**
* Rotate binary tree node with left child.
* For AVL trees, this is a single rotation for case 1.
* Update heights, then set new root.
*/
template <class Comparable>
void
AvlTree<Comparable>::rotateWithLeftChild( AvlNode<Comparable>
* & k2 ) const
{
Double Rotation
/**
* Double rotate binary tree node: first left child.
* with its right child; then node k3 with new left child.
*/
void
AvlTree<Comparable>::doubleWithLeftChild( AvlNode<Compa
rable> * & k3 ) const
{
rotateWithRightChild( k3->left );
rotateWithLeftChild( k3 );
}
/* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the tree.
*/
) const
{
if( t == NULL )
t = new AvlNode<Comparable>( x, NULL, NULL );
else if( x < t->element )
{
insert( x, t->left );
if( height( t->left ) - height( t->right ) == 2 )
if( x < t->left->element )
rotateWithLeftChild( t );
else
doubleWithLeftChild( t );
}
else if( t->element < x )
{
insert( x, t->right );
if( height( t->right ) - height( t->left ) == 2 )
if( t->right->element < x )
rotateWithRightChild( t );
else
doubleWithRightChild( t );
}
else
; // Duplicate; do nothing
t->height = max( height( t->left ), height( t->right ) ) + 1;
}
Thank you!