AVL Trees

1
Presentation
Data Structure & Algorithms
Presented By: Group 11
By
Komal 6622
Tayyaba 6648
Maryam 6626
Kanwal 6619
Mahnoor 6623
AVL Trees

2
AVL Trees

6

v
8

3

z
4

AVL Trees

3
Contents
1) Defination
2) Why we need?
3) Difference beteen AVL and Non AVL
by example
4) Techniques with examples
5) Advantage and Disadvantage
6) Time complexity
AVL Trees

4
 

AVL Trees:
AVL trees were invented in 1962 by two Russian
scientist G.M Adelson Velsky & E.M Landis.

Definition:
An AVL tree is a binary search tree in
which the balance factor of every node is
either 0 or +1 or -1 .
Height Of A Tree:
The height of a binary tree is the
maximum path length from root to the leaf
Level 0
Level 1
Level 2
Level 3
The height of this tree is= 3

AVL Trees

6
Balance Factor:
 It is defined as the difference b/w the
heights of the node’s left & right sub trees

 Balance factor=ht of left sub tree – ht of
right sub tree.

 ht=height
AVL Trees

7
Why we need AVL Tree?
o To keep binary search tree perfectly
balanced we need AVL Tree with
O(log N) complexity…

AVL Trees

8
Example :

AVL tree

BST ( not AVL tree )

1

2

0
0

0
-1

0

0

0
0

0

0
-1

0

0
Techniques
to make AVL Tree:
If an insertion of a new node makes an AVL tree
unbalanced , we transform the tree by a rotation
there are four types of rotation we have:

1.
2.
3.
4.

Single right rotation or R-rotation
Single left rotation or L-rotation
Double right-left rotation or RL-rotation
Double left-right rotation or LR-rotation
AVL Trees

10
 

1.Straight line with positive unbalanced.
apply Right rotation for unbalanced node.
+2

+1
0

2

4

5

0 4
0 2

0

5
 

2.Straight line with negative unbalanced.
apply left rotation for unbalanced node.
5

-2
0
6 -1
7

0 5
0

6
0 7
 

3.Curved line with positive unbalanced.
apply left-right rotation.

Right rotation for unbalanced node and left
rotation for the nearest node.
For example: C,A,B
+2
C

C

0 B

B

A -1
B

0

A

0

A

C

0
 

4.Curved line with negative unbalanced.
apply right-left rotation.

Left rotation for unbalanced node and right
rotation for the nearest node.
For example: A,C,B
A -2
C
B

0

A

0 B

B
+1

C

0

A

C

0
Advantages &Disadvantages
 The advantage of an AVL tree is that it
is always balanced, guaranteeing the
O(logn) speed of the Binary Search
algorithm.
 The disadvantages the complex
rotations used by the insertion and
removal
AVL Trees

15
Running Times for AVL
Trees
find is O(log n)
 height of tree is O(log n)
insert is O(log n)
 initial find is O(log n)
remove is O(log n)
 initial remove is O(log n)
AVL Trees

16
Thank You

AVL Trees

17

Avltrees

  • 1.
  • 2.
    Presentation Data Structure &Algorithms Presented By: Group 11 By Komal 6622 Tayyaba 6648 Maryam 6626 Kanwal 6619 Mahnoor 6623 AVL Trees 2
  • 3.
  • 4.
    Contents 1) Defination 2) Whywe need? 3) Difference beteen AVL and Non AVL by example 4) Techniques with examples 5) Advantage and Disadvantage 6) Time complexity AVL Trees 4
  • 5.
      AVL Trees: AVL treeswere invented in 1962 by two Russian scientist G.M Adelson Velsky & E.M Landis. Definition: An AVL tree is a binary search tree in which the balance factor of every node is either 0 or +1 or -1 .
  • 6.
    Height Of ATree: The height of a binary tree is the maximum path length from root to the leaf Level 0 Level 1 Level 2 Level 3 The height of this tree is= 3 AVL Trees 6
  • 7.
    Balance Factor:  Itis defined as the difference b/w the heights of the node’s left & right sub trees  Balance factor=ht of left sub tree – ht of right sub tree.  ht=height AVL Trees 7
  • 8.
    Why we needAVL Tree? o To keep binary search tree perfectly balanced we need AVL Tree with O(log N) complexity… AVL Trees 8
  • 9.
    Example : AVL tree BST( not AVL tree ) 1 2 0 0 0 -1 0 0 0 0 0 0 -1 0 0
  • 10.
    Techniques to make AVLTree: If an insertion of a new node makes an AVL tree unbalanced , we transform the tree by a rotation there are four types of rotation we have: 1. 2. 3. 4. Single right rotation or R-rotation Single left rotation or L-rotation Double right-left rotation or RL-rotation Double left-right rotation or LR-rotation AVL Trees 10
  • 11.
      1.Straight line withpositive unbalanced. apply Right rotation for unbalanced node. +2 +1 0 2 4 5 0 4 0 2 0 5
  • 12.
      2.Straight line withnegative unbalanced. apply left rotation for unbalanced node. 5 -2 0 6 -1 7 0 5 0 6 0 7
  • 13.
      3.Curved line withpositive unbalanced. apply left-right rotation. Right rotation for unbalanced node and left rotation for the nearest node. For example: C,A,B +2 C C 0 B B A -1 B 0 A 0 A C 0
  • 14.
      4.Curved line withnegative unbalanced. apply right-left rotation. Left rotation for unbalanced node and right rotation for the nearest node. For example: A,C,B A -2 C B 0 A 0 B B +1 C 0 A C 0
  • 15.
    Advantages &Disadvantages  Theadvantage of an AVL tree is that it is always balanced, guaranteeing the O(logn) speed of the Binary Search algorithm.  The disadvantages the complex rotations used by the insertion and removal AVL Trees 15
  • 16.
    Running Times forAVL Trees find is O(log n)  height of tree is O(log n) insert is O(log n)  initial find is O(log n) remove is O(log n)  initial remove is O(log n) AVL Trees 16
  • 17.