SlideShare a Scribd company logo
1 of 48
Data Structures
(AVL Tree)
Dr.N.S.Nithya
ASP/CSE
AVL Tree
 The AVL stands for Adelson-
Velskii and Landis, who are the
inventors of the AVL tree.AVL tree is a
self-balancing binary search tree in
which heights of the left sub trees and
right sub trees of any node differ by at
most one.
Balancing Factor
 Balance factor=height of left sub tree –
height of the right sub tree.
 The value of balance factor should always
be -1, 0 or +1.
Need for height balanced
trees
 If the binary search tree is either left skewed or
right skewed(not balanced) , the searching time
will be increased. Because searching time
depends upon the height of the tree. h=log(n)
where h is the height of the tree and n is the
number of nodes in the binary tree.It takes
log(n) for searching.
Balancing Factor Routine
int BF(node *T)
{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
Need for height balanced
trees
 The disadvantage of a binary search tree
is that its height can be as large as N-1
 This means that the time needed to
perform insertion and deletion and many
other operations can be O(N) in the worst
case
 We want a tree with small height
 A binary tree with N node has height at
least Q(log N)
 Thus, our goal is to keep the height of a
binary search tree O(log N)
 Such trees are called balanced binary
How to balanace AVL tree (Rotation)
 An insertion or deletion may cause an
imbalance in an AVL tree.
 An AVL tree causes imbalance when
any of following condition occurs:
 i. An insertion into Right child’s right
subtree.
 ii. An insertion into Left child’s left subtree.
 iii. An insertion into Right child’s left
subtree.
 iv. An insertion into Left child’s right
subtree.
How to balanace AVL tree (Rotation)
 Whenever the tree becomes imbalanced due to
any operation we use rotation operations to
make the tree balanced.
 Rotation is the process of moving nodes
either to left or to right to make the tree
balanced.
Single Left Rotation (LL Rotation)
 If a tree becomes unbalanced, when a node is
inserted into the right subtree of the right
subtree, then we perform a single left rotation .
 If BF(node) = +2 and BF(node -> left-child) = +1, perform LL
rotation.
Single Right Rotation (RR Rotation)
 AVL tree may become unbalanced, if a node
is inserted in the left subtree of the left
subtree. The tree then needs a right rotation.
 If BF(node) = -2 and BF(node -> right-child) = 1, perform RR
rotation.

node * rotateright(node *x)
{
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node * rotateleft(node *x)
{
node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
Left Right Rotation (LR Rotation)
(Double Rotation)
 AVL tree may become unbalanced, if a node is inserte
the left subtree of the right subtree .The tree needs
Rotation is a sequence of single left rotation followed
single right rotation.
 If BF(node) = -2 and BF(node -> right-child) = +1, perform RL rotation.
Right Left Rotation (RL Rotation)
(Double Rotation)
 AVL tree may become unbalanced, if a node is
inserted in the right subtree of the left subtree
.The tree needs The RL Rotation is sequence
of single right rotation followed by single left
rotation.
 If BF(node) = +2 and BF(node -> left-child) = -1, perform LR
rotation.
node * RR(node *T) //insert right subtree of right
{
T=rotateleft(T);
return(T);
}
node * LL(node *T) //insert left subtree of left
{
T=rotateright(T);
return(T);
}
node * LR(node *T) // insert left subtree of right
{
T->left=rotateleft(T->left);
T=rotateright(T);
return(T);
}
node * RL(node *T) //insert right subtree of left
{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}
Left Rotation
Right Rotation
Left-Right Rotation
Right-Left Rotation
Operations on an AVL Tree
 The following operations are performed
on AVL tree...
 Search
 Insertion
 Deletion
 search operation is similar to Binary
search tree.
Insertion Operation in AVL Tree
 Step 1 - Insert the new element into the
tree using Binary Search Tree insertion
logic.
 Step 2 - After insertion, check
the Balance Factor of every node.
 Step 3 - If the Balance Factor of every
node is 0 or 1 or -1 then go for next
operation.
 Step 4 - If the Balance Factor of any
node is other than 0 or 1 or -1 then that
tree is said to be imbalanced. In this
case, perform suitable Rotation to make
it balanced and go for next operation.
Rotating conditions
 If BF(node) = +2 and BF(node -> left-child) =
+1, perform LL rotation.
 If BF(node) = -2 and BF(node -> right-child) =
1, perform RR rotation.
 If BF(node) = -2 and BF(node -> right-child) =
+1, perform RL rotation.
 If BF(node) = +2 and BF(node -> left-child) =
1, perform LR rotation.
An Extended Example
Insert 3,2,1,4,5,6,7, 16,15,14
3
Fig 1
3
2
Fig 2
3
2
1
Fig 3
2
1
3
Fig 4
2
1
3
4
Fig 5
2
1
3
4
5
Fig 6
Single rotation
Single rotation
Insert 3 Insert 2
Insert 1
Insert 4
Insert 5
0
1
0
1
0
2 0
0 0
-
1
-1
0
0
-
2
-
2
-
1 0
0
2
1
4
5
3
Fig 7 6
2
1
4
5
3
Fig 8
4
2
5
6
1 3
Fig 9
4
2
5
6
1 3
7
Fig 10
4
2
6
7
1 3
5
Fig 11
Single rotation
Single rotation
Insert 6
Insert 7
-1
0
0
0
0
0
0
0
-1
-
1
-
2
0
-
1
0
0
0
0
-
1
0
0
0
-
2
-
1
0
0
1
0
0
0
0
0
0
4
2
6
7
1 3
5 16
Fig 12
4
2
6
7
1 3
5 16
15
Fig 13
4
2
6
15
1 3 5
16
7
Fig 14
Double rotation
Insert 16
Insert 15
-1
-
1
-1
0
0
0
0
0
-
2
-
1
-2
-
1
0
1
0
0
0
-
1
0
0
0
-
1
0
0
0
0
5
4
2 7
15
1 3 6
16
14
Fig 16
4
2
6
15
1 3 5
16
7
14
Fig 15
Double rotation
Insert 14
-
2
-2
1
0
0
-1
0
0
0
0
-
1
0
0
0
0
1
0
0 0
0
1
Operations on an AVL Tree
 The following operations are performed
on AVL tree...
 Search
 Insertion
 Deletion
 search operation is similar to Binary
search tree.
node * insert(node *T,int x)
{
if(T==NULL)
{
T=(node*)malloc(sizeof(node));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2)
if(x>T->right->data)
T=RR(T);
else
T=RL(T);
}
else
if(x<T->data)
{
T->left=insert(T->left,x);
if(BF(T)==2)
if(x < T->left->data)
T=LL(T);
else
T=LR(T);
}
T->ht=height(T);
return(T);
}
Deletion in AVL Trees
 Step 1: Find the element in the tree.
 Step 2: Delete the node, as per the
BST Deletion.
 Step 3: Two cases are possible:-
 Case 1: Deleting from the right subtree.
 Case 2: Deleting from left subtree.
Case 1: Deleting from the right
subtree.
 1A. If BF(node) = +2 and BF(node ->
left-child) = +1, perform LL rotation.
 1B. If BF(node) = +2 and BF(node ->
left-child) = -1, perform LR rotation.
 1C. If BF(node) = +2 and BF(node ->
left-child) = 0, perform LL rotation.
Case 2: Deleting from left subtree.
 2A. If BF(node) = -2 and BF(node ->
right-child) = -1, perform RR rotation.
 2B. If BF(node) = -2 and BF(node ->
right-child) = +1, perform RL rotation.
 2C. If BF(node) = -2 and BF(node ->
right-child) = 0, perform RR rotation.
AVL Tree Example:
• Now remove 53(leaf Node)
14
17
7
4
53
11
12
8 13
AVL Tree Example:
• Now remove 53, unbalanced
14
17
7
4
11
12
8 13
AVL Tree Example:
• Balanced! Remove 11(node with two
children)
14
17
7
4
11
12
8
13
AVL Tree Example:
• Remove 11, replace it with the largest in its left
branch tree balanced
14
17
7
4
8
12
13
AVL Tree Example:
• Remove 8(place with inorder predessor)replace
with largest element in the left subtree, unbalanced
14
17
4
7
12
13
AVL Tree Example:
• Remove 8, unbalanced
14
17
4
7
12
13
AVL Tree Example:
• Balanced!!
14
17
4
7
12
13
node * Delete(node *T,int x)
{
node *p;
if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // delete in right
subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance
during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found
if(T->right!=NULL)
{ //delete its inorder succesor
p=p->right;
while(p->left!= NULL)
p=p->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
if(BF(T)==2)//Rebalance during windup
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}
Height calculation
int height(node *T)
{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
Applications of AVL Tree
 AVL trees are used for frequent insertion.
 It is used in Memory management
subsystem of linux kernel to search
memory regions of processes during
preemption.
 Sort the following database:
 Dictioonary
 Google search engine
 some sites to take data faster
 sites which have large amount of data
example is Facebook.
In Class Exercises
 Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25
15
15, 20, 24, 10, 13, 7, 30, 36, 25
20
24
15
20
24
10
13
15
20
24
13
10
13
20
24
15
10
13
20
24
15
10
15, 20, 24, 10, 13, 7, 30, 36, 25
7
13
20
24
15
10
7
30
36
13
20
30
15
10
7
36
24
13
20
30
15
10
7
36
24
15, 20, 24, 10, 13, 7, 30, 36, 25
25
13
20
30
15
10
7
36
24
25
13
24
36
20
10
7
25
30
15
Remove 24 and 20 from the AVL tree.
13
24
36
20
10
7
25
30
15
13
20
36
15
10
7
25
30
13
15
36
10
7
25
30
13
30
36
10
7
25
15
Data structures trees and graphs - AVL  tree.pptx

More Related Content

Similar to Data structures trees and graphs - AVL tree.pptx

Similar to Data structures trees and graphs - AVL tree.pptx (20)

Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdf
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
DS_Mod4_2.pdf
DS_Mod4_2.pdfDS_Mod4_2.pdf
DS_Mod4_2.pdf
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
Avl trees 2
Avl trees 2Avl trees 2
Avl trees 2
 
Avl trees
Avl treesAvl trees
Avl trees
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
AVL Tree.ppt
AVL Tree.pptAVL Tree.ppt
AVL Tree.ppt
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
 
avl insertion-rotation
avl insertion-rotationavl insertion-rotation
avl insertion-rotation
 
data structures
data structuresdata structures
data structures
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
AVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to doAVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to do
 

More from MalligaarjunanN

English article power point presentation eng.pptx
English article power point presentation eng.pptxEnglish article power point presentation eng.pptx
English article power point presentation eng.pptxMalligaarjunanN
 
Digital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptxDigital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptxMalligaarjunanN
 
Technical English grammar and tenses.pptx
Technical English grammar and tenses.pptxTechnical English grammar and tenses.pptx
Technical English grammar and tenses.pptxMalligaarjunanN
 
Polymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptxPolymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptxMalligaarjunanN
 
Chemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptxChemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptxMalligaarjunanN
 
C programming DOC-20230723-WA0001..pptx
C programming  DOC-20230723-WA0001..pptxC programming  DOC-20230723-WA0001..pptx
C programming DOC-20230723-WA0001..pptxMalligaarjunanN
 
Chemistry fluorescent topic chemistry.pptx
Chemistry fluorescent topic  chemistry.pptxChemistry fluorescent topic  chemistry.pptx
Chemistry fluorescent topic chemistry.pptxMalligaarjunanN
 
C programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptxC programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptxMalligaarjunanN
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxMalligaarjunanN
 
Python programming file handling mhhk.pptx
Python programming file handling mhhk.pptxPython programming file handling mhhk.pptx
Python programming file handling mhhk.pptxMalligaarjunanN
 
Computer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptxComputer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptxMalligaarjunanN
 
Data structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptxData structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptxMalligaarjunanN
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxMalligaarjunanN
 
Computer organisation and architecture .
Computer organisation and architecture .Computer organisation and architecture .
Computer organisation and architecture .MalligaarjunanN
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and commentMalligaarjunanN
 
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptxpythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptxMalligaarjunanN
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuplesMalligaarjunanN
 
presentation-130909130658- (1).pdf
presentation-130909130658- (1).pdfpresentation-130909130658- (1).pdf
presentation-130909130658- (1).pdfMalligaarjunanN
 

More from MalligaarjunanN (20)

English article power point presentation eng.pptx
English article power point presentation eng.pptxEnglish article power point presentation eng.pptx
English article power point presentation eng.pptx
 
Digital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptxDigital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptx
 
Technical English grammar and tenses.pptx
Technical English grammar and tenses.pptxTechnical English grammar and tenses.pptx
Technical English grammar and tenses.pptx
 
Polymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptxPolymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptx
 
Chemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptxChemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptx
 
C programming DOC-20230723-WA0001..pptx
C programming  DOC-20230723-WA0001..pptxC programming  DOC-20230723-WA0001..pptx
C programming DOC-20230723-WA0001..pptx
 
Chemistry fluorescent topic chemistry.pptx
Chemistry fluorescent topic  chemistry.pptxChemistry fluorescent topic  chemistry.pptx
Chemistry fluorescent topic chemistry.pptx
 
C programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptxC programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptx
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
 
Python programming file handling mhhk.pptx
Python programming file handling mhhk.pptxPython programming file handling mhhk.pptx
Python programming file handling mhhk.pptx
 
Computer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptxComputer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptx
 
Data structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptxData structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptx
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptx
 
Computer organisation and architecture .
Computer organisation and architecture .Computer organisation and architecture .
Computer organisation and architecture .
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptxpythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
X02PredCalculus.ppt
X02PredCalculus.pptX02PredCalculus.ppt
X02PredCalculus.ppt
 
presentation-130909130658- (1).pdf
presentation-130909130658- (1).pdfpresentation-130909130658- (1).pdf
presentation-130909130658- (1).pdf
 
Presentation (5)-1.pptx
Presentation (5)-1.pptxPresentation (5)-1.pptx
Presentation (5)-1.pptx
 

Recently uploaded

All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...Sérgio Sacani
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡anilsa9823
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)DHURKADEVIBASKAR
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trssuser06f238
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaDashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaPraksha3
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
The Black hole shadow in Modified Gravity
The Black hole shadow in Modified GravityThe Black hole shadow in Modified Gravity
The Black hole shadow in Modified GravitySubhadipsau21168
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 

Recently uploaded (20)

All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 tr
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaDashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
The Black hole shadow in Modified Gravity
The Black hole shadow in Modified GravityThe Black hole shadow in Modified Gravity
The Black hole shadow in Modified Gravity
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 

Data structures trees and graphs - AVL tree.pptx

  • 2. AVL Tree  The AVL stands for Adelson- Velskii and Landis, who are the inventors of the AVL tree.AVL tree is a self-balancing binary search tree in which heights of the left sub trees and right sub trees of any node differ by at most one.
  • 3. Balancing Factor  Balance factor=height of left sub tree – height of the right sub tree.  The value of balance factor should always be -1, 0 or +1.
  • 4. Need for height balanced trees  If the binary search tree is either left skewed or right skewed(not balanced) , the searching time will be increased. Because searching time depends upon the height of the tree. h=log(n) where h is the height of the tree and n is the number of nodes in the binary tree.It takes log(n) for searching.
  • 5. Balancing Factor Routine int BF(node *T) { int lh,rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; if(T->right==NULL) rh=0; else rh=1+T->right->ht; return(lh-rh);
  • 6. Need for height balanced trees  The disadvantage of a binary search tree is that its height can be as large as N-1  This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case  We want a tree with small height  A binary tree with N node has height at least Q(log N)  Thus, our goal is to keep the height of a binary search tree O(log N)  Such trees are called balanced binary
  • 7. How to balanace AVL tree (Rotation)  An insertion or deletion may cause an imbalance in an AVL tree.  An AVL tree causes imbalance when any of following condition occurs:  i. An insertion into Right child’s right subtree.  ii. An insertion into Left child’s left subtree.  iii. An insertion into Right child’s left subtree.  iv. An insertion into Left child’s right subtree.
  • 8. How to balanace AVL tree (Rotation)  Whenever the tree becomes imbalanced due to any operation we use rotation operations to make the tree balanced.  Rotation is the process of moving nodes either to left or to right to make the tree balanced.
  • 9. Single Left Rotation (LL Rotation)  If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then we perform a single left rotation .  If BF(node) = +2 and BF(node -> left-child) = +1, perform LL rotation.
  • 10. Single Right Rotation (RR Rotation)  AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree. The tree then needs a right rotation.  If BF(node) = -2 and BF(node -> right-child) = 1, perform RR rotation. 
  • 11. node * rotateright(node *x) { node *y; y=x->left; x->left=y->right; y->right=x; x->ht=height(x); y->ht=height(y); return(y); } node * rotateleft(node *x) { node *y; y=x->right; x->right=y->left; y->left=x; x->ht=height(x); y->ht=height(y); return(y); }
  • 12. Left Right Rotation (LR Rotation) (Double Rotation)  AVL tree may become unbalanced, if a node is inserte the left subtree of the right subtree .The tree needs Rotation is a sequence of single left rotation followed single right rotation.  If BF(node) = -2 and BF(node -> right-child) = +1, perform RL rotation.
  • 13. Right Left Rotation (RL Rotation) (Double Rotation)  AVL tree may become unbalanced, if a node is inserted in the right subtree of the left subtree .The tree needs The RL Rotation is sequence of single right rotation followed by single left rotation.  If BF(node) = +2 and BF(node -> left-child) = -1, perform LR rotation.
  • 14. node * RR(node *T) //insert right subtree of right { T=rotateleft(T); return(T); } node * LL(node *T) //insert left subtree of left { T=rotateright(T); return(T); } node * LR(node *T) // insert left subtree of right { T->left=rotateleft(T->left); T=rotateright(T); return(T); } node * RL(node *T) //insert right subtree of left { T->right=rotateright(T->right); T=rotateleft(T); return(T); }
  • 18. Operations on an AVL Tree  The following operations are performed on AVL tree...  Search  Insertion  Deletion  search operation is similar to Binary search tree.
  • 19. Insertion Operation in AVL Tree  Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.  Step 2 - After insertion, check the Balance Factor of every node.  Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.  Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to be imbalanced. In this case, perform suitable Rotation to make it balanced and go for next operation.
  • 20. Rotating conditions  If BF(node) = +2 and BF(node -> left-child) = +1, perform LL rotation.  If BF(node) = -2 and BF(node -> right-child) = 1, perform RR rotation.  If BF(node) = -2 and BF(node -> right-child) = +1, perform RL rotation.  If BF(node) = +2 and BF(node -> left-child) = 1, perform LR rotation.
  • 21. An Extended Example Insert 3,2,1,4,5,6,7, 16,15,14 3 Fig 1 3 2 Fig 2 3 2 1 Fig 3 2 1 3 Fig 4 2 1 3 4 Fig 5 2 1 3 4 5 Fig 6 Single rotation Single rotation Insert 3 Insert 2 Insert 1 Insert 4 Insert 5 0 1 0 1 0 2 0 0 0 - 1 -1 0 0 - 2 - 2 - 1 0 0
  • 22. 2 1 4 5 3 Fig 7 6 2 1 4 5 3 Fig 8 4 2 5 6 1 3 Fig 9 4 2 5 6 1 3 7 Fig 10 4 2 6 7 1 3 5 Fig 11 Single rotation Single rotation Insert 6 Insert 7 -1 0 0 0 0 0 0 0 -1 - 1 - 2 0 - 1 0 0 0 0 - 1 0 0 0 - 2 - 1 0 0 1 0 0 0 0 0 0
  • 23. 4 2 6 7 1 3 5 16 Fig 12 4 2 6 7 1 3 5 16 15 Fig 13 4 2 6 15 1 3 5 16 7 Fig 14 Double rotation Insert 16 Insert 15 -1 - 1 -1 0 0 0 0 0 - 2 - 1 -2 - 1 0 1 0 0 0 - 1 0 0 0 - 1 0 0 0 0
  • 24. 5 4 2 7 15 1 3 6 16 14 Fig 16 4 2 6 15 1 3 5 16 7 14 Fig 15 Double rotation Insert 14 - 2 -2 1 0 0 -1 0 0 0 0 - 1 0 0 0 0 1 0 0 0 0 1
  • 25. Operations on an AVL Tree  The following operations are performed on AVL tree...  Search  Insertion  Deletion  search operation is similar to Binary search tree.
  • 26. node * insert(node *T,int x) { if(T==NULL) { T=(node*)malloc(sizeof(node)); T->data=x; T->left=NULL; T->right=NULL; } else if(x > T->data) // insert in right subtree { T->right=insert(T->right,x); if(BF(T)==-2) if(x>T->right->data) T=RR(T); else T=RL(T); }
  • 28. Deletion in AVL Trees  Step 1: Find the element in the tree.  Step 2: Delete the node, as per the BST Deletion.  Step 3: Two cases are possible:-  Case 1: Deleting from the right subtree.  Case 2: Deleting from left subtree.
  • 29. Case 1: Deleting from the right subtree.  1A. If BF(node) = +2 and BF(node -> left-child) = +1, perform LL rotation.  1B. If BF(node) = +2 and BF(node -> left-child) = -1, perform LR rotation.  1C. If BF(node) = +2 and BF(node -> left-child) = 0, perform LL rotation.
  • 30. Case 2: Deleting from left subtree.  2A. If BF(node) = -2 and BF(node -> right-child) = -1, perform RR rotation.  2B. If BF(node) = -2 and BF(node -> right-child) = +1, perform RL rotation.  2C. If BF(node) = -2 and BF(node -> right-child) = 0, perform RR rotation.
  • 31. AVL Tree Example: • Now remove 53(leaf Node) 14 17 7 4 53 11 12 8 13
  • 32. AVL Tree Example: • Now remove 53, unbalanced 14 17 7 4 11 12 8 13
  • 33. AVL Tree Example: • Balanced! Remove 11(node with two children) 14 17 7 4 11 12 8 13
  • 34. AVL Tree Example: • Remove 11, replace it with the largest in its left branch tree balanced 14 17 7 4 8 12 13
  • 35. AVL Tree Example: • Remove 8(place with inorder predessor)replace with largest element in the left subtree, unbalanced 14 17 4 7 12 13
  • 36. AVL Tree Example: • Remove 8, unbalanced 14 17 4 7 12 13
  • 37. AVL Tree Example: • Balanced!! 14 17 4 7 12 13
  • 38. node * Delete(node *T,int x) { node *p; if(T==NULL) { return NULL; } else if(x > T->data) // delete in right subtree { T->right=Delete(T->right,x); if(BF(T)==2) if(BF(T->left)>=0) T=LL(T); else
  • 40. else { //data to be deleted is found if(T->right!=NULL) { //delete its inorder succesor p=p->right; while(p->left!= NULL) p=p->left; T->data=p->data; T->right=Delete(T->right,p->data); if(BF(T)==2)//Rebalance during windup if(BF(T->left)>=0) T=LL(T); else T=LR(T); } else return(T->left); } T->ht=height(T); return(T); }
  • 41. Height calculation int height(node *T) { int lh,rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; if(T->right==NULL) rh=0; else rh=1+T->right->ht; if(lh>rh) return(lh); return(rh);
  • 42. Applications of AVL Tree  AVL trees are used for frequent insertion.  It is used in Memory management subsystem of linux kernel to search memory regions of processes during preemption.  Sort the following database:  Dictioonary  Google search engine  some sites to take data faster  sites which have large amount of data example is Facebook.
  • 43. In Class Exercises  Build an AVL tree with the following values: 15, 20, 24, 10, 13, 7, 30, 36, 25
  • 44. 15 15, 20, 24, 10, 13, 7, 30, 36, 25 20 24 15 20 24 10 13 15 20 24 13 10 13 20 24 15 10
  • 45. 13 20 24 15 10 15, 20, 24, 10, 13, 7, 30, 36, 25 7 13 20 24 15 10 7 30 36 13 20 30 15 10 7 36 24
  • 46. 13 20 30 15 10 7 36 24 15, 20, 24, 10, 13, 7, 30, 36, 25 25 13 20 30 15 10 7 36 24 25 13 24 36 20 10 7 25 30 15
  • 47. Remove 24 and 20 from the AVL tree. 13 24 36 20 10 7 25 30 15 13 20 36 15 10 7 25 30 13 15 36 10 7 25 30 13 30 36 10 7 25 15