SlideShare a Scribd company logo
AVL Trees
Binary Search Tree - Best Time
• All BST operations are O(h), where h is tree
height
• minimum h is O(log n) for a binary tree with
N nodes
– What is the best case tree? O(log n)
– What is the worst case tree? O(n)
• So, best case running time of BST operations
is O(log N)
2
Binary Search Tree - Worst Time
• Worst case running time is O(N)
– What happens when you Insert elements in
ascending order?
• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
– Problem: Lack of “balance”:
• compare depths of left and right subtree
– Unbalanced degenerate tree
3
Balanced and unbalanced BST
4
4
2 5
1 3
1
5
2
4
3
7
6
4
2 6
5 71 3
Is this “balanced”?
Approaches to balancing trees
• Don't balance
– May end up with some nodes very deep
• Strict balance
– The tree must always be balanced perfectly
• Pretty good balance
– Only allow a little out of balance
5
Balancing Binary Search Trees
• Many algorithms exist for keeping binary
search trees balanced
– Adelson-Velskii and Landis (AVL) trees (height-
balanced trees)
– Splay trees and other self-adjusting trees
– B-trees and other multiway search trees
6
Perfect Balance
• Want a complete tree after every operation
– tree is full except possibly in the lower right
• This is expensive
– For example, insert 2 in the tree on the left and
then rebuild as a complete tree
7
Insert 2 &
complete tree
6
4 9
81 5
5
2 8
6 91 4
AVL - Good but not Perfect Balance
• AVL trees are height-balanced binary search
trees
• Balance factor of a node
– height(left subtree) - height(right subtree)
• An AVL tree has balance factor calculated at
every node
– For every node, heights of left and right subtree
can differ by no more than 1
– Store current balance in each node
8
Implementation
9
balance (1,0,-1)
key
rightleft
No need to keep the height; just the difference in height,
i.e. the balance factor; this has to be modified on the path of
insertion even if you don’t perform rotations
Once you have performed a rotation (single or double) you won’t
need to go back up the tree
5
3 8
1 4 10
5
3
1 4
AVL Tree
Not AVL Tree
Node Heights
11
1
00
2
0
6
4 9
81 5
1
height of node = h
balance factor = hleft-hright
empty height = -1
0
0
height=2 BF=1-0=1
0
6
4 9
1 5
1
Tree A (AVL) Tree B (AVL)
Node Heights after Insert 7
12
2
10
3
0
6
4 9
81 5
1
height of node = h
balance factor = hleft-hright
empty height = -1
1
0
2
0
6
4 9
1 5
1
0
7
0
7
balance factor
1-(-1) = 2
-1
Tree A (AVL) Tree B (not AVL)
Insert and Rotation in AVL Trees
• Insert operation may cause balance factor to
become 2 or –2 for some node
– only nodes on the path from insertion point to
root node have possibly changed in height
– So after the Insert, go back up to the root node by
node, updating heights
– If a new balance factor (the difference hleft-hright) is
2 or –2, adjust tree by rotation around the node
13
Inserting a Node in an AVL Tree
• During insertion, the new node is inserted as the leaf node, so it will
always have balance factor equal to zero.
• The nodes whose balance factors will change are those which lie on
the path between the root of the tree and the newly inserted node.
• The possible changes which may take place in any node on the path
are as follows:
 Initially the node was either left or right heavy and after insertion has
become balanced.
 Initially the node was balanced and after insertion has become either
left or right heavy.
 Initially the node was heavy (either left or right) and the new node has
been inserted in the heavy sub-tree thereby creating an unbalanced
sub-tree. Such a node is said to be a critical node.
Single Rotation in an AVL Tree
15
2
10
2
0
6
4 9
81 5
1
0
7
0
1
0
2
0
6
4
9
8
1 5
1
0
7
5
3
1 4
Insert 0.8
AVL Tree
8
0.8
5
3
1 4
8 Z
3
5
1
0.8
4 8
After Rotation
17
Let the node that needs rebalancing be .
There are 4 cases:
Outside Cases (require single rotation) :
1. Insertion into left subtree of left child of .
2. Insertion into right subtree of right child of .
Inside Cases (require double rotation) :
3. Insertion into right subtree of left child of .
4. Insertion into left subtree of right child of .
The rebalancing is performed through four
separate rotation algorithms.
Insertions in AVL Trees
Rotations to Balance AVL Trees
• To perform rotation, our first work is to find the critical node. Critical node is
the nearest ancestor node on the path from the root to the inserted node
whose balance factor is neither -1, 0 nor 1.
• The second task is to determine which type of rotation has to be done.
• There are four types of rebalancing rotations and their application depends on
the position of the inserted node with reference to the critical node.
 LL rotation: the new node is inserted in the left sub-tree of the left sub-tree of
the critical node
 RR rotation: the new node is inserted in the right sub-tree of the right sub-tree
of the critical node
 LR rotation: the new node is inserted in the right sub-tree of the left sub-tree
of the critical node
 RL rotation: the new node is inserted in the left sub-tree of the right sub-tree
of the critical node
19
j
k
X Y
Z
Consider a valid
AVL subtree
AVL Insertion: Outside Case
h
h
h
20
j
k
X
Y
Z
Inserting into X
destroys the AVL
property at node j
AVL Insertion: Outside Case
h
h+1 h
21
j
k
X
Y
Z
Do a “right rotation”
AVL Insertion: Outside Case
h
h+1 h
22
j
k
X
Y
Z
Do a “right rotation”
Single right rotation
h
h+1 h
23
j
k
X Y Z
“Right rotation” done!
(“Left rotation” is mirror
symmetric)
Outside Case Completed
AVL property has been restored!
h
h+1
h
24
j
k
X Y
Z
AVL Insertion: Inside Case
Consider a valid
AVL subtree
h
hh
25
Inserting into Y
destroys the
AVL property
at node j
j
k
X
Y
Z
AVL Insertion: Inside Case
Does “right rotation”
restore balance?
h
h+1h
26
j
k
X
Y
Z
“Right rotation”
does not restore
balance… now k is
out of balance
AVL Insertion: Inside Case
h
h+1
h
27
Consider the structure
of subtree Y… j
k
X
Y
Z
AVL Insertion: Inside Case
h
h+1h
28
j
k
X
V
Z
W
i
Y = node i and
subtrees V and W
AVL Insertion: Inside Case
h
h+1h
h or h-1
29
j
k
X
V
Z
W
i
AVL Insertion: Inside Case
We will do a left-right
“double rotation” . . .
30
j
k
X V
Z
W
i
Double rotation : first rotation
left rotation complete
31
j
k
X V
Z
W
i
Double rotation : second
rotation
Now do a right rotation
32
jk
X V ZW
i
Double rotation : second
rotation
right rotation complete
Balance has been
restored
hh h or h-1
5
3
1 4
Insert 3.5
AVL Tree
8
3.5
5
3
1 4
8
4
5
0.8
3
3.5
After Rotation
8
Insertion in AVL Trees
• Insert at the leaf (as for all BST)
– only nodes on the path from insertion point to
root node have possibly changed in height
– So after the Insert, go back up to the root node by
node, updating heights
– If a new balance factor (the difference hleft-hright) is
2 or –2, adjust tree by rotation around the node
34
Example of Insertions in an AVL Tree
35
1
0
2
20
10 30
25
0
35
0
Insert 5, 40
Example of Insertions in an AVL Tree
36
1
0
2
20
10 30
25
1
35
0
5
0
20
10 30
25
1
355
40
0
0
0
1
2
3
Now Insert 45
Single rotation (outside case)
37
2
0
3
20
10 30
25
1
35
2
5
0
20
10 30
25
1
405
40
0
0
0
1
2
3
45
Imbalance
35 45
0 0
1
Now Insert 34
Double rotation (inside case)
38
3
0
3
20
10 30
25
1
40
2
5
0
20
10 35
30
1
405
45
0 1
2
3
Imbalance
45
0
1
Insertion of 34
35
34
0
0
1 25 340
Extended Example
Insert 3,2,1,4,5,6,7, 16,15,14
3
Fig 1
3
2
Fig 2
3
2
1
Fig 3
2
1 3
Fig 4
2
1 3
4
Fig 5
2
1 3
4
5
Fig 6
2
1 4
53
Fig 7 6
2
1 4
53
Fig 8
4
2 5
61 3
Fig 9
4
2 5
61 3
7Fig 10
4
2 6
71 3
5 Fig 11
4
2 6
71 3
5 16
Fig 12
4
2 6
71 3
5 16
15
Fig 13
4
2 6
151 3 5
16
7Fig 14
5
4
2 7
151 3 6
1614
Fig 16
4
2 6
151 3 5
16
7
14
Fig 15
Deletions can be done with similar rotations
AVL Tree Deletion
• Similar but more complex than insertion
– Rotations and double rotations needed to
rebalance
– Imbalance may propagate upward so that many
rotations may be needed.
43
44
Arguments for AVL trees:
1. Search is O(log N) since AVL trees are always balanced.
2. Insertion and deletions are also O(logn)
3. The height balancing adds no more than a constant factor to the
speed of insertion.
Arguments against using AVL trees:
1. Difficult to program & debug; more space for balance factor.
2. Asymptotically faster but rebalancing costs time.
3. Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
4. May be OK to have O(N) for a single operation if total run time for
many consecutive operations is fast (e.g. Splay trees).
Pros and Cons of AVL Trees
Deleting a Node from an AVL Tree
• Deletion of a node in an AVL tree is similar to that of binary search trees.
• But deletion may disturb the AVLness of the tree, so to re-balance the
AVL tree we need to perform rotations.
• There are two classes of rotation that can be performed on an AVL tree
after deleting a given node: R rotation and L rotation.
• If the node to be deleted is present in the left sub-tree of the critical
node, then L rotation is applied else if node is in the right sub-tree, R
rotation is performed.
• Further there are three categories of L and R rotations. The variations of
L rotation are: L-1, L0 and L1 rotation. Correspondingly for R rotation,
there are R0, R-1 and R1 rotations.
Deleting a Node from an AVL Tree
• R0 Rotation
 Let B be the root of the left or right sub-tree of A (critical node).
 R0 rotation is applied if the balance factor of B is 0.
 Consider the AVL tree given below and delete 72 from it.
4
5
6
3
3
6
2
7 3
9
7
2
-1
1
1
-1
0
1
8
0
4
0
0
0
45
6336
27 39
0
2
1
-1
0
18 40
0 0
36
4527
18
39
1
-1
0
-1
1
40
63
0
0
Deleting a Node from an AVL Tree
• R1 Rotation
 Let B be the root of the left or right sub-tree of the critical node.
 R1 rotation is applied if the balance factor of B is 1.
 Consider the AVL tree given below and delete 72 from it.
4
5
6
3
3
6
2
7 3
9
7
2
1
1
1
0
1
1
8
0
0
45
6336
27 39
0
2
1
0
1
18
0
3
6
4
5
2
7
1
8
3
9
0
0
0 0
1
6
3
0
Deleting a Node from an AVL Tree
• R-1Rotation
 Let B be the root of the left or right sub-tree of the critical node.
 R-1 rotation is applied if the balance factor of B is -1.
 Consider the AVL tree given below and delete 72 from it.
4
5
6
3
3
6
2
7 3
9
-1
1
0 0
-1
3
7
4
1
0 0
7
2
0
4
5
6
3
3
6
2
7 3
9
0
2
0 0
-1
3
7
4
1
0 0
3
9
4
5
3
6
2
7
3
7
4
1
1
1
1 0
1
0
6
3
0

More Related Content

What's hot

Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
Quick sort
Quick sortQuick sort
Quick sort
Afaq Mansoor Khan
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
Binary tree
Binary tree Binary tree
Binary tree
Rajendran
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Abrish06
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
Vrushali Dhanokar
 
Red black tree
Red black treeRed black tree
Red black tree
uos lahore
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
smruti sarangi
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
Rajandeep Gill
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Red black tree
Red black treeRed black tree
Red black tree
Dr Sandeep Kumar Poonia
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
hamza javed
 

What's hot (20)

Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Avl trees
Avl treesAvl trees
Avl trees
 
Quick sort
Quick sortQuick sort
Quick sort
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Binary tree
Binary tree Binary tree
Binary tree
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Red black tree
Red black treeRed black tree
Red black tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 

Similar to Avl trees final

Avl tree
Avl treeAvl tree
Avl tree
Van Pham
 
Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
meenamadhuvandhi2
 
AVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to doAVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to do
lokaprasaadvs
 
4. avl
4. avl4. avl
DS_Mod4_2.pdf
DS_Mod4_2.pdfDS_Mod4_2.pdf
DS_Mod4_2.pdf
SankarTerli
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
Surkhab Shelly
 
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.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
Trad5
 
avl.ppt
avl.pptavl.ppt
avl.ppt
cotisa2402
 
avl.ppt
avl.pptavl.ppt
avl.ppt
plagcheck
 
AVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinyaAVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinya
bagusciptapratama
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
Pran K Mohanty
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
Dr Sandeep Kumar Poonia
 
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
22001003058
 
Avltrees 121106175039-phpapp01
Avltrees 121106175039-phpapp01Avltrees 121106175039-phpapp01
Avltrees 121106175039-phpapp01queenmarry
 
Avl trees
Avl treesAvl trees
Avl treesppreeta
 
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMSAVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AnyaForger34
 

Similar to Avl trees final (20)

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
 
Avl tree
Avl treeAvl tree
Avl tree
 
Avl trees
Avl treesAvl trees
Avl trees
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
 
AVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to doAVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to do
 
4. avl
4. avl4. avl
4. avl
 
DS_Mod4_2.pdf
DS_Mod4_2.pdfDS_Mod4_2.pdf
DS_Mod4_2.pdf
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
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.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
AVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinyaAVL TREE java dalam penerapannya dan aplikasinya
AVL TREE java dalam penerapannya dan aplikasinya
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
Design data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sirDesign data Analysis Avl Trees.pptx by piyush sir
Design data Analysis Avl Trees.pptx by piyush sir
 
Avltrees 121106175039-phpapp01
Avltrees 121106175039-phpapp01Avltrees 121106175039-phpapp01
Avltrees 121106175039-phpapp01
 
Avl trees
Avl treesAvl trees
Avl trees
 
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMSAVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
 

Recently uploaded

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 

Recently uploaded (20)

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 

Avl trees final

  • 2. Binary Search Tree - Best Time • All BST operations are O(h), where h is tree height • minimum h is O(log n) for a binary tree with N nodes – What is the best case tree? O(log n) – What is the worst case tree? O(n) • So, best case running time of BST operations is O(log N) 2
  • 3. Binary Search Tree - Worst Time • Worst case running time is O(N) – What happens when you Insert elements in ascending order? • Insert: 2, 4, 6, 8, 10, 12 into an empty BST – Problem: Lack of “balance”: • compare depths of left and right subtree – Unbalanced degenerate tree 3
  • 4. Balanced and unbalanced BST 4 4 2 5 1 3 1 5 2 4 3 7 6 4 2 6 5 71 3 Is this “balanced”?
  • 5. Approaches to balancing trees • Don't balance – May end up with some nodes very deep • Strict balance – The tree must always be balanced perfectly • Pretty good balance – Only allow a little out of balance 5
  • 6. Balancing Binary Search Trees • Many algorithms exist for keeping binary search trees balanced – Adelson-Velskii and Landis (AVL) trees (height- balanced trees) – Splay trees and other self-adjusting trees – B-trees and other multiway search trees 6
  • 7. Perfect Balance • Want a complete tree after every operation – tree is full except possibly in the lower right • This is expensive – For example, insert 2 in the tree on the left and then rebuild as a complete tree 7 Insert 2 & complete tree 6 4 9 81 5 5 2 8 6 91 4
  • 8. AVL - Good but not Perfect Balance • AVL trees are height-balanced binary search trees • Balance factor of a node – height(left subtree) - height(right subtree) • An AVL tree has balance factor calculated at every node – For every node, heights of left and right subtree can differ by no more than 1 – Store current balance in each node 8
  • 9. Implementation 9 balance (1,0,-1) key rightleft No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree
  • 10. 5 3 8 1 4 10 5 3 1 4 AVL Tree Not AVL Tree
  • 11. Node Heights 11 1 00 2 0 6 4 9 81 5 1 height of node = h balance factor = hleft-hright empty height = -1 0 0 height=2 BF=1-0=1 0 6 4 9 1 5 1 Tree A (AVL) Tree B (AVL)
  • 12. Node Heights after Insert 7 12 2 10 3 0 6 4 9 81 5 1 height of node = h balance factor = hleft-hright empty height = -1 1 0 2 0 6 4 9 1 5 1 0 7 0 7 balance factor 1-(-1) = 2 -1 Tree A (AVL) Tree B (not AVL)
  • 13. Insert and Rotation in AVL Trees • Insert operation may cause balance factor to become 2 or –2 for some node – only nodes on the path from insertion point to root node have possibly changed in height – So after the Insert, go back up to the root node by node, updating heights – If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node 13
  • 14. Inserting a Node in an AVL Tree • During insertion, the new node is inserted as the leaf node, so it will always have balance factor equal to zero. • The nodes whose balance factors will change are those which lie on the path between the root of the tree and the newly inserted node. • The possible changes which may take place in any node on the path are as follows:  Initially the node was either left or right heavy and after insertion has become balanced.  Initially the node was balanced and after insertion has become either left or right heavy.  Initially the node was heavy (either left or right) and the new node has been inserted in the heavy sub-tree thereby creating an unbalanced sub-tree. Such a node is said to be a critical node.
  • 15. Single Rotation in an AVL Tree 15 2 10 2 0 6 4 9 81 5 1 0 7 0 1 0 2 0 6 4 9 8 1 5 1 0 7
  • 16. 5 3 1 4 Insert 0.8 AVL Tree 8 0.8 5 3 1 4 8 Z 3 5 1 0.8 4 8 After Rotation
  • 17. 17 Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms. Insertions in AVL Trees
  • 18. Rotations to Balance AVL Trees • To perform rotation, our first work is to find the critical node. Critical node is the nearest ancestor node on the path from the root to the inserted node whose balance factor is neither -1, 0 nor 1. • The second task is to determine which type of rotation has to be done. • There are four types of rebalancing rotations and their application depends on the position of the inserted node with reference to the critical node.  LL rotation: the new node is inserted in the left sub-tree of the left sub-tree of the critical node  RR rotation: the new node is inserted in the right sub-tree of the right sub-tree of the critical node  LR rotation: the new node is inserted in the right sub-tree of the left sub-tree of the critical node  RL rotation: the new node is inserted in the left sub-tree of the right sub-tree of the critical node
  • 19. 19 j k X Y Z Consider a valid AVL subtree AVL Insertion: Outside Case h h h
  • 20. 20 j k X Y Z Inserting into X destroys the AVL property at node j AVL Insertion: Outside Case h h+1 h
  • 21. 21 j k X Y Z Do a “right rotation” AVL Insertion: Outside Case h h+1 h
  • 22. 22 j k X Y Z Do a “right rotation” Single right rotation h h+1 h
  • 23. 23 j k X Y Z “Right rotation” done! (“Left rotation” is mirror symmetric) Outside Case Completed AVL property has been restored! h h+1 h
  • 24. 24 j k X Y Z AVL Insertion: Inside Case Consider a valid AVL subtree h hh
  • 25. 25 Inserting into Y destroys the AVL property at node j j k X Y Z AVL Insertion: Inside Case Does “right rotation” restore balance? h h+1h
  • 26. 26 j k X Y Z “Right rotation” does not restore balance… now k is out of balance AVL Insertion: Inside Case h h+1 h
  • 27. 27 Consider the structure of subtree Y… j k X Y Z AVL Insertion: Inside Case h h+1h
  • 28. 28 j k X V Z W i Y = node i and subtrees V and W AVL Insertion: Inside Case h h+1h h or h-1
  • 29. 29 j k X V Z W i AVL Insertion: Inside Case We will do a left-right “double rotation” . . .
  • 30. 30 j k X V Z W i Double rotation : first rotation left rotation complete
  • 31. 31 j k X V Z W i Double rotation : second rotation Now do a right rotation
  • 32. 32 jk X V ZW i Double rotation : second rotation right rotation complete Balance has been restored hh h or h-1
  • 33. 5 3 1 4 Insert 3.5 AVL Tree 8 3.5 5 3 1 4 8 4 5 0.8 3 3.5 After Rotation 8
  • 34. Insertion in AVL Trees • Insert at the leaf (as for all BST) – only nodes on the path from insertion point to root node have possibly changed in height – So after the Insert, go back up to the root node by node, updating heights – If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node 34
  • 35. Example of Insertions in an AVL Tree 35 1 0 2 20 10 30 25 0 35 0 Insert 5, 40
  • 36. Example of Insertions in an AVL Tree 36 1 0 2 20 10 30 25 1 35 0 5 0 20 10 30 25 1 355 40 0 0 0 1 2 3 Now Insert 45
  • 37. Single rotation (outside case) 37 2 0 3 20 10 30 25 1 35 2 5 0 20 10 30 25 1 405 40 0 0 0 1 2 3 45 Imbalance 35 45 0 0 1 Now Insert 34
  • 38. Double rotation (inside case) 38 3 0 3 20 10 30 25 1 40 2 5 0 20 10 35 30 1 405 45 0 1 2 3 Imbalance 45 0 1 Insertion of 34 35 34 0 0 1 25 340
  • 39. Extended Example Insert 3,2,1,4,5,6,7, 16,15,14 3 Fig 1 3 2 Fig 2 3 2 1 Fig 3 2 1 3 Fig 4 2 1 3 4 Fig 5 2 1 3 4 5 Fig 6
  • 40. 2 1 4 53 Fig 7 6 2 1 4 53 Fig 8 4 2 5 61 3 Fig 9 4 2 5 61 3 7Fig 10 4 2 6 71 3 5 Fig 11
  • 41. 4 2 6 71 3 5 16 Fig 12 4 2 6 71 3 5 16 15 Fig 13 4 2 6 151 3 5 16 7Fig 14
  • 42. 5 4 2 7 151 3 6 1614 Fig 16 4 2 6 151 3 5 16 7 14 Fig 15 Deletions can be done with similar rotations
  • 43. AVL Tree Deletion • Similar but more complex than insertion – Rotations and double rotations needed to rebalance – Imbalance may propagate upward so that many rotations may be needed. 43
  • 44. 44 Arguments for AVL trees: 1. Search is O(log N) since AVL trees are always balanced. 2. Insertion and deletions are also O(logn) 3. The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: 1. Difficult to program & debug; more space for balance factor. 2. Asymptotically faster but rebalancing costs time. 3. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 4. May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees). Pros and Cons of AVL Trees
  • 45. Deleting a Node from an AVL Tree • Deletion of a node in an AVL tree is similar to that of binary search trees. • But deletion may disturb the AVLness of the tree, so to re-balance the AVL tree we need to perform rotations. • There are two classes of rotation that can be performed on an AVL tree after deleting a given node: R rotation and L rotation. • If the node to be deleted is present in the left sub-tree of the critical node, then L rotation is applied else if node is in the right sub-tree, R rotation is performed. • Further there are three categories of L and R rotations. The variations of L rotation are: L-1, L0 and L1 rotation. Correspondingly for R rotation, there are R0, R-1 and R1 rotations.
  • 46. Deleting a Node from an AVL Tree • R0 Rotation  Let B be the root of the left or right sub-tree of A (critical node).  R0 rotation is applied if the balance factor of B is 0.  Consider the AVL tree given below and delete 72 from it. 4 5 6 3 3 6 2 7 3 9 7 2 -1 1 1 -1 0 1 8 0 4 0 0 0 45 6336 27 39 0 2 1 -1 0 18 40 0 0 36 4527 18 39 1 -1 0 -1 1 40 63 0 0
  • 47. Deleting a Node from an AVL Tree • R1 Rotation  Let B be the root of the left or right sub-tree of the critical node.  R1 rotation is applied if the balance factor of B is 1.  Consider the AVL tree given below and delete 72 from it. 4 5 6 3 3 6 2 7 3 9 7 2 1 1 1 0 1 1 8 0 0 45 6336 27 39 0 2 1 0 1 18 0 3 6 4 5 2 7 1 8 3 9 0 0 0 0 1 6 3 0
  • 48. Deleting a Node from an AVL Tree • R-1Rotation  Let B be the root of the left or right sub-tree of the critical node.  R-1 rotation is applied if the balance factor of B is -1.  Consider the AVL tree given below and delete 72 from it. 4 5 6 3 3 6 2 7 3 9 -1 1 0 0 -1 3 7 4 1 0 0 7 2 0 4 5 6 3 3 6 2 7 3 9 0 2 0 0 -1 3 7 4 1 0 0 3 9 4 5 3 6 2 7 3 7 4 1 1 1 1 0 1 0 6 3 0