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

PPTX
AVL Tree in Data Structure
Vrushali Dhanokar
 
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PPTX
Binary Search Tree
sagar yadav
 
PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPTX
Trees data structure
Sumit Gupta
 
PDF
Binary search tree operations
Kamran Zafar
 
PDF
Binary Search Tree
International Islamic University
 
PPTX
Data Structures : hashing (1)
Home
 
AVL Tree in Data Structure
Vrushali Dhanokar
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Binary Search Tree
sagar yadav
 
Binary search tree(bst)
Hossain Md Shakhawat
 
Trees data structure
Sumit Gupta
 
Binary search tree operations
Kamran Zafar
 
Data Structures : hashing (1)
Home
 

What's hot (20)

PPT
Lec 17 heap data structure
Sajid Marwat
 
PPTX
Data structure - Graph
Madhu Bala
 
PPTX
Doubly Linked List
Ninad Mankar
 
PPT
Graphs In Data Structure
Anuj Modi
 
PPT
Spanning trees
Shareb Ismaeel
 
PPTX
Red black tree in data structure
Vrushali Dhanokar
 
PPTX
Graphs in data structure
hamza javed
 
PPTX
single linked list
Sathasivam Rangasamy
 
PPTX
Threaded Binary Tree
khabbab_h
 
PPT
1.7 avl tree
Krish_ver2
 
PPTX
Indexing structure for files
Zainab Almugbel
 
PPTX
Tree and graph
Muhaiminul Islam
 
PPTX
String matching algorithms
Ashikapokiya12345
 
PPT
Heap sort
Mohd Arif
 
PPTX
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
PPT
1.1 binary tree
Krish_ver2
 
PPT
Indexing and Hashing
sathish sak
 
PPTX
Divide and Conquer - Part 1
Amrinder Arora
 
PPTX
queue & its applications
somendra kumar
 
Lec 17 heap data structure
Sajid Marwat
 
Data structure - Graph
Madhu Bala
 
Doubly Linked List
Ninad Mankar
 
Graphs In Data Structure
Anuj Modi
 
Spanning trees
Shareb Ismaeel
 
Red black tree in data structure
Vrushali Dhanokar
 
Graphs in data structure
hamza javed
 
single linked list
Sathasivam Rangasamy
 
Threaded Binary Tree
khabbab_h
 
1.7 avl tree
Krish_ver2
 
Indexing structure for files
Zainab Almugbel
 
Tree and graph
Muhaiminul Islam
 
String matching algorithms
Ashikapokiya12345
 
Heap sort
Mohd Arif
 
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
1.1 binary tree
Krish_ver2
 
Indexing and Hashing
sathish sak
 
Divide and Conquer - Part 1
Amrinder Arora
 
queue & its applications
somendra kumar
 
Ad

Similar to 4. avl (20)

PPTX
Avl trees final
PRAKASH RANJAN SINGH
 
PDF
DS_Mod4_2.pdf
SankarTerli
 
PPT
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
PPTX
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
 
PPTX
Adelson velskii Landis rotations based on
banupriyar5
 
PPT
lecture18(1) for the data structure .ppt
umakalaimani1
 
PPTX
9.bst(contd.) avl tree
Chandan Singh
 
PDF
Avl trees
Xad Kuain
 
PPTX
AVL Tree.pptx
Trad5
 
PPTX
Lecture 10 data structures and algorithms
Aakash deep Singhal
 
PPT
Presentation1 data structure for CSE.ppt
umakalaimani1
 
PDF
Lect 13, 14 (final)AVL Tree and Rotations.pdf
MuhammadUmerIhtisham
 
PDF
data structure AVL TREES chapter slides for learning about AVL trees
ethar2303338
 
PPT
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
PDF
Study about AVL Tree & Operations
Editor IJCTER
 
PPTX
AVL tree PPT.pptx
SamyakJain710491
 
PPTX
Presentation_30219_Content_Document_20250107125144AM.pptx
chiraglab007
 
PPT
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
PPT
Avl tree
Van Pham
 
Avl trees final
PRAKASH RANJAN SINGH
 
DS_Mod4_2.pdf
SankarTerli
 
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
 
Adelson velskii Landis rotations based on
banupriyar5
 
lecture18(1) for the data structure .ppt
umakalaimani1
 
9.bst(contd.) avl tree
Chandan Singh
 
Avl trees
Xad Kuain
 
AVL Tree.pptx
Trad5
 
Lecture 10 data structures and algorithms
Aakash deep Singhal
 
Presentation1 data structure for CSE.ppt
umakalaimani1
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
MuhammadUmerIhtisham
 
data structure AVL TREES chapter slides for learning about AVL trees
ethar2303338
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
Study about AVL Tree & Operations
Editor IJCTER
 
AVL tree PPT.pptx
SamyakJain710491
 
Presentation_30219_Content_Document_20250107125144AM.pptx
chiraglab007
 
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
Avl tree
Van Pham
 
Ad

More from Rajandeep Gill (7)

PDF
Chronic Kidney Disease Prediction
Rajandeep Gill
 
PPT
Graph traversal-BFS & DFS
Rajandeep Gill
 
PPT
Asymptotic Notation and Complexity
Rajandeep Gill
 
PPTX
Operating system quiz
Rajandeep Gill
 
PPT
Deadlock
Rajandeep Gill
 
PPTX
Software Engineering Methodology
Rajandeep Gill
 
PPTX
Enterprise and Enterprise Application
Rajandeep Gill
 
Chronic Kidney Disease Prediction
Rajandeep Gill
 
Graph traversal-BFS & DFS
Rajandeep Gill
 
Asymptotic Notation and Complexity
Rajandeep Gill
 
Operating system quiz
Rajandeep Gill
 
Deadlock
Rajandeep Gill
 
Software Engineering Methodology
Rajandeep Gill
 
Enterprise and Enterprise Application
Rajandeep Gill
 

Recently uploaded (20)

PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 

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