Sunteți pe pagina 1din 21

AVL Tree

by

P.VASUKI
Aug 2017
Need for balanced tree
• Try inserting the elements
100,98,45,38,31,26,22,11,8,1.
• You will land up with degenerative tree ..
Tree with every parent node with single
child.
• The worst case searching time is O(n)
• Why did you get so?. How do you wanted
to have a search tree to be smarter( with
less search time)
Need for a balanced tree
• Which do you prefer? What is the search
time complexity of both trees

A
A

B
B C

C
D F E
D

F
AVL (Adelson-Velskii and
Landis) tree
• A balanced binary search tree where
the height of the two subtrees
(children) of a node differs by at most
one.
• Look-up, insertion, and deletion are
O(log n), where n is the number of
nodes in the tree.
Height of a tree
• Height: the length of the longest
path from a node to a leaf.
height(leaf) = 0
height(an_empty_tree) = –1
Balancing the tree
• After every insertion and deletion
check the balance

• Difference in depth of left and right


sub tree >1, tee is not balanced and
do the roatation
Insertion in AVL Tree
• Depends on the values of key, the tree
structure varies. May not get a balanced tree
always.

• It can be shown that there are only two


possible types of imbalance (see next slide):
– Left-left (or right-right) imbalance
– Left-right (or right-left) imbalance
– The right-hand imbalances are the same,
by symmetry
The two types of imbalance
Courtesy: Mark Allen Weiss Book
• Left-left (right- • Left-right (right-
right) left)
2 Left imbalance 2

1 so-called 1
‘dog-leg’

C C
B A
A B

There are no other possibilities for the left (or right) subtree
Localising the problem
Two principles:

• Imbalance will only occur on the path from


the inserted node to the root (only these
nodes have had their subtrees altered -
local problem)

• Rebalancing should occur at the deepest


unbalanced node (local solution too)
Left(left) imbalance (1)
[and right(right) imbalance, by symmetry]

Picture Courtesy: Mark Allen Weiss Book


• B and C have the • Note the levels
same height 2

• A is one level
higher 1

• Do single rotation
• Therefore make 1 C
the new root, 2 B
its right child and A

B and C the sub


trees of 2
Left(left) imbalance (2)
[and right(right) imbalance, by symmetry]

Picture Courtesy: Mark Allen Weiss Book


• B and C have the • Note the levels
same height
1
• A is one level higher
• Do dingle rotation 2

• Therefore make 1
the new root, 2 its
right child and B and
C the subtrees of 2 A B C
• Result: a more
balanced and legal
AVL tree
Single rotation
Picture Courtesy: Mark Allen Weiss Book

2 1

2
1

C
A B C
B
A
Left(right) imbalance (1)
[and right(left) imbalance by symmetry]
• Can’t use same
technique Picture Courtesy: Mark Allen Weiss Book

• Because now it’s 3


the middle
subtree, i.e. B, 1
that’s too deep.
• You can’t bring
C
middle tree up
A
• Instead consider B
what’s inside B...
Left(right) imbalance (2)
[and right(left) imbalance by symmetry]

Picture Courtesy: Mark Allen Weiss Book

• B will have two


subtrees 3
containing at
least one item 1
• We do not know 2
which is too deep
C
- set them both to
A
0.5 levels below B1 B2
subtree A
Left(right) imbalance (3)
[and right(left) imbalance by symmetry]

• Neither 1 nor 3
worked as root 2

node so make 2
1 3
the root
• Rearrange the
subtrees in the
correct order A B1 B2 C
• No matter how
deep B1 or B2
(+/- 0.5 levels)
we get a legal
double rotation

Picture Courtesy: Mark Allen Weiss Book

3 2

1 3
1

C
A B1 B2 C
A
B1 B2
private AvlNode<Anytype> insert(Anytype x, AvlNode<Anytype> t )
{
/*1*/ if( t == null )
t = new AvlNode<Anytype>( x, null, null );
/*2*/ else if( x.compareTo( t.element ) < 0 )
insert
{
t.left = insert( x, t.left ); method
if( height( t.left ) - height( t.right ) == 2 )
if( x.compareTo( t.left.element ) < 0 )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
/*3*/ else if( x.compareTo( t.element ) > 0 )
{
t.right = insert( x, t.right );
if( height( t.right ) - height( t.left ) == 2 )
if( x.compareTo( t.right.element ) > 0 )
t = rotateWithRightChild( t );
else
t = doubleWithRightChild( t );
}
/*4*/ else
; // Duplicate; do nothing
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
} Algorithm Courtesy: Mark Allen Weiss Book
rotateWithLeftChild method
private static AvlNode<Anytype> rotateWithLeftChild(
AvlNode<Anytype> k2 )
{
AvlNode<Anytype> 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;
}

Algorithm Courtesy: Mark Allen Weiss Book


rotateWithRightChild method
Algorithm Courtesy: Mark Allen Weiss Book
private static AvlNode<Anytype>
rotateWithRightChild( AvlNode<Anytype> k1 )
{
AvlNode<Anytype> 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;
}
doubleWithLeftChild method

private static AvlNode<Anytype>


doubleWithLeftChild(
AvlNode<Anytype> k3 )
{
k3.left = rotateWithRightChild(
k3.left );
return rotateWithLeftChild( k3
);
}
doubleWithRightChild method

private static AvlNode<Anytype>


doubleWithRightChild( AvlNode<Anytype>
k1 )
{
k1.right = rotateWithLeftChild( k1.right );
return rotateWithRightChild( k1 );
}
Algorithm Courtesy: Mark Allen Weiss Book

S-ar putea să vă placă și