Você está na página 1de 47

Presentation on

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

§ The balance factor (BF) of a node is the difference


between the height of the node’s left sub-tree (HL)
and the height of the node’s right sub-tree (HR).

§ Balance Factor = Height of Left subtree – Height of
Right subtree.

§ By the AVL Tree Property, the balance factor of each
node in an AVL tree must be -1, 0, or 1.
Example: AVL Tree with Balance
Factor

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

Single LL rotation fixes an AVL tree after insertion of 1.


Analysis
§ One rotation is sufficient to fix cases 1 and 4.
§ Single rotation preserves the original height:
– The new height of the entire subtree is exactly the
same as the height of the original subtreebefore
the insertion.
§ Therefore it is enough to do rotation only at the
first node, where imbalance occurred, on the
path from inserted node to root.
§ Thus the rotation takes O(1) time.


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

Double rotation fixes AVL tree after the insertion of 5.


Right–Left double rotation to fix case 3.
Right–Left double rotation to fix case 3.
Insertions in AVL Trees
§Insertion in AVL trees is similar to the binary search tree.
§To restore the property of AVL tree:
1.New converted tree should be balanced.
2.New converted tree should be a binary search tree
with same inorder traversal.
§There are 4 cases:
§ Outside Cases (require single rotation) :
1. Insertion into left subtree of left child of X.
2. Insertion into right subtree of right child of X.
§ Inside Cases (require double rotation) :
3. Insertion into right subtree of left child of X.
4. Insertion into left subtree of right child of X.

§The rebalancing is performed through four separate rotation


procedure.
AVL Insertion: Outside Case
Consider a valid
AVL subtree
j

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

j Now do a right rotation

i
k Z
W
X V
Double rotation : second rotation
right rotation complete!

AVL property has been restored!

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;

 template <class Comparable>


class AvlNode

{

 Comparable element;
 AvlNode *left;
 AvlNode *right;
 int height;

 AvlNode( const Comparable & theElement, AvlNode *lt,


 AvlNode *rt, int h = 0 )
 : element( theElement ), left( lt ), right( rt ),
 height( h ) { }
 friend class AvlTree<Comparable>;
 };
Height
 template class <Comparable>
int

AvlTree<Comparable>::height( AvlNode<Comparable
> *t) const
{

 return t == NULL ? -1 : t->height;


}


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
{

 AvlNode<Comparable> *k1 = k2->left;


 k2->left = k1->right;
 k1->right = k2;
 k2->height = max( height( k2->left ), height( k2->right ))+1;
 k1->height = max( height( k1->left ), k2->height ) + 1;
 k2 = k1;
}


Double Rotation
 /**
* Double rotate binary tree node: first left child.

* with its right child; then node k3 with new left child.

* For AVL trees, this is a double rotation for case 2.

* Update heights, then set new root.

*/

template <class Comparable>

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.

*/

template <class Comparable>

void AvlTree<Comparable>::insert( const Comparable & x, AvlNode<Comparable> * & t

) 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!

Você também pode gostar