SlideShare a Scribd company logo
Definition:
 One of the more popular balanced tree known as
an AVL tree.
 It was introduced in 1962 by Adelson - Velskii
and Landis.
 An empty binary tree is an AVL tree. If T is a
nonempty binary tree with TL and TR as its left
and right subtrees, then T is an AVL tree iff,
(i) TL and TR are AVL trees
(ii) |hL - hR|<=1 where hL and hR are the
heights of TL and TR, respectively.
Let Nh be the minimum number of nodes in an
AVL tree of height h.
In the worst case the height of one of the subtrees
is h-1, and the height of the other is h-2.
Both subtrees are also AVL trees.
Nh=Nh-1 + Nh-2 + 1, N0=0, and N1=1
The similarity between this definition for Nh and
the definition of the Fibonacci numbers,
Fn=Fn-1 +Fn-2, F0=0 and F1=1
 AVL Trees are normally represented by using
the linked representation scheme for binary
trees. However, to facilitate insertion and
deletion, a balance factor bf is associated with
each node.
 The balance factor bf(x) of a node x is defined
as
height of left subtree of x – height of
right subtree of x
 From the definition of an AVL tree, it follows
that the permissible balance factors are -1, 0
and 1.
30
5 40
35
20
15
12 18
25
30
-1
0
1
0
0
0
0 0
-1
0
The number outside each node is its balance factor
(a) (b)
The tree contains nodes with balance factors
other than -1,0 and 1, it is not an AVL tree.
When an insertion into an AVL tree using
algorithm then the results in a search tree that
has one or more nodes with balance factors
other than -1,0 and 1, the resulting tree is
unbalanced.
We can restore balance(i.e., make all balance
factors -1, 0 and 1) by shifting some of the
subtrees of the unbalanced tree
30
5 40
35
32
-2
1
2
0
30
5 35
32 40
-1
0 0
0 0
(a)Immediately after insertion (b) Following rebalancing
 Before examining the subtree movement
needed to restore balance, let us make some
observations about the unbalanced tree that
results from an insertion.
 I1 denotes insertion observation 1.
I1: In the unbalanced tree the balance factors
are limited to -2,-1,0,1 and 2.
I2: A node with balance factor 2 had a balance
factor 1 before the insertion.
Similarly, a node with balance factor -2 had a
balance factor -1 before the insertion.
I3: The balance factors of only those nodes on the
path from the root to the newly inserted node can
change as a result of the insertion.
I4: Let A denote the nearest ancestor of the newly
inserted node whose balance factor is either -2 or 2.
The balance factor of all nodes on the path from
A to the newly inserted node was 0 prior to the
insertion.
1 0 2
X X X
XL XR
h-1 h-2
XL X’R
h-1 h-1
X’L XR
h h-2
(a) Before insertion (b) After inserting into XR (c) After inserting into XL
Balance factor of X is inside the node
Subtree heights are below subtree names
 For the balance factor to become 0, the insertion must be
made in XR resulting in an X’R of height h-1.
 The height of X’R must increase to h-1 as all balance
factors on the path from X to the newly inserted node were
0 prior to the insertion.
 The height of X remains h, and the balance factors of the
ancestors of X are the same before and after the insertion,
so the tree is balanced.
A
0
1
A
A 2
AR
B 1 AR
h
0B
B’L
BRBL B’L BR
h+1
0
A
BR AR
h h h+1 h h h
(a) Before insertion (b) After inserting into BL (c) After LL rotation
h
Balance factors are inside nodes
Subtree heights are below subtree names
h
A
0B
2A
B
Cb
-1
0C
B AAR
h
BL BR
hh
BL
h
CL CR
BL CL CR AR
h h
AR
1
(a) Before insertion (b) After inserting into BR (c) After LR rotation
b=0=>bf(B)=bf(A)=0 after rotation
b=1=>bf(B)=0 and bf(A)=-1 after rotation
b=-1=>bf(B)=1 and bf(A)=0 after rotation
 The transformations done to remedy LL and RR
imbalances are often called single rotations, while
those done for LR and RL imbalances are called
double rotations.
 The transformation for an LR imbalance can be viewed
as an RR rotation followed by an LL rotation, while
that for an RL imbalance can be viewed as an LL
rotation followed by an RR rotation.
 A single rotation(LL, LR, RR, or RL) is sufficient to
restore balance if the insertion causes imbalance.
 Let q be the parent of the node that was physically deleted.
 The node containing this element is deleted and the right-
child pointer from the root diverted to the only child of the
element node.
 The parent of the deleted node is the root, so q is the root.
 The nodes on the path from the root q have changed as a
result of the deletion, this path backward from q toward
the root.
 If the deletion took place from the left subtree of q, bf(q)
decreases by 1, and if it took place from the right subtree
bf(q) increases by 1.
 D1 : If the new balance factor of q is 0, its height
has decreased by 1.
 D2 : If the new balance factor of q is either -1 or
1, its height is the same as before the deletion and the
balance factors of its ancestors are unchanged.
 D3 : If the new balance factor of q is either -2 or
2, the tree is unbalanced at q.
2
A’R
BL BR
h h
B
A
h-1
(b) After deletion from AR
-1
1 A
B
BL
h
BR A’R
h h-1
(c) After R0 rotation
1
0
BL BR
h h
AR
h
A
B
(a) Before deletion
An R0 rotation(single rotation)
0
 An R0 imbalanced at A is rectified by performing
the rotation. Notice that the height of the shown
subtree was h + 2 before the deletion and is h + 2
after.
 So the balance factors of the remaining nodes on the
path to the root are unchanged.
 How to handle an R1 imbalance. While the pointer
changes are the same as for an R0 imbalance,
 The new balance factors for A and B are different
and the height of the subtree the notation is now h + 1,
which is 1 less than before the deletion.
1
1
2
1
0
0
A
B
BL
h
BR
h-1
AR
h
A
B A’R
h-1
BL BR
h-1h
B
A
BL
h
BR A’R
h-1 h-1
(a) Before deletion (b) After deletion from AR (c) After R1 rotation
An R1 rotation (single rotation)
 R1 rotation, we must continue to examine nodes
on the path to the root.
 Unlike the case of an insertion, one rotation
may not suffice to restore balance following a
deletion.
 The number of rotation needed is 0(log n).
 The transformation needed when the imbalance
is of type R- 1.
 The balance factors of A and B following the
rotation depend on the balance factor b of the
right child B.
1
-1
b
2
-1
b
0A
B
BL
h - 1
CL CR
C
AR
h
A
B A’R
h - 1
CBL
h - 1
CL
CR
C
B A
BL
h - 1
CL CR A’R
h - 1
(a) Before deletion (b) After deletion from AR
(c) After R – 1 rotation
b =0=> bf(A) = bf(B) = 0 after rotation
b = 1 => bf(A) = -1 and bf(B) = 0 after rotation
b = -1 => bf(A) = 0 and bf(B) = 1 after rotation
An R – 1 rotation (double rotation)
 This rotation leaves behind a
subtree of height h + 1, while the
subtree height prior to the deletion
was h + 2.
 So we need to continue on the
path to the root.
 LL and R1 rotation are identical.
 LL and R0 rotation differ only in
the final balance factors of A and
B and LR and R-1 rotation are
identical.
Avl trees

More Related Content

Similar to Avl trees

9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl tree
Chandan Singh
 
CS-102 AVLSv2.pdf
CS-102 AVLSv2.pdfCS-102 AVLSv2.pdf
CS-102 AVLSv2.pdf
ssuser034ce1
 
CS-102 AVLS.pdf
CS-102 AVLS.pdfCS-102 AVLS.pdf
CS-102 AVLS.pdf
ssuser034ce1
 
lec41.ppt
lec41.pptlec41.ppt
lec41.ppt
SharmilaBP1
 
Avl tree
Avl treeAvl tree
Avl tree
Shankar Bishnoi
 
4. avl
4. avl4. avl
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
 
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
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
Pran K Mohanty
 
Avl excercise
Avl excerciseAvl excercise
Avl excercise
Shipra Swati
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
omkarbhabal
 
Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
Surkhab Shelly
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
Meghaj Mallick
 
972 B3102005 Cullity Chapter 2
972 B3102005 Cullity Chapter 2972 B3102005 Cullity Chapter 2
972 B3102005 Cullity Chapter 2
praying1
 
Avl Tree Implementation
Avl Tree ImplementationAvl Tree Implementation
Avl Tree Implementation
Ehsan Elahi
 
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
 
Avl tree
Avl treeAvl tree
Avl tree
Van Pham
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 

Similar to Avl trees (20)

9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl tree
 
CS-102 AVLSv2.pdf
CS-102 AVLSv2.pdfCS-102 AVLSv2.pdf
CS-102 AVLSv2.pdf
 
CS-102 AVLS.pdf
CS-102 AVLS.pdfCS-102 AVLS.pdf
CS-102 AVLS.pdf
 
lec41.ppt
lec41.pptlec41.ppt
lec41.ppt
 
Avl tree
Avl treeAvl tree
Avl tree
 
4. avl
4. avl4. avl
4. avl
 
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
 
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.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
Avl excercise
Avl excerciseAvl excercise
Avl excercise
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
 
Avl trees
Avl treesAvl trees
Avl trees
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
972 B3102005 Cullity Chapter 2
972 B3102005 Cullity Chapter 2972 B3102005 Cullity Chapter 2
972 B3102005 Cullity Chapter 2
 
Avl Tree Implementation
Avl Tree ImplementationAvl Tree Implementation
Avl Tree Implementation
 
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
 
Avl tree
Avl treeAvl tree
Avl tree
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 

Recently uploaded

Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 

Recently uploaded (20)

Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 

Avl trees

  • 1.
  • 2.
  • 3. Definition:  One of the more popular balanced tree known as an AVL tree.  It was introduced in 1962 by Adelson - Velskii and Landis.  An empty binary tree is an AVL tree. If T is a nonempty binary tree with TL and TR as its left and right subtrees, then T is an AVL tree iff, (i) TL and TR are AVL trees (ii) |hL - hR|<=1 where hL and hR are the heights of TL and TR, respectively.
  • 4. Let Nh be the minimum number of nodes in an AVL tree of height h. In the worst case the height of one of the subtrees is h-1, and the height of the other is h-2. Both subtrees are also AVL trees. Nh=Nh-1 + Nh-2 + 1, N0=0, and N1=1 The similarity between this definition for Nh and the definition of the Fibonacci numbers, Fn=Fn-1 +Fn-2, F0=0 and F1=1
  • 5.  AVL Trees are normally represented by using the linked representation scheme for binary trees. However, to facilitate insertion and deletion, a balance factor bf is associated with each node.  The balance factor bf(x) of a node x is defined as height of left subtree of x – height of right subtree of x  From the definition of an AVL tree, it follows that the permissible balance factors are -1, 0 and 1.
  • 6. 30 5 40 35 20 15 12 18 25 30 -1 0 1 0 0 0 0 0 -1 0 The number outside each node is its balance factor (a) (b)
  • 7. The tree contains nodes with balance factors other than -1,0 and 1, it is not an AVL tree. When an insertion into an AVL tree using algorithm then the results in a search tree that has one or more nodes with balance factors other than -1,0 and 1, the resulting tree is unbalanced. We can restore balance(i.e., make all balance factors -1, 0 and 1) by shifting some of the subtrees of the unbalanced tree
  • 8. 30 5 40 35 32 -2 1 2 0 30 5 35 32 40 -1 0 0 0 0 (a)Immediately after insertion (b) Following rebalancing
  • 9.  Before examining the subtree movement needed to restore balance, let us make some observations about the unbalanced tree that results from an insertion.  I1 denotes insertion observation 1. I1: In the unbalanced tree the balance factors are limited to -2,-1,0,1 and 2. I2: A node with balance factor 2 had a balance factor 1 before the insertion. Similarly, a node with balance factor -2 had a balance factor -1 before the insertion.
  • 10. I3: The balance factors of only those nodes on the path from the root to the newly inserted node can change as a result of the insertion. I4: Let A denote the nearest ancestor of the newly inserted node whose balance factor is either -2 or 2. The balance factor of all nodes on the path from A to the newly inserted node was 0 prior to the insertion.
  • 11. 1 0 2 X X X XL XR h-1 h-2 XL X’R h-1 h-1 X’L XR h h-2 (a) Before insertion (b) After inserting into XR (c) After inserting into XL Balance factor of X is inside the node Subtree heights are below subtree names  For the balance factor to become 0, the insertion must be made in XR resulting in an X’R of height h-1.  The height of X’R must increase to h-1 as all balance factors on the path from X to the newly inserted node were 0 prior to the insertion.  The height of X remains h, and the balance factors of the ancestors of X are the same before and after the insertion, so the tree is balanced.
  • 12. A 0 1 A A 2 AR B 1 AR h 0B B’L BRBL B’L BR h+1 0 A BR AR h h h+1 h h h (a) Before insertion (b) After inserting into BL (c) After LL rotation h Balance factors are inside nodes Subtree heights are below subtree names
  • 13. h A 0B 2A B Cb -1 0C B AAR h BL BR hh BL h CL CR BL CL CR AR h h AR 1 (a) Before insertion (b) After inserting into BR (c) After LR rotation b=0=>bf(B)=bf(A)=0 after rotation b=1=>bf(B)=0 and bf(A)=-1 after rotation b=-1=>bf(B)=1 and bf(A)=0 after rotation
  • 14.  The transformations done to remedy LL and RR imbalances are often called single rotations, while those done for LR and RL imbalances are called double rotations.  The transformation for an LR imbalance can be viewed as an RR rotation followed by an LL rotation, while that for an RL imbalance can be viewed as an LL rotation followed by an RR rotation.  A single rotation(LL, LR, RR, or RL) is sufficient to restore balance if the insertion causes imbalance.
  • 15.  Let q be the parent of the node that was physically deleted.  The node containing this element is deleted and the right- child pointer from the root diverted to the only child of the element node.  The parent of the deleted node is the root, so q is the root.  The nodes on the path from the root q have changed as a result of the deletion, this path backward from q toward the root.  If the deletion took place from the left subtree of q, bf(q) decreases by 1, and if it took place from the right subtree bf(q) increases by 1.
  • 16.  D1 : If the new balance factor of q is 0, its height has decreased by 1.  D2 : If the new balance factor of q is either -1 or 1, its height is the same as before the deletion and the balance factors of its ancestors are unchanged.  D3 : If the new balance factor of q is either -2 or 2, the tree is unbalanced at q.
  • 17. 2 A’R BL BR h h B A h-1 (b) After deletion from AR -1 1 A B BL h BR A’R h h-1 (c) After R0 rotation 1 0 BL BR h h AR h A B (a) Before deletion An R0 rotation(single rotation) 0
  • 18.  An R0 imbalanced at A is rectified by performing the rotation. Notice that the height of the shown subtree was h + 2 before the deletion and is h + 2 after.  So the balance factors of the remaining nodes on the path to the root are unchanged.  How to handle an R1 imbalance. While the pointer changes are the same as for an R0 imbalance,  The new balance factors for A and B are different and the height of the subtree the notation is now h + 1, which is 1 less than before the deletion.
  • 19. 1 1 2 1 0 0 A B BL h BR h-1 AR h A B A’R h-1 BL BR h-1h B A BL h BR A’R h-1 h-1 (a) Before deletion (b) After deletion from AR (c) After R1 rotation An R1 rotation (single rotation)
  • 20.  R1 rotation, we must continue to examine nodes on the path to the root.  Unlike the case of an insertion, one rotation may not suffice to restore balance following a deletion.  The number of rotation needed is 0(log n).  The transformation needed when the imbalance is of type R- 1.  The balance factors of A and B following the rotation depend on the balance factor b of the right child B.
  • 21. 1 -1 b 2 -1 b 0A B BL h - 1 CL CR C AR h A B A’R h - 1 CBL h - 1 CL CR C B A BL h - 1 CL CR A’R h - 1 (a) Before deletion (b) After deletion from AR (c) After R – 1 rotation b =0=> bf(A) = bf(B) = 0 after rotation b = 1 => bf(A) = -1 and bf(B) = 0 after rotation b = -1 => bf(A) = 0 and bf(B) = 1 after rotation An R – 1 rotation (double rotation)
  • 22.  This rotation leaves behind a subtree of height h + 1, while the subtree height prior to the deletion was h + 2.  So we need to continue on the path to the root.  LL and R1 rotation are identical.  LL and R0 rotation differ only in the final balance factors of A and B and LR and R-1 rotation are identical.