SlideShare a Scribd company logo
Binary Search Tree(BST)
AVL Tree
1
Binary Search Tree (BST)
 Binary Search Tree, is a binary tree data structure
which has the following properties:
 The left subtree of a node contains only nodes with
values lesser than the node’s value.
 The right subtree of a node contains only nodes with
value greater than the node’s value.
 The left and right subtree each must also be a binary
search tree.
 There must be no duplicate nodes.
2
Example of BST
3
Skewed BST
 If a tree which is dominated
by left child node or right
child node, is said to be
a Skewed Binary Tree.
 In a left skewed tree, most of
the nodes have the left child
without corresponding right
child.
 In a right skewed tree, most
of the nodes have the right
child without corresponding
left child.
4
Limitation of BST
 The average search time for a binary search tree is
directly proportional to its height: O(h). Most of the
operation average case time is O(log2n).
 BST’s are not guaranteed to be balanced. It may be
skewed tree also.
 For skewed BST, the average search time becomes
O(n). So, it is working like an linear array.
 To improve average search time and make BST
balanced, AVL trees are used.
5
AVL Tree
 AVL tree is a height balanced tree.
 It is a self-balancing binary search tree.
 It was invented by Adelson-Velskii and Landis.
 AVL trees have a faster retrieval.
 It takes O(logn) time for insertion and deletion
operation.
 In AVL tree, difference between heights of left and
right subtree cannot be more than one for all
nodes.
6
AVL Tree
 Balance Factor of node is:
 Height of left subtree – Height of Right subtree
 Balance Factor is calculated for every node of AVL
tree.
 At every node, height of left and right subtree can
differ by no more than 1.
 For AVL tree, the possible values of balance factor
are -1, 0, 1
 Balance Factor of leaf nodes is 0 (zero).
7
Example
 Every AVL Tree is a binary search tree but all the
Binary Search Tree need not to be AVL trees.
BST and AVL
BST but not AVL
8
Finding Balance Factor
BF= hl - hr
9
Height of AVL Tree
 By the definition of complete trees, any complete
binary search tree is an AVL tree
 Thus, an upper bound on the number of nodes in an
AVL tree of height h a perfect binary tree with 2h + 1 – 1
nodes.
 What is a lower bound?
10
Height of AVL Tree
11
Let F(h) be the fewest number of nodes in a tree of
height h.
From a previous slide:
F(0) = 1
F(1) = 2
F(2) = 4
Then what is F(h) in general?
Height of AVL Tree
12
The worst-case AVL tree of height h would have:
 A worst-case AVL tree of height h – 1 on one side,
 A worst-case AVL tree of height h – 2 on the other, and
 The root node
This is a recurrence relation:









11)2F()1F(
12
01
)F(
hhh
h
h
h
We get: F(h) = F(h – 1) + F(h – 2) + 1
Imbalance
 After an insertion, when the balance factor of node A
is –2 or 2, the node A is one of the following four
imbalance types
1. LL: new node is in the left subtree of the left subtree
of A
2. LR: new node is in the right subtree of the left
subtree of A
3. RR: new node is in the right subtree of the right
subtree of A
4. RL: new node is in the left subtree of the right
subtree of A
13
AVL Tree Example
 Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
14
1711
7 53
4
14
177
4 5311
13
14
Types of Rotation
Rotation
Single
Rotation
LL
Rotation
RR
Rotation
Double
Rotation
LR
Rotation
RL
Rotation
15
Rotation- To switch children and parents among two or
three adjacent nodes to restore balance of a tree.
Types of Rotation
 Single Rotation is applied when imbalanced node and
child has same sign of BF (in the direction of new
inserted node).
 LL Rotation is applied in case of +ve sign. It mean left
tree is heavy and so LL rotation is done.
 RR Rotation is applied in case of -ve sign. It mean right
tree is heavy and so RR rotation is done.
 Double Rotation is applied when imbalanced node
and child has different signs of BF (in the direction of
new inserted node).
16
LL Rotation
Imbalanced AVL Tree LL Rotation Balanced AVL Tree
Insert 3,2,1 in AVL Tree
17
L
L
RR Rotation
Imbalanced AVL Tree RR Rotation Balanced AVL Tree
Insert 1,2,3 in AVL Tree
18
R
R
LR Rotation
Imbalanced AVL Tree RR Rotation LL Rotation Balanced AVL
Tree
Insert 3, 1, 2 in AVL Tree
19
L
R
RL Rotation
Imbalanced AVL Tree LL Rotation RR Rotation Balanced AVL
Tree
Insert 1, 3, 2 in AVL Tree
20
L
R
Construct a AVL Tree by inserting
from 1 to 5 numbers
1 0
1
2
3
1
2
2 3
0
-1
-1 -2
0
Not AVL
Apply RR Rotation
21
Construct a AVL Tree by inserting
from 1 to 5 numbers
1
2
3
After Rotation
0
0
0
4
1
2
3
4
0
0
-1
-1
1
2
3
4
0
-1
-2
-2
5
5 0
22
Construct a AVL Tree by inserting
from 1 to 5 numbers
1
2
3
4
0
0
-1
5 0
0
AVL Tree
23
Construct
AVL Tree
with data
items: 51, 26,
11, 6, 8, 4, 31
24
Insertion in AVL Tree
Insert 2
LL Rotation
25
Insertion in AVL Tree
Insert 4
LR Rotation
(3, 5, 6)
26
Deletion in AVL Tree
27
Delete 8
Balanced AVL Balanced AVL
It is also possible to delete an item from AVL Tree.
Deletion in AVL Tree
28
Delete 8
Balanced AVL Imbalanced AVL
Just like insertion, deletion can cause an imbalance,
which will need to be fixed by applying one of the
four rotations.
R1 Rotation
Deletion in AVL Tree
 The deletion is also the same as in BST. However, in
imbalanced tree due to deletion, one or more rotations
need to be applied to balance the AVL tree.
 The Right(R) imbalance is classified into R0, R1, R-1
 The Left(L) imbalance is classified into L0, L1, L-1
29
Deletion in AVL Tree
 LL Rotation is same to R0 and R1
 RR Rotation is same to L0 and L-1
 LR Rotation is same to R-1
 RL Rotation is same to L1
30
R0 Rotation
B
AR
h
BL BR
(0)
(+1)
A
Delete node X
h
x
B
AR
h
(0)
(+2)
A
BL BR
Unbalanced AVL
search treeafter
deletion of node x
h -1
R0 Rotation
R0 Rotation
Balanced AVL
search treeafter
rotation
B
AR
h
(0)
(+2)
A
BL BR
Unbalanced AVL
search treeafter
deletion of x
AR
h
BR
(+1)
A
BL
(-1) B
BF(B) == 0, use
R0 rotation
R0 Rotation Example
Unbalanced AVL search
tree after deletion
(0)
(0)
(+1)
46
20 54
(-1)
Delete 60
(+1)
18
7(0)
23 (-1)
60
(0) 24
(0)
(+2)
46
20
(0)
54
(-1)(+1)
18 23
7(0) (0) 24
R0 Rotation Example
R0
(0)
(+2)
46
20
(0)
54
(+1)
18
7(0)
23 (-1)
(0) 24
Balanced AVL search tree
afterdeletion
(+1)
(-1)
20
18
23
(-1)(0)
7
(+1)
46
(0)
54
(0) 24
R1 Rotation
h -1
(+1) B
(+1)
A
Delete node X
h AR
x
h
BL BR
h
BL BR
(+1) B
(+2)
A
Unbalanced AVL
search tree after
deletion of node x
h -1 AR
h -1
R1 Rotation
R1 Rotation
Balanced AVL
search treeafter
rotation
h -1
BL BR
(+1) B
(+2)
A
R
h-1 A
BR
(0)
A
B
BL
(0)
BF(B) == 1, use
R1 rotation
h
h -1 AR
R1 Rotation Example
Unbalanced AVL search
tree after deletion
(+1)
37
(+1)
26 41
(+1)
Delete 39
(+1)
18
(0) 16
28
39 (0)
(0)
(+1)
(+2)
37
26
(0)
41
(0)
(+1)
18 28
(0) 16
R1 Rotation Example
Balanced AVL search tree
afterdeletion
(+2)
37
(+1)
26
28 (0)
(0)
41
(+1)
18
(0) 16
(+1)
(0)
26
18
16 28
(0)
(0)
37
R1 Rotation
(0)
41
(0)
DeleteX
BL
C (0)
(-1)
B
h-1
h
R-1 Rotation
A (+1)
CL CR AR
x
C
CL CR
(0)
(-1)
B
Unbalanced AVL
search treeafter
deletion
BL
(+2)
A
AR
h-1
R-1 Rotation
R -1
BL
C (0)
(-1)
B
h-1
h -1
A (+2)
CL CR AR
CR
(+1)
BL
(+2)
A
ARBF(B) == -1,
use R-1 rotation
C
B
CL
(0)
R-1 Rotation
R -1
CL
CR
(0)
Balanced AVL search
treeafter Rotation
BL
(0)
C
AR
(0)
B
h -1
A
h -1
(0)
R-1 Rotation Example
(+1)
44
(-1)
22
(-1) Delete 52
48
(0)
18 5228 (0)
2923
(+2)
44
(-1)
22
28 (0)
(0)
48
(0)
18
Unbalanced AVL search
tree after deletion
23 (0) 29
R-1 Rotation Example
(+2)
44
(-1)
22
(-1) R-1 Rotation
48
(0)
18 28 (0)
2923
(+2)
44
(+1)
28
29 (0)
(0)
48
(0)
22
Unbalanced AVL search
tree after deletion
23 (0)
(0)
18
R-1 Rotation Example
(0)
(0)
28
22
48
(0)
44
Balanced AVL search tree
after rotation
(0)(0) (0)
18 23
29
L0 Rotation
AL
h
BL BR
B (0)
(-1)
A
Delete X
h
x
BL BR
B
(0)
(-2)
A
h
Unbalanced AVL
search treeafter
deletion
AL
h-1
L0 Rotation
AL
h
BL BR
(-2)
A
B (0)
L0 Rotation
h -1
BR
(0)
(+1)
B
h
BL
Balanced AVL
search treeafter
deletion
h-1
(-1)
AL
A
L0 Rotation
47
(-1)
5450
48
5244 (0)
(-1)
(0)
(1)
22(0)
(-2)
5450
48
5244 (0)
(-1)(1)
(0)
(0)56 56
49 49(0)
(1)
Delete 22
(0)
L0 Rotation
48
(1)
(1)
5448
52
44
(0)
(0)
49
5650
(-1)
(0)
(-1)
L0 Rotation
L1 Rotation
L
h-1
CL
BR
B (+1)
(-1)
A
Delete X
h
x
A
C
R
h-1
(0)
C
R
B
(+1)
(-2)
A
h-1
B
search treeafter
deletion
AL
h-1
CL CR
Unbalanced AVL
(0)
C
L1 Rotation
AL
h-1
CL
BR
B (+1)
(-2)
A
L1 Rotation
h-1
C
R
h-1
(0)
C
(0)
A B
CL
R
(0)
(0)
C
h-1
B
Unbalanced AVL
search treeafter
deletion
AL
h-1
C
R
L1 Rotation
51
(-2)
5250
48
5144 (1)
(0)
(1)
(0)
Delete 22
(-1)
5250
44 (1)
(0)
(0)
(1)
49
48
51
22
(-1)
(0) 49(0)
L1 Rotation
52
5044
48 (-2)
(-1)
51
(0)
(0)
(-1)
L1 Rotation
52 (0)
49
L1 Rotation
53
5148
50 (0)
(-1)
49 5244
(0)
(0) (0)
(0)
L1 Rotation
L-1 Rotation
AL
h
BL BR
B (-1)
(-1)
A
Delete X
h
x
h-1
BL BR
B
(-1)
(-2)
A
h
Unbalanced AVL
search treeafter
deletion
AL
h-1
h-1
L-1 Rotation
AL
h
BL BR
B (-1)
(-2)
A
L-1 Rotation
h-1 A
BR
(-1)
(0)
B
h
Balanced AVL
search treeafter
deletion
AL
h-1 BL
h-1
56
(1)
22
(-1) Delete 18
48
(0)
18 52
(-1)
44
5450
(0)
22
(-1)
(-2)
44
5450
48
5247
47
L-1 Rotation
(0)
(0)
(0)
(0)
(0)
(0)
(0)
(0)
L-1 Rotation
57
(0)
5450
48
5244 (0)
(0)
(0)
(0)
4722(0)
L-1 Rotation
Summary
 Binary Search Tree and its Limitation
 AVL Tree
 Definition
 Rotation & its Types
 Insertion
 Deletion
58

More Related Content

What's hot

B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
Ashish Arun
 
Schedule in DBMS
Schedule in DBMSSchedule in DBMS
Schedule in DBMS
PratibhaRashmiSingh
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
ShahDhruv21
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Multiplication algorithm
Multiplication algorithmMultiplication algorithm
Multiplication algorithm
Gaurav Subham
 
Serializability
SerializabilitySerializability
Serializability
Pyingkodi Maran
 
Digital search tree
Digital search treeDigital search tree
Digital search tree
Mahmudul Hasan
 
Hashing
HashingHashing
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
Preetham Devisetty
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
SeethaDinesh
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
Topological Sort
Topological SortTopological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMSkoolkampus
 
Linked lists
Linked listsLinked lists
Linked lists
SARITHA REDDY
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
Aamir Sohail
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
Keval Bhogayata
 
Expression trees
Expression treesExpression trees
Expression trees
Salman Vadsarya
 

What's hot (20)

B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Schedule in DBMS
Schedule in DBMSSchedule in DBMS
Schedule in DBMS
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Multiplication algorithm
Multiplication algorithmMultiplication algorithm
Multiplication algorithm
 
Serializability
SerializabilitySerializability
Serializability
 
Digital search tree
Digital search treeDigital search tree
Digital search tree
 
Hashing
HashingHashing
Hashing
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Linked list
Linked list Linked list
Linked list
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Linked lists
Linked listsLinked lists
Linked lists
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 
Expression trees
Expression treesExpression trees
Expression trees
 

Similar to 4. avl

Avl trees final
Avl trees finalAvl trees final
Avl trees final
PRAKASH RANJAN SINGH
 
DS_Mod4_2.pdf
DS_Mod4_2.pdfDS_Mod4_2.pdf
DS_Mod4_2.pdf
SankarTerli
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 
9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl tree
Chandan Singh
 
Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
Trad5
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsAakash deep Singhal
 
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
MuhammadUmerIhtisham
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
Editor IJCTER
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
SamyakJain710491
 
Avl tree
Avl treeAvl tree
Avl tree
Van Pham
 
Avl tree
Avl treeAvl tree
Avl tree
Shankar Bishnoi
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
meenamadhuvandhi2
 
TREES.pptx
TREES.pptxTREES.pptx
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
avl.ppt
avl.pptavl.ppt
avl.ppt
cotisa2402
 

Similar to 4. avl (20)

Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
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
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl tree
 
Avl trees
Avl treesAvl trees
Avl trees
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
 
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 PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
 
Avl tree
Avl treeAvl tree
Avl tree
 
Avltrees
AvltreesAvltrees
Avltrees
 
Avl tree
Avl treeAvl tree
Avl tree
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 

More from Rajandeep Gill

Chronic Kidney Disease Prediction
Chronic Kidney Disease PredictionChronic Kidney Disease Prediction
Chronic Kidney Disease Prediction
Rajandeep Gill
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
Rajandeep Gill
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
Rajandeep Gill
 
Operating system quiz
Operating system quizOperating system quiz
Operating system quiz
Rajandeep Gill
 
Deadlock
DeadlockDeadlock
Deadlock
Rajandeep Gill
 
Software Engineering Methodology
Software Engineering MethodologySoftware Engineering Methodology
Software Engineering Methodology
Rajandeep Gill
 
Enterprise and Enterprise Application
Enterprise and Enterprise ApplicationEnterprise and Enterprise Application
Enterprise and Enterprise Application
Rajandeep Gill
 

More from Rajandeep Gill (7)

Chronic Kidney Disease Prediction
Chronic Kidney Disease PredictionChronic Kidney Disease Prediction
Chronic Kidney Disease Prediction
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Operating system quiz
Operating system quizOperating system quiz
Operating system quiz
 
Deadlock
DeadlockDeadlock
Deadlock
 
Software Engineering Methodology
Software Engineering MethodologySoftware Engineering Methodology
Software Engineering Methodology
 
Enterprise and Enterprise Application
Enterprise and Enterprise ApplicationEnterprise and Enterprise Application
Enterprise and Enterprise Application
 

Recently uploaded

Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 

Recently uploaded (20)

Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 

4. avl

  • 2. Binary Search Tree (BST)  Binary Search Tree, is a binary tree data structure which has the following properties:  The left subtree of a node contains only nodes with values lesser than the node’s value.  The right subtree of a node contains only nodes with value greater than the node’s value.  The left and right subtree each must also be a binary search tree.  There must be no duplicate nodes. 2
  • 4. Skewed BST  If a tree which is dominated by left child node or right child node, is said to be a Skewed Binary Tree.  In a left skewed tree, most of the nodes have the left child without corresponding right child.  In a right skewed tree, most of the nodes have the right child without corresponding left child. 4
  • 5. Limitation of BST  The average search time for a binary search tree is directly proportional to its height: O(h). Most of the operation average case time is O(log2n).  BST’s are not guaranteed to be balanced. It may be skewed tree also.  For skewed BST, the average search time becomes O(n). So, it is working like an linear array.  To improve average search time and make BST balanced, AVL trees are used. 5
  • 6. AVL Tree  AVL tree is a height balanced tree.  It is a self-balancing binary search tree.  It was invented by Adelson-Velskii and Landis.  AVL trees have a faster retrieval.  It takes O(logn) time for insertion and deletion operation.  In AVL tree, difference between heights of left and right subtree cannot be more than one for all nodes. 6
  • 7. AVL Tree  Balance Factor of node is:  Height of left subtree – Height of Right subtree  Balance Factor is calculated for every node of AVL tree.  At every node, height of left and right subtree can differ by no more than 1.  For AVL tree, the possible values of balance factor are -1, 0, 1  Balance Factor of leaf nodes is 0 (zero). 7
  • 8. Example  Every AVL Tree is a binary search tree but all the Binary Search Tree need not to be AVL trees. BST and AVL BST but not AVL 8
  • 10. Height of AVL Tree  By the definition of complete trees, any complete binary search tree is an AVL tree  Thus, an upper bound on the number of nodes in an AVL tree of height h a perfect binary tree with 2h + 1 – 1 nodes.  What is a lower bound? 10
  • 11. Height of AVL Tree 11 Let F(h) be the fewest number of nodes in a tree of height h. From a previous slide: F(0) = 1 F(1) = 2 F(2) = 4 Then what is F(h) in general?
  • 12. Height of AVL Tree 12 The worst-case AVL tree of height h would have:  A worst-case AVL tree of height h – 1 on one side,  A worst-case AVL tree of height h – 2 on the other, and  The root node This is a recurrence relation:          11)2F()1F( 12 01 )F( hhh h h h We get: F(h) = F(h – 1) + F(h – 2) + 1
  • 13. Imbalance  After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types 1. LL: new node is in the left subtree of the left subtree of A 2. LR: new node is in the right subtree of the left subtree of A 3. RR: new node is in the right subtree of the right subtree of A 4. RL: new node is in the left subtree of the right subtree of A 13
  • 14. AVL Tree Example  Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 1711 7 53 4 14 177 4 5311 13 14
  • 15. Types of Rotation Rotation Single Rotation LL Rotation RR Rotation Double Rotation LR Rotation RL Rotation 15 Rotation- To switch children and parents among two or three adjacent nodes to restore balance of a tree.
  • 16. Types of Rotation  Single Rotation is applied when imbalanced node and child has same sign of BF (in the direction of new inserted node).  LL Rotation is applied in case of +ve sign. It mean left tree is heavy and so LL rotation is done.  RR Rotation is applied in case of -ve sign. It mean right tree is heavy and so RR rotation is done.  Double Rotation is applied when imbalanced node and child has different signs of BF (in the direction of new inserted node). 16
  • 17. LL Rotation Imbalanced AVL Tree LL Rotation Balanced AVL Tree Insert 3,2,1 in AVL Tree 17 L L
  • 18. RR Rotation Imbalanced AVL Tree RR Rotation Balanced AVL Tree Insert 1,2,3 in AVL Tree 18 R R
  • 19. LR Rotation Imbalanced AVL Tree RR Rotation LL Rotation Balanced AVL Tree Insert 3, 1, 2 in AVL Tree 19 L R
  • 20. RL Rotation Imbalanced AVL Tree LL Rotation RR Rotation Balanced AVL Tree Insert 1, 3, 2 in AVL Tree 20 L R
  • 21. Construct a AVL Tree by inserting from 1 to 5 numbers 1 0 1 2 3 1 2 2 3 0 -1 -1 -2 0 Not AVL Apply RR Rotation 21
  • 22. Construct a AVL Tree by inserting from 1 to 5 numbers 1 2 3 After Rotation 0 0 0 4 1 2 3 4 0 0 -1 -1 1 2 3 4 0 -1 -2 -2 5 5 0 22
  • 23. Construct a AVL Tree by inserting from 1 to 5 numbers 1 2 3 4 0 0 -1 5 0 0 AVL Tree 23
  • 24. Construct AVL Tree with data items: 51, 26, 11, 6, 8, 4, 31 24
  • 25. Insertion in AVL Tree Insert 2 LL Rotation 25
  • 26. Insertion in AVL Tree Insert 4 LR Rotation (3, 5, 6) 26
  • 27. Deletion in AVL Tree 27 Delete 8 Balanced AVL Balanced AVL It is also possible to delete an item from AVL Tree.
  • 28. Deletion in AVL Tree 28 Delete 8 Balanced AVL Imbalanced AVL Just like insertion, deletion can cause an imbalance, which will need to be fixed by applying one of the four rotations. R1 Rotation
  • 29. Deletion in AVL Tree  The deletion is also the same as in BST. However, in imbalanced tree due to deletion, one or more rotations need to be applied to balance the AVL tree.  The Right(R) imbalance is classified into R0, R1, R-1  The Left(L) imbalance is classified into L0, L1, L-1 29
  • 30. Deletion in AVL Tree  LL Rotation is same to R0 and R1  RR Rotation is same to L0 and L-1  LR Rotation is same to R-1  RL Rotation is same to L1 30
  • 31. R0 Rotation B AR h BL BR (0) (+1) A Delete node X h x B AR h (0) (+2) A BL BR Unbalanced AVL search treeafter deletion of node x h -1
  • 32. R0 Rotation R0 Rotation Balanced AVL search treeafter rotation B AR h (0) (+2) A BL BR Unbalanced AVL search treeafter deletion of x AR h BR (+1) A BL (-1) B BF(B) == 0, use R0 rotation
  • 33. R0 Rotation Example Unbalanced AVL search tree after deletion (0) (0) (+1) 46 20 54 (-1) Delete 60 (+1) 18 7(0) 23 (-1) 60 (0) 24 (0) (+2) 46 20 (0) 54 (-1)(+1) 18 23 7(0) (0) 24
  • 34. R0 Rotation Example R0 (0) (+2) 46 20 (0) 54 (+1) 18 7(0) 23 (-1) (0) 24 Balanced AVL search tree afterdeletion (+1) (-1) 20 18 23 (-1)(0) 7 (+1) 46 (0) 54 (0) 24
  • 35. R1 Rotation h -1 (+1) B (+1) A Delete node X h AR x h BL BR h BL BR (+1) B (+2) A Unbalanced AVL search tree after deletion of node x h -1 AR h -1
  • 36. R1 Rotation R1 Rotation Balanced AVL search treeafter rotation h -1 BL BR (+1) B (+2) A R h-1 A BR (0) A B BL (0) BF(B) == 1, use R1 rotation h h -1 AR
  • 37. R1 Rotation Example Unbalanced AVL search tree after deletion (+1) 37 (+1) 26 41 (+1) Delete 39 (+1) 18 (0) 16 28 39 (0) (0) (+1) (+2) 37 26 (0) 41 (0) (+1) 18 28 (0) 16
  • 38. R1 Rotation Example Balanced AVL search tree afterdeletion (+2) 37 (+1) 26 28 (0) (0) 41 (+1) 18 (0) 16 (+1) (0) 26 18 16 28 (0) (0) 37 R1 Rotation (0) 41 (0)
  • 39. DeleteX BL C (0) (-1) B h-1 h R-1 Rotation A (+1) CL CR AR x C CL CR (0) (-1) B Unbalanced AVL search treeafter deletion BL (+2) A AR h-1
  • 40. R-1 Rotation R -1 BL C (0) (-1) B h-1 h -1 A (+2) CL CR AR CR (+1) BL (+2) A ARBF(B) == -1, use R-1 rotation C B CL (0)
  • 41. R-1 Rotation R -1 CL CR (0) Balanced AVL search treeafter Rotation BL (0) C AR (0) B h -1 A h -1 (0)
  • 42. R-1 Rotation Example (+1) 44 (-1) 22 (-1) Delete 52 48 (0) 18 5228 (0) 2923 (+2) 44 (-1) 22 28 (0) (0) 48 (0) 18 Unbalanced AVL search tree after deletion 23 (0) 29
  • 43. R-1 Rotation Example (+2) 44 (-1) 22 (-1) R-1 Rotation 48 (0) 18 28 (0) 2923 (+2) 44 (+1) 28 29 (0) (0) 48 (0) 22 Unbalanced AVL search tree after deletion 23 (0) (0) 18
  • 44. R-1 Rotation Example (0) (0) 28 22 48 (0) 44 Balanced AVL search tree after rotation (0)(0) (0) 18 23 29
  • 45. L0 Rotation AL h BL BR B (0) (-1) A Delete X h x BL BR B (0) (-2) A h Unbalanced AVL search treeafter deletion AL h-1
  • 46. L0 Rotation AL h BL BR (-2) A B (0) L0 Rotation h -1 BR (0) (+1) B h BL Balanced AVL search treeafter deletion h-1 (-1) AL A
  • 47. L0 Rotation 47 (-1) 5450 48 5244 (0) (-1) (0) (1) 22(0) (-2) 5450 48 5244 (0) (-1)(1) (0) (0)56 56 49 49(0) (1) Delete 22 (0)
  • 49. L1 Rotation L h-1 CL BR B (+1) (-1) A Delete X h x A C R h-1 (0) C R B (+1) (-2) A h-1 B search treeafter deletion AL h-1 CL CR Unbalanced AVL (0) C
  • 50. L1 Rotation AL h-1 CL BR B (+1) (-2) A L1 Rotation h-1 C R h-1 (0) C (0) A B CL R (0) (0) C h-1 B Unbalanced AVL search treeafter deletion AL h-1 C R
  • 51. L1 Rotation 51 (-2) 5250 48 5144 (1) (0) (1) (0) Delete 22 (-1) 5250 44 (1) (0) (0) (1) 49 48 51 22 (-1) (0) 49(0)
  • 53. L1 Rotation 53 5148 50 (0) (-1) 49 5244 (0) (0) (0) (0) L1 Rotation
  • 54. L-1 Rotation AL h BL BR B (-1) (-1) A Delete X h x h-1 BL BR B (-1) (-2) A h Unbalanced AVL search treeafter deletion AL h-1 h-1
  • 55. L-1 Rotation AL h BL BR B (-1) (-2) A L-1 Rotation h-1 A BR (-1) (0) B h Balanced AVL search treeafter deletion AL h-1 BL h-1
  • 56. 56 (1) 22 (-1) Delete 18 48 (0) 18 52 (-1) 44 5450 (0) 22 (-1) (-2) 44 5450 48 5247 47 L-1 Rotation (0) (0) (0) (0) (0) (0) (0) (0)
  • 58. Summary  Binary Search Tree and its Limitation  AVL Tree  Definition  Rotation & its Types  Insertion  Deletion 58