SlideShare a Scribd company logo
1 of 12
AVL TREES
• An AVL tree (Adelson-Velskii and Landis' tree, 
named after the inventors) is a self-balancing 
binary search tree. 
• In an AVL tree, the heights of the 
two child subtrees of any node differ by at most 
one; if at any time they differ by more than one, 
rebalancing is done to restore this property. 
• An AVL tree is a BST in which each node has a 
balance factor (Bf) of +1,0 or -1.
• The balance factor of a node is the difference of the 
height of the left subtree (hL)and height of the right 
subtree(hR) 
Bf=hL-hR) 
• if a node has a balance factor of +1 then its subtree 
is said to be left heavy since the height of left 
subtree is greater than then right subtree. 
• if a node has a balance factor of -1 then its subtree 
is said to be right heavy since the height of right 
subtree is greater than then left subtree. 
• If a node has balance factor of 0 then its subtree is 
said to be balanced.
AVL ROTATION 
• Whenever a node is inserted or deleted from an 
AVL tree then the resultant tree may become 
unbalanced.So we must rebalance the tree by 
performing an operation known as AVL rotation. 
• A rotation rebalances a part of AVL tree by 
rearranging node in such a way that BST property is 
preserved. 
• After performing rotation ,the balance factor of 
each node in the rotated subtree becomes either 
+1,0,-1
• AVL Rotations can be classified into following four 
categories. 
• LL (Left-Left) Rotation 
• LR (Left-Right) Rotation 
• RR (Right-Right) Rotation 
• RL (right-Left) Rotation 
• Among these LL and RR rotations are also known as 
single rotation and LR and RL rotations are known 
as double rotation.
• In order to understand when to apply each rotation 
,let us consider a node N that is inserted at its 
correct position in the AVL tree and let P present a 
pivot node which is the nearest ancestor node 
whose balance factor has changed to other than 
+1,0,-1 on inserting the node N.
LL ROTATION 
• LL rotation is performed when the node N is 
inserted in the left subtree of the left child of the 
pivot node P. 
• In order to rebalance the BST using LL rotation ,we 
perform following steps: 
• 1.the right subtree (XR) of the left child (X) of the 
pivot node(P) becomes the left subtree of P 
• 2.P becomes right child of node X. 
• 3.left sub tree (XL) of the node X remains 
unchanged.
RR ROTATIONS 
•RR rotation is performed when a node N is inserted in the 
right subtree of the right child of the pivot node P. In order 
to rebalance this BST using RR rotation, we perform the 
following steps: 
• 1. The left subtree (YL ) of the right child (Y) of the pivot 
node (P) becomes the right subtree of P. 
• 2. P becomes the left child of node Y. 
• 3. Right subtree (YR ) of node Y remains unchanged. 
• After RR rotation , the AVL tree has the same height s it 
had before the insertion of node.
LR ROTATION 
• LR Rotation is performed when node N is inserted 
in the right subtree of the left child of the pivot 
node P. 
• In order to explain LR rotation,let us assume node X 
be the left child of node P,and node Z be the right 
child of node X.The following steps are performed 
in the LR rotation.
ROTATION 1: 
The left subtree of node Z (ZL) becomes the right subtree of 
node X. 
2.The left child (node X) of the pivot node P becomes the 
left child of node Z 
ROTATION 2: 
3.The right subtree of node Z (ZR) becomes the left subtree 
of node P. 
4.P becomes the right child of node Z.
RL ROTATION 
• RL( Right-Left) rotation is just mirror image of LR 
rotation. It is performed when a node N is inserted in the 
left subtree of the right child of the pivot node P. Like 
LR rotation , it is also a double rotation process. 
• Let us assume node Y be the right child of node P and 
node Z be the left child of node Y. The following steps 
are performed in RL rotation:
Rotation 1 
1. The right subtree of node Z (ZR ) becomes the left 
subtree of node Y. 
2. The right child (node Y) of the pivot node P becomes 
the right child of node Z. 
Rotation 2 
3. The left subtree of node Z( ZL ) becomes the right 
subtree of node P. 
4. P becomes the left child of node Z.

More Related Content

Similar to AVL Tree Balancing and Rotations

Similar to AVL Tree Balancing and Rotations (20)

Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
DS_Mod4_2.pdf
DS_Mod4_2.pdfDS_Mod4_2.pdf
DS_Mod4_2.pdf
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
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 trees final
Avl trees finalAvl trees final
Avl trees final
 
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
 
4. avl
4. avl4. avl
4. avl
 
Avl Tree Implementation
Avl Tree ImplementationAvl Tree Implementation
Avl Tree Implementation
 
data structures
data structuresdata structures
data structures
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
Avl trees
Avl treesAvl trees
Avl trees
 
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
 
Avl tree
Avl treeAvl tree
Avl tree
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 

More from Shankar Bishnoi

More from Shankar Bishnoi (6)

Trees
TreesTrees
Trees
 
Heap tree
Heap treeHeap tree
Heap tree
 
trees
trees trees
trees
 
binary search tree
binary search treebinary search tree
binary search tree
 
binary tree
binary treebinary tree
binary tree
 
tree in Data Structures
tree in Data Structurestree in Data Structures
tree in Data Structures
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

AVL Tree Balancing and Rotations

  • 2. • An AVL tree (Adelson-Velskii and Landis' tree, named after the inventors) is a self-balancing binary search tree. • In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. • An AVL tree is a BST in which each node has a balance factor (Bf) of +1,0 or -1.
  • 3. • The balance factor of a node is the difference of the height of the left subtree (hL)and height of the right subtree(hR) Bf=hL-hR) • if a node has a balance factor of +1 then its subtree is said to be left heavy since the height of left subtree is greater than then right subtree. • if a node has a balance factor of -1 then its subtree is said to be right heavy since the height of right subtree is greater than then left subtree. • If a node has balance factor of 0 then its subtree is said to be balanced.
  • 4. AVL ROTATION • Whenever a node is inserted or deleted from an AVL tree then the resultant tree may become unbalanced.So we must rebalance the tree by performing an operation known as AVL rotation. • A rotation rebalances a part of AVL tree by rearranging node in such a way that BST property is preserved. • After performing rotation ,the balance factor of each node in the rotated subtree becomes either +1,0,-1
  • 5. • AVL Rotations can be classified into following four categories. • LL (Left-Left) Rotation • LR (Left-Right) Rotation • RR (Right-Right) Rotation • RL (right-Left) Rotation • Among these LL and RR rotations are also known as single rotation and LR and RL rotations are known as double rotation.
  • 6. • In order to understand when to apply each rotation ,let us consider a node N that is inserted at its correct position in the AVL tree and let P present a pivot node which is the nearest ancestor node whose balance factor has changed to other than +1,0,-1 on inserting the node N.
  • 7. LL ROTATION • LL rotation is performed when the node N is inserted in the left subtree of the left child of the pivot node P. • In order to rebalance the BST using LL rotation ,we perform following steps: • 1.the right subtree (XR) of the left child (X) of the pivot node(P) becomes the left subtree of P • 2.P becomes right child of node X. • 3.left sub tree (XL) of the node X remains unchanged.
  • 8. RR ROTATIONS •RR rotation is performed when a node N is inserted in the right subtree of the right child of the pivot node P. In order to rebalance this BST using RR rotation, we perform the following steps: • 1. The left subtree (YL ) of the right child (Y) of the pivot node (P) becomes the right subtree of P. • 2. P becomes the left child of node Y. • 3. Right subtree (YR ) of node Y remains unchanged. • After RR rotation , the AVL tree has the same height s it had before the insertion of node.
  • 9. LR ROTATION • LR Rotation is performed when node N is inserted in the right subtree of the left child of the pivot node P. • In order to explain LR rotation,let us assume node X be the left child of node P,and node Z be the right child of node X.The following steps are performed in the LR rotation.
  • 10. ROTATION 1: The left subtree of node Z (ZL) becomes the right subtree of node X. 2.The left child (node X) of the pivot node P becomes the left child of node Z ROTATION 2: 3.The right subtree of node Z (ZR) becomes the left subtree of node P. 4.P becomes the right child of node Z.
  • 11. RL ROTATION • RL( Right-Left) rotation is just mirror image of LR rotation. It is performed when a node N is inserted in the left subtree of the right child of the pivot node P. Like LR rotation , it is also a double rotation process. • Let us assume node Y be the right child of node P and node Z be the left child of node Y. The following steps are performed in RL rotation:
  • 12. Rotation 1 1. The right subtree of node Z (ZR ) becomes the left subtree of node Y. 2. The right child (node Y) of the pivot node P becomes the right child of node Z. Rotation 2 3. The left subtree of node Z( ZL ) becomes the right subtree of node P. 4. P becomes the left child of node Z.