You are on page 1of 61

AVL

• Adelson-Velskii and Landis trees (AVL Tree).


• A self-balancing Binary Search Tree (BST) where the sub-trees of each
node can differ by at most 1 in their heights.
• Balance factor of a node:

height(left subtree) - height(right subtree)<= +1, 0,


-1
AVL Property
1) The sub-trees of every node differ in height by at most one (i.e) -1,0,1.
2) Every sub-tree is an AVL tree.
AVLTree
7

5 9
Because difference between heights of left
and right subtrees for every
4 6 1
node is EQUAL TO -1 , 0 , 1

3
Not AVLTree 7

5 9

Because difference between heights of left 4 6 1


and right subtrees for 5 and 9 is GREATER
THAN 1
3 8

2
Why AVL Tree?
1. Most of the BST operations like search, max, min, insert, delete etc.. will take
O(h) time where h is the height of the BST.
2. The cost of these operations may become O(n) for a skewed Binary tree.
3. Make sure that height of the tree remains O(Logn) after every insertion and
deletion, then an upper bound of O(Logn) for all these operations are
guaranteed.
4. The height of an AVL tree is always O(Logn) where n is the number of nodes
in the tree.
AVLTree Rotations
To balance itself, an AVL tree may perform the following four kinds of
rotations :

1. Left rotation

2. Right rotation
3. Left-Right rotation

4. Right-Left rotation
Left Rotation

C
Left Rotation

C
Left Rotation

A C
Right Rotation

A
Right Rotation

A
Right Rotation

A C
Left Right Rotation

B
Left Right Rotation

B
Left Right Rotation

A
Left Right Rotation

A
Left Right Rotation

A C
Right Left Rotation

B
Right Left Rotation

B
Right Left Rotation

C
Right Left Rotation

C
Right Left Rotation

C
A
AVL Tree

Single rotations : 14, 15, 16, 13, 12, 11, 10


Insert 14

14
Insert 15

14

15
Insert 16

14

15

16
Left Rotation

14

15

16
Left Rotation 15

14 16
Insert 13 15

14 16

13
Insert 12 15

14 16

13

12
Right Rotation 15

14 16

13

12
Right Rotation 15

13 16

12 14
Insert 11 15

13 16

12 14

11
Right Rotation 15

13 16

12 14

11
Right Rotation 13

12 15

11 14 16
Insert 10 13

12 15

11 14 16

10
Right Rotation 13

12 15

11 14 16

10
Right Rotation 13

11 15

14 16
10 12
AVL Tree

Double rotations : 1, 2, 3, 4
Insert 1
13

11 15

10 12 14 16

1
Insert 2
13

11 15

10 12 14 16

2
Left Right Rotate
13

11 15

10 12 14 16

2
Left Right Rotate
13

11 15

2 12 14 16

1 10
Insert 3 13

11 15

2 12 14 16

1 10

3
Left Right Rotation
13

11 15

2 12 14 16

1 10

3
Left Right Rotation
13

10 15

2 11 14 16

12
1 3
Insert 4 13

10 15

2 11 14 16

12
1 3

4
Right Left Rotation
13

10 15

2 11 14 16

12
1 3

4
Right Left Rotation
10

2 13

1 3 11 15

4 12 14 16
Application

Memory management subsystem of linux kernel to search memory


regions of processes during preemption.
Advantages

1. Low cost
2. Efficient
3. Easy to access
4. Easy to Understand
Disadvantage

Consume more Time for Insertion and Deletion


Red Black tree 13

Every node has color either red or black 10 15

Root of tree is always black


NULL 16
14
There are no two adjacent red nodes
NULL 16
14 16
A red node cannot have a red parent or red child
NULL NULL
NULL
NULL
Why Red Black tree?
1. Most of the BST operations like search, max, min, insert, delete etc.. will take
O(h) time where h is the height of the BST.
2. The cost of these operations may become O(n) for a skewed Binary tree.
3. Make sure that height of the tree remains O(Logn) after every insertion and
deletion, then an upper bound of O(Logn) for all these operations are
guaranteed.
4. The height of an Red black tree is always O(Logn) where n is the number of
nodes in the tree.
Why Red Black tree?
Red black tree AVL tree
It provide faster insertion and It may cause more rotations
removal operations during insertion and deletion
Searching is lower than AVL AVL trees provide faster
lookups than Red Black
Red Black Trees are used in most AVL trees are used
of the language libraries like map, in databases where faster
multiset in C++ retrievals are required.
Real time problem
Telephone EMP Name City
Number
1) Insert Employee Record 8976543634 John Chennai

2) Search for an Employee 9876543423 Jerry Banglore

3) Delete Employee Record

How will you done this operations in most efficient mannner?


Real time problem
Telephone EMP Name City
Number
1) Array 8976543634 John Chennai

- Search takes linear time 9876543423 Jerry Banglore

- Stored in sorted order, search can


be in O(log n) but then insertion and deletion becomes costly
Real time problem
Telephone EMP Name City
Number
2) Linked List 8976543634 John Chennai

- Search takes linear time 9876543423 Jerry Banglore


Real time problem
Telephone EMP Name City
Number
3) Balanced Binary Search tree 8976543634 John Chennai

- Insertion O(log n) 9876543423 Jerry Banglore

- Search O(log n)
- Deletion O(log n)
Real time problem
Telephone EMP Name City
Number
4) Direct Access Table 8976543634 John Chennai

- Insertion O(1) 9876543423 Jerry Banglore

- Search O(1)
Limitation:
- Deletion O(1) 1)Size of table: (m * 10 ^ n)
m -> size of the pointer to the record
n -> digits in the telephone number

2) Integer may not hold the number

You might also like