0% found this document useful (0 votes)
28 views43 pages

AVL Trees

Uploaded by

emc.javaid2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views43 pages

AVL Trees

Uploaded by

emc.javaid2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

AVL Trees

AVL TREES
● An AVL tree is a binary search tree with a
balance condition
● Introduced by Andelson-Velskii and E.M.
Landis in 1962.
○ To ensure that the height of the tree is O(logN)
○ For every node in the AVL tree, the height of left
subtree and the height of the right subtree can
differ by at most one.
○ We are concerned about minimizing worst case
behavior. We wish to consider trees that have a
AVL TREES
● The height of the left subtree minus the
height of the right subtree of a node is
called the balance of the node. For an
AVL tree, the balances of the nodes are
always -1, 0 or 1.
○ The height of an empty tree is defined to be
-1.
● Given an AVL tree, if insertions or
deletions are performed, the AVL tree
AVL TREES
5 7

2 8 2 8

1 4 7 1 4

3 3 5

An AVL Tree Not an AVL Tree


AVL TREES

● To maintain the height balanced


property of the AVL tree after insertion
or deletion, it is necessary to perform a
transformation on the tree so that

(1) the inorder traversal of the transformed tree


is the same as for the original tree (i.e., the new
tree remains a binary search tree).
AVL TREES
● After an insertion, only nodes that are on the
path from the insertion point to the root might
have their balance altered

● Follow the path up to the root, find the first


node (i.e., deepest) whose new balance
violates the AVL condition. Call this node a.

● Rebalance the tree at node a.


AVL TREES
● Violation may occur when an insertion into
1. left subtree of left child (LL case)

2. right subtree of left child (LR case)

3. left subtree of right child (RL case)

4. right subtree of right child (RR case)


AVL TREES
● Rotation

- to restore the AVL tree after insertion


- single rotation
○ For case 1 and 4

- double rotation
○ For case 2 and 3
AVL TREES: SINGLE ROTATION

i.e. LL case
AVL TREES: SINGLE
ROTATION

i.e.RR case
AVL TREES: SINGLE
ROTATION

● Example: 3 2 1 4 5 6 7
● Construct AVL tree (height balanced)
AVL TREES: SINGLE
ROTATION
Insert 4, 5

Insert 6
AVL TREES: SINGLE ROTATION

Insert 7

Now Insert 16 and then 15


AVL TREES: DOUBLE
ROTATION

● Single rotation fails to fix cases 2 and 3


(i.e. LR and RL cases)
DOUBLE ROTATION
● LR – implemented as RR rotation followed
by LL rotation

● RL – implemented as LL rotation followed


by RR rotation
AVL TREES: DOUBLE ROTATION

● Double rotation is used


● case 2 (LR case)
AVL TREES: DOUBLE
ROTATION

● case 3 (RL case)


AVL TREES: DOUBLE ROTATION

● Example:
AVL TREES: DOUBLE
ROTATION

● Insert 14
AVL TREES: DOUBLE ROTATION

● Insert 13 (This is single rotation: RR


Case)
AVL TREES: DOUBLE ROTATION

● Insert 12
AVL TREES: DOUBLE ROTATION

● Insert 11 and 10 (single rotation), then 8


AVL TREES: DOUBLE ROTATION

● Inserting 9
Right Rotation of node Q Left Rotation of node P
Let P be Q's left child. Let Q be P's right child.
Set P to be the new root. Set Q to be the new root.
Set Q's left child to be P's right Set P's right child to be Q's left
child. child.
AVL Trees: Double Rotation
● Left-Right Rotation (LR) (Right followed
by a Left)
a b
a
b
c a c

b
c
AVL Trees: Double Rotation
● Right-Left Rotation (RL) (Left followed by
a Right)
c b
c
b c
a a

b
a
AVL TREES
IMPLEMENTATION
AVL TREES:
IMPLEMENTATION
● After insertion, if the tree is still balanced =>
done
● Otherwise, if imbalance appears, then
determine the appropriate action: single or
double rotation
● LH: The left subtree of the current node has
more levels
● EH: The left and right subtrees have equal
height
●RH: The right subtree has more levels
AVL TREES:
IMPLEMENTATION
struct AvlNode;

typedef struct AvlNode *Position;


typedef struct AvlNode *AvlTree;

Position Find( Item X, AvlTree T );


AvlTree Insert( Item X, AvlTree T );
AVL TREES:
IMPLEMENTATION
struct AvlNode
{
Item Element;
AvlTree Left;
AvlTree Right;
int Height;
};
AVL TREES:
IMPLEMENTATION
int Height( Position P )
{
if( P == NULL )
return -1;
else
return P->Height;
}
AVL TREES:
IMPLEMENTATION
AvlTree Insert( Item X, AvlTree T )
{ if ( T == NULL )
{ /* Create and return a one-node tree */
T = new AvlNode;
T->Element = X;
T->Height = 0;
T->Left = T->Right = NULL;
}
AVL TREES: IMPLEMENTATION
if ( X < T->Element )
{
T->Left = Insert( X, T->Left );
if ( Height( T->Left ) - Height( T->Right ) == 2 )

if ( X < T->Left->Element )
T = SingleRotateWithLeft( T );
else
T = DoubleRotateWithLeft( T );
}
else
AVL TREES: IMPLEMENTATION
if ( X > T->Element )
{ T->Right = Insert( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == 2 )
if( X > T->Right->Element )
T = SingleRotateWithRight( T );
else
T = DoubleRotateWithRight( T );
}
/* Else X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T>Right ) ) + 1;
return T;
}
AVL TREES: IMPLEMENTATION

Position SingleRotateWithLeft( Position K2 )


{
Position K1;

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;
return K1; /* New root */
CASE 1: RIGHT IS HIGHER
● Left rotation is performed
● Add value 60

20
20

10 40
10 40
30 50

30 50
60
CASE 1: RIGHT IS HIGHER
● Resulting Tree after left Rotation

40

20 50

10 30 60
AVL TREES: IMPLEMENTATION
Position SingleRotateWithRight( Position K1 )
{
Position K2;

K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;
K1->Height = Max( Height(K1->Left), Height(K1->Right)
) + 1;
K2->Height = Max( Height(K2->Right), K1->Height ) + 1;
return K2; /* New root */
}
CASE 2:LEFT SUBTREE IS
HIGHER
.
40

20 50

10 30 60

25 35
CASE 2:LEFT SUBTREE IS
HIGHER
● Adding node 22

40 40

20 50 30 50

10 30 60 20 35 60

10
25 35 25

22 22
CASE 2:LEFT SUBTREE IS
HIGHER
● AVL Balanced Tree

30

20 40

10 25
35 50

22
60
AVL TREES: IMPLEMENTATION

Position DoubleRotateWithLeft( Position K3 )


{ Single Right Rotation at
/* Rotate between K1 and K2 */ K1
Single
K3->Left = SingleRotateWithRight( K3->Left ); Left rotation at K3
/* Rotate between K3 and K2 */
return SingleRotateWithLeft( K3 );
}
AVL TREES: IMPLEMENTATION

Position DoubleRotateWithRight( Position K1)


{
/* Rotate between K3 and K2 */
K1->Right = SingleRotateWithLeft( K1->Right );
/* Rotate between K1 and K2 */
return SingleRotateWithRight( K1 );
}

You might also like