SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
Introduction
Insertion
Deletion
Balance Binary Search Tree
Worst case height of binary search tree: N
Insertion, deletion can be O(N) in the worst case
We want a tree with small height
Height of a binary tree with N node is at least Θ(log
N)
Goal: keep the height of a binary search tree O(log N)
Balanced binary search trees
Examples: AVL tree, red-black tree
Balanced Tree?
Suggestion 1: the left and right subtrees of root have the
same height
Suggestion 2: every node must have left and right
subtrees of the same height
Our choice: for each node, the height of the left and
right subtrees can differ at most 1,-1,0.
AVL Tree
An AVL (Adelson-Velskii and Landis 1962) tree is a
binary search tree in which
for every node in the tree, the height of the left and
right subtrees differ by at most 1.
AVL property
violated here
AVL tree
N2 = 2 N3 =4 N4 = N2+N3+1=7N1 = 1
Height of AVL Tree
Denote Nh the minimum number of nodes in an AVL tree
of height h
N1=1, N2 =2 (base)
Nh= Nh-1 + Nh-2 +1 (recursive relation)
 many operations (i.e. searching) on an AVL tree will
take O(log N) time
Insertion in AVL Tree
Basically follows insertion strategy of binary search
tree
But may cause violation of AVL tree property
Restore the destroyed balance condition if needed
6
7
6 8
Original AVL tree
Insert 6
Property violated
Restore AVL property
Some Observations
After an insertion, only nodes that are on the path from
the insertion point to the root might have their balance
altered
Because only those nodes have their subtrees altered
Rebalance the tree at the deepest such node guarantees
that the entire tree satisfies the AVL property
7
6 8
Rebalance node 7
guarantees the whole tree be AVL
6
Node 5,8,7 might
have balance altered
Different Rebalancing
Rotations
The rebalancing rotations are classified as LL, LR, RR
and RL as illustrated below, based on the position of
the inserted node with reference to α.
LL rotation: Inserted node is in the left subtree of the left
subtree of node α
RR rotation: Inserted node is in the right subtree of the
right subtree of node α
LR rotation: Inserted node is in the right subtree of the
left subtree of node α
RL rotation: Inserted node is in the left subtree of the
right subtree of node α
Insertion Analysis
Insert the new key as a new leaf just as in
ordinary binary search tree: O(logN)
Then trace the path from the new leaf towards
the root, for each node x encountered: O(logN)
Check height difference: O(1)
If satisfies AVL property, proceed to next node: O(1)
If not, perform a rotation: O(1)
The insertion stops when
A single rotation is performed
Or, we’ve checked all nodes in the path
Time complexity for insertion O(logN)
logN
Delete 5, Node 10 is unbalanced Single Rotation
20
10 35
40155 25
18 453830
50
20
15 35
401810 25
453830
50
 Basically follows deletion strategy of binary search tree
 But may cause violation of AVL tree property
 Restore the destroyed balance condition if needed
For deletion, after rotation, we need to continue tracing
upward to see if AVL-tree property is violated at other node.
Different rotations are classified in R0, R1, R-1, L0, L1 And L-1
20
15 35
401810 25
453830
50
20
15
35
40
1810
25 4538
30 50
Continue to check parents
Oops!! Node 20 is
unbalanced!!
Single Rotation
Different rotations
• An element can be deleted from AVL tree which
may change the BF of a node such that it results
in unbalanced tree.
• Some rotations will be applied on AVL tree to
balance it.
• R rotation is applied if the deleted node is in the
right subtree of node A (A is the node with
balance factor other than 0, 1 and -1).
• L rotation is applied if the deleted node is in the
left subtree of node A.
Different rotations cont…
• Suppose we have deleted node X from the tree.
• A is the closest ancestor node on the path from X
to the root node, with a balance factor -2 or +2.
• B is the desendent of node A on the opposite
subtree of deleted node i.e. if the deleted node is
on left side the B is the desendent on the right
subtree of A or the root of right subtree of A.
R Rotation
• R Rotation is applied when the deleted node is in the right
subtree of node A.
• There are three different types of rotations based on the
balanced factor of node B.
• R0 Rotation: When the balance Factor of node B is 0.
• Apply LL Rotation on node A.
• R1 Rotation: When the balance Factor of node B is +1.
• Apply LL Rotation on node A.
• R-1 Rotation: When the balance Factor of node B is -1.
• Apply LR Rotation(RR rotation on B and LL rotation on
node A).
L Rotation
• L Rotation is applied when the deleted node is in the
left subtree of node A.
• There are three different types of rotations based on
the balanced factor of node B.
• L0 Rotation: When the balance Factor of node B is 0.
• Apply RR Rotation on node A.
• L-1 Rotation: When the balance Factor of node B is -1.
• Apply RR Rotation on node A.
• L1 Rotation: When the balance Factor of node B is +1.
• Apply RL Rotation(LL rotation on B and RR
rotation on node A).
Data Structure and Algorithms AVL Trees

More Related Content

PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPTX
trees in data structure
shameen khan
 
PPTX
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
PPTX
AVL Tree Data Structure
Afaq Mansoor Khan
 
PPT
1.5 binary search tree
Krish_ver2
 
PPTX
Binary Search Tree
Abhishek L.R
 
PDF
AVL tree ( Balanced Binary Search Tree)-Data Structure
Yaksh Jethva
 
Binary search tree(bst)
Hossain Md Shakhawat
 
trees in data structure
shameen khan
 
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
AVL Tree Data Structure
Afaq Mansoor Khan
 
1.5 binary search tree
Krish_ver2
 
Binary Search Tree
Abhishek L.R
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
Yaksh Jethva
 

What's hot (20)

PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PDF
Binary tree
Rajendran
 
PPTX
Red black tree in data structure
Vrushali Dhanokar
 
PPT
17. Trees and Graphs
Intro C# Book
 
PPTX
Trees data structure
Sumit Gupta
 
PPT
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
PPTX
Tree traversal techniques
Syed Zaid Irshad
 
PPT
1.1 binary tree
Krish_ver2
 
PPTX
Graph in data structure
Abrish06
 
PPTX
Trees (data structure)
Trupti Agrawal
 
PPTX
AVL Tree in Data Structure
Vrushali Dhanokar
 
PPTX
Bca ii dfs u-3 tree and graph
Rai University
 
PPTX
Avl trees final
PRAKASH RANJAN SINGH
 
PPT
AVL Tree.ppt
ChiragJoshi59934
 
PPT
Avl trees
ppreeta
 
PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PPTX
Data structure - Graph
Madhu Bala
 
PPTX
FIle Organization.pptx
Sreenivas R
 
PPT
Arrays
SARITHA REDDY
 
PPT
Spanning trees
Shareb Ismaeel
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Binary tree
Rajendran
 
Red black tree in data structure
Vrushali Dhanokar
 
17. Trees and Graphs
Intro C# Book
 
Trees data structure
Sumit Gupta
 
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Tree traversal techniques
Syed Zaid Irshad
 
1.1 binary tree
Krish_ver2
 
Graph in data structure
Abrish06
 
Trees (data structure)
Trupti Agrawal
 
AVL Tree in Data Structure
Vrushali Dhanokar
 
Bca ii dfs u-3 tree and graph
Rai University
 
Avl trees final
PRAKASH RANJAN SINGH
 
AVL Tree.ppt
ChiragJoshi59934
 
Avl trees
ppreeta
 
Binary Tree Traversal
Dhrumil Panchal
 
Data structure - Graph
Madhu Bala
 
FIle Organization.pptx
Sreenivas R
 
Spanning trees
Shareb Ismaeel
 
Ad

Similar to Data Structure and Algorithms AVL Trees (20)

PDF
AVL.pdf.................................
snamya20
 
PPTX
4. avl
Rajandeep Gill
 
PPT
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
PPT
3-avl-tree.ppt
meenamadhuvandhi2
 
PPT
Avl tree
Shankar Bishnoi
 
PPTX
Presentation_30219_Content_Document_20250107125144AM.pptx
chiraglab007
 
PPT
Avl trees 2
Shankar Bishnoi
 
PPTX
AVLDeletion in advanced data structures in jvava
shivanibasika06
 
PPTX
AVL tree PPT.pptx
SamyakJain710491
 
PPTX
Avl tree ppt
Surkhab Shelly
 
PPT
Avltrees
komalkoyal
 
PDF
What is AVL trees and example based on AVL Tree
Jaydeep Kale
 
PDF
Avl trees
Xad Kuain
 
PPTX
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
 
PPT
lecture18(1) for the data structure .ppt
umakalaimani1
 
PPTX
Adelson velskii Landis rotations based on
banupriyar5
 
PPTX
AVL Tree.pptx
Trad5
 
PDF
Lect 13, 14 (final)AVL Tree and Rotations.pdf
MuhammadUmerIhtisham
 
PDF
CS-102 AVLSv2.pdf
ssuser034ce1
 
AVL.pdf.................................
snamya20
 
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
3-avl-tree.ppt
meenamadhuvandhi2
 
Avl tree
Shankar Bishnoi
 
Presentation_30219_Content_Document_20250107125144AM.pptx
chiraglab007
 
Avl trees 2
Shankar Bishnoi
 
AVLDeletion in advanced data structures in jvava
shivanibasika06
 
AVL tree PPT.pptx
SamyakJain710491
 
Avl tree ppt
Surkhab Shelly
 
Avltrees
komalkoyal
 
What is AVL trees and example based on AVL Tree
Jaydeep Kale
 
Avl trees
Xad Kuain
 
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
 
lecture18(1) for the data structure .ppt
umakalaimani1
 
Adelson velskii Landis rotations based on
banupriyar5
 
AVL Tree.pptx
Trad5
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
MuhammadUmerIhtisham
 
CS-102 AVLSv2.pdf
ssuser034ce1
 
Ad

More from ManishPrajapati78 (14)

PPT
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
PPT
Data Structure and Algorithms Queues
ManishPrajapati78
 
PPTX
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
PPTX
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
PPT
Data Structure and Algorithms Stacks
ManishPrajapati78
 
PPT
Data Structure and Algorithms Linked List
ManishPrajapati78
 
PPT
Data Structure and Algorithms Sorting
ManishPrajapati78
 
PPT
Data Structure and Algorithms Arrays
ManishPrajapati78
 
PPT
Data Structure and Algorithms
ManishPrajapati78
 
PPT
Data Structure and Algorithms Hashing
ManishPrajapati78
 
PPTX
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
PPT
Data Structure and Algorithms Graphs
ManishPrajapati78
 
PPT
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
PPT
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Data Structure and Algorithms Queues
ManishPrajapati78
 
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
Data Structure and Algorithms Stacks
ManishPrajapati78
 
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Data Structure and Algorithms Sorting
ManishPrajapati78
 
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Data Structure and Algorithms
ManishPrajapati78
 
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
Data Structure and Algorithms Graphs
ManishPrajapati78
 
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 

Recently uploaded (20)

PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Presentation about variables and constant.pptx
kr2589474
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Presentation about variables and constant.pptx
safalsingh810
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
oapresentation.pptx
mehatdhavalrajubhai
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 

Data Structure and Algorithms AVL Trees

  • 2. Balance Binary Search Tree Worst case height of binary search tree: N Insertion, deletion can be O(N) in the worst case We want a tree with small height Height of a binary tree with N node is at least Θ(log N) Goal: keep the height of a binary search tree O(log N) Balanced binary search trees Examples: AVL tree, red-black tree
  • 3. Balanced Tree? Suggestion 1: the left and right subtrees of root have the same height Suggestion 2: every node must have left and right subtrees of the same height Our choice: for each node, the height of the left and right subtrees can differ at most 1,-1,0.
  • 4. AVL Tree An AVL (Adelson-Velskii and Landis 1962) tree is a binary search tree in which for every node in the tree, the height of the left and right subtrees differ by at most 1. AVL property violated here AVL tree
  • 5. N2 = 2 N3 =4 N4 = N2+N3+1=7N1 = 1
  • 6. Height of AVL Tree Denote Nh the minimum number of nodes in an AVL tree of height h N1=1, N2 =2 (base) Nh= Nh-1 + Nh-2 +1 (recursive relation)  many operations (i.e. searching) on an AVL tree will take O(log N) time
  • 7. Insertion in AVL Tree Basically follows insertion strategy of binary search tree But may cause violation of AVL tree property Restore the destroyed balance condition if needed 6 7 6 8 Original AVL tree Insert 6 Property violated Restore AVL property
  • 8. Some Observations After an insertion, only nodes that are on the path from the insertion point to the root might have their balance altered Because only those nodes have their subtrees altered Rebalance the tree at the deepest such node guarantees that the entire tree satisfies the AVL property 7 6 8 Rebalance node 7 guarantees the whole tree be AVL 6 Node 5,8,7 might have balance altered
  • 9. Different Rebalancing Rotations The rebalancing rotations are classified as LL, LR, RR and RL as illustrated below, based on the position of the inserted node with reference to α. LL rotation: Inserted node is in the left subtree of the left subtree of node α RR rotation: Inserted node is in the right subtree of the right subtree of node α LR rotation: Inserted node is in the right subtree of the left subtree of node α RL rotation: Inserted node is in the left subtree of the right subtree of node α
  • 10. Insertion Analysis Insert the new key as a new leaf just as in ordinary binary search tree: O(logN) Then trace the path from the new leaf towards the root, for each node x encountered: O(logN) Check height difference: O(1) If satisfies AVL property, proceed to next node: O(1) If not, perform a rotation: O(1) The insertion stops when A single rotation is performed Or, we’ve checked all nodes in the path Time complexity for insertion O(logN) logN
  • 11. Delete 5, Node 10 is unbalanced Single Rotation 20 10 35 40155 25 18 453830 50 20 15 35 401810 25 453830 50  Basically follows deletion strategy of binary search tree  But may cause violation of AVL tree property  Restore the destroyed balance condition if needed
  • 12. For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node. Different rotations are classified in R0, R1, R-1, L0, L1 And L-1 20 15 35 401810 25 453830 50 20 15 35 40 1810 25 4538 30 50 Continue to check parents Oops!! Node 20 is unbalanced!! Single Rotation
  • 13. Different rotations • An element can be deleted from AVL tree which may change the BF of a node such that it results in unbalanced tree. • Some rotations will be applied on AVL tree to balance it. • R rotation is applied if the deleted node is in the right subtree of node A (A is the node with balance factor other than 0, 1 and -1). • L rotation is applied if the deleted node is in the left subtree of node A.
  • 14. Different rotations cont… • Suppose we have deleted node X from the tree. • A is the closest ancestor node on the path from X to the root node, with a balance factor -2 or +2. • B is the desendent of node A on the opposite subtree of deleted node i.e. if the deleted node is on left side the B is the desendent on the right subtree of A or the root of right subtree of A.
  • 15. R Rotation • R Rotation is applied when the deleted node is in the right subtree of node A. • There are three different types of rotations based on the balanced factor of node B. • R0 Rotation: When the balance Factor of node B is 0. • Apply LL Rotation on node A. • R1 Rotation: When the balance Factor of node B is +1. • Apply LL Rotation on node A. • R-1 Rotation: When the balance Factor of node B is -1. • Apply LR Rotation(RR rotation on B and LL rotation on node A).
  • 16. L Rotation • L Rotation is applied when the deleted node is in the left subtree of node A. • There are three different types of rotations based on the balanced factor of node B. • L0 Rotation: When the balance Factor of node B is 0. • Apply RR Rotation on node A. • L-1 Rotation: When the balance Factor of node B is -1. • Apply RR Rotation on node A. • L1 Rotation: When the balance Factor of node B is +1. • Apply RL Rotation(LL rotation on B and RR rotation on node A).

Editor's Notes

  • #14: 목차 자바의 생성배경 자바를 사용하는 이유 과거, 현재, 미래의 자바 자바의 발전과정 버전별 JDK에 대한 설명 자바와 C++의 차이점 자바의 성능 자바 관련 산업(?)의 경향
  • #15: 목차 자바의 생성배경 자바를 사용하는 이유 과거, 현재, 미래의 자바 자바의 발전과정 버전별 JDK에 대한 설명 자바와 C++의 차이점 자바의 성능 자바 관련 산업(?)의 경향
  • #16: 목차 자바의 생성배경 자바를 사용하는 이유 과거, 현재, 미래의 자바 자바의 발전과정 버전별 JDK에 대한 설명 자바와 C++의 차이점 자바의 성능 자바 관련 산업(?)의 경향
  • #17: 목차 자바의 생성배경 자바를 사용하는 이유 과거, 현재, 미래의 자바 자바의 발전과정 버전별 JDK에 대한 설명 자바와 C++의 차이점 자바의 성능 자바 관련 산업(?)의 경향