SlideShare a Scribd company logo
Data Structures & Algorithm
CS-102
Lecture 13,14
AVL TREES
Lecturer: Syeda Nazia Ashraf 1
Problem with BST
• The disadvantage of skewed BST is that the
worst case time complexity of a search is O(n)
Insertion in Binary Search Tree
BST for 3 4 5 7 9 14 15 16 17 18 20
14
15
4
9
7
18
3
5
16
20
17
Linked List!
3
• Data not Symmetric i.e Right Skewed
• Less nodes, but Height is more
• Go for Linear Search and its efficiency lost
• Time complexity for linear search = O(n),
which is less efficient and take more time
to search
Insertion in Binary Search Tree
BST for 14, 15, 4, 9,18, 3
14
15
4
9 18
3
4
Level 0, 20= 1 node
Level 1, 21= 2 nodes
Level 2, 22= 4 nodes
Height=3, i.e max 3 comparisons for searching
Search 9 ?
we move on one side
of the Tree according
to BST comparison
order
• For 10 levels, 29=512 nodes (at last level 9)
• Sum of all nodes=1023 nodes, i.e need max 10 comparisons (Advantage of BST)
• Time complexity for Binary Search= O(log2 n), which is more efficient than Linear Search because it takes
less time
• To make sure when value inserted in BST its height should be balanced, we use AVL TREE
Balanced BST
• We should keep the tree balanced.
• One idea would be to have the left and right
subtrees have the same height
5
Balanced BST
Does not force the tree to be shallow.
14
15
18
16
20
17
4
9
7
3
5
6
Balanced BST
• We could insist that every node must have left and
right subtrees of same height.
• But this requires that the tree be a complete binary
tree
• To do this, there must have (2d+1 – 1) data items,
where d is the depth of the tree.
• This is too rigid a condition.
7
AVL
• There is a need to maintain the binary search tree to
be of balanced height, so that it is possible to
obtained for the search option a time complexity of
O(log n) in the worst case
• One of the most popular balanced tree was
introduced by Adelson-Velskii and Landis (AVL)
AVL Tree
• AVL (Adelson-Velskii and Landis) tree (are 2 Russian scientists,
proposed technique in their research paper published in 1962 to
balance binary tree and to save BST from degenerated form )
• An AVL tree is identical to a BST except
▪ height of the left and right subtrees can differ by at
most 1.
▪ height of an empty tree is defined to be (–1).
9
AVL Tree
• An empty binary tree is an AVL Tree.
• A nonempty binary tree T is an AVL Tree iff given
TL and TR to be the left and right subtrees of T
and h(TL) and h(TR) to be the heights of subtrees
TL and TR respectively, TL and TR are AVL Trees
and | h(TL) - h(TR) | ≤ 1
Balanced Binary Tree
• The height of a binary tree is the maximum level of its
leaves (also called the depth).
• The balance of a node in a binary tree is defined as
the height of its left subtree minus height of its right
subtree.
• In balanced tree, each node has an indicated balance
of 1, 0, or –1.
11
Balance Factor
• h(TL) - h(TR) is called Balance Factor (BF)
• For AVL Tree, the balance factor of a node can
be either 0, 1, -1
AVL Tree
50
T L
T R
h=2
h=3
BF = h(TL)-h(TR)
BF = 3-2
BF = 1
Balanced Binary Tree
-1
0
1
0
0
0
0
-1
0
1
0
0 0
0 0
0 0
This tree contains balance factors instead of data
14
Balanced Binary Tree
Insertions and effect on balance
-1
0
1
0
0
0
0
-1
0
1
0
0 0
0 0
0 0
U1 U2 U3 U4
U5 U6 U7 U8 U9 U10 U11 U12
B B
B B
B B
15
Balanced Binary Tree
• Tree becomes unbalanced only if the newly inserted
node
▪ is a left descendant (grand child) of a node that
previously had a balance of 1 (U1 to U8),
▪ or is a right descendant (grand child) of a node
that previously had a balance of –1 (U9 to U12)
16
Balanced Binary Tree
Insertions and effect on balance
-1
0
1
0
0
0
0
-1
0
1
0
0 0
0 0
0 0
U1 U2 U3 U4
U5 U6 U7 U8 U9 U10 U11 U12
B B
B B
B B
17
Inserting New Node in AVL Tree
1
0
A
B
T1
T3
T2
1
Use triangle notation for rest of the nodes
18
Inserting New Node in AVL Tree
2
1
A
B
T1
T3
T2
new
1
2
Violate AVL condition
19
Searching in AVL Tree
• Searching an AVL search tree for an element is
exactly similar to the method used in a binary
search tree.
An AVL Tree
5
8
2
4
3
1 7
Height = 4
0
1
2
3
21
Not an AVL tree
6
8
1
4
1
5
Height = 4
0
1
2
3
22
• After insertion of node 5, BF of 6 node disturb
• Before next insertion, we have to balance BST
• To balance BST, we use Rotations
• Rotations do shuffling of nodes and make BST balance and transform BFs to -1, 0 and 1
Rotations
•To perform the rotations it is necessary to identify a
specific node A whose balance factor (BF) is neither 0,
1, -1 and which is the nearest ancestor to the inserted
node on the path from the inserted node to the root.
Rotation Types
Note: The insertion occurs on the “outside” (i.e., left-left or right-right rotations)
Insertion occurs on the “inside” i.e., right-left or left-right rotations)
• LL rotation: Inserted node is in the left subtree of left
subtree of node A.
• RR rotation: Inserted node is in the right subtree of right
subtree of node A.
• LR rotation: Inserted node is in the right subtree of left
subtree of node A.
• RL rotation: Inserted node is in the left subtree of right
subtree of node A.
Trick to solve
Note: We also say, LL and RR rotations can fix through Single Rotation.
LR and RL Rotation can fix through Double Rotation
• For LL and RR rotations, identify A and B, then
make A as a child of B.
• FOR LR and RL rotations, identify A, B and C, then
make A and B child of C.
Example of Rotation
• Generate AVL Tree for values:
5, 7, 19, 12, 10, 15, 18, 20, 25, 23
• Solve Example on board….

More Related Content

What's hot

Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
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
Yaksh Jethva
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
Trees
TreesTrees
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
Andres Mendez-Vazquez
 
Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
Pravin Dsilva
 
Avl tree
Avl treeAvl tree
Avl tree
Van Pham
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
Sadaf Ismail
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
Editor IJCTER
 
Tree
TreeTree

What's hot (14)

Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Avltrees
AvltreesAvltrees
Avltrees
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 
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
 
Trees
TreesTrees
Trees
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Trees
TreesTrees
Trees
 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
 
Red black 1
Red black 1Red black 1
Red black 1
 
Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
 
Avl tree
Avl treeAvl tree
Avl tree
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
Tree
TreeTree
Tree
 

Similar to Lect 13, 14 (final)AVL Tree and Rotations.pdf

4. avl
4. avl4. avl
Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
 
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
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
SanghdipUdrake
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
PRAKASH RANJAN SINGH
 
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
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
Surkhab Shelly
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
Dr Sandeep Kumar Poonia
 
DS_Mod4_2.pdf
DS_Mod4_2.pdfDS_Mod4_2.pdf
DS_Mod4_2.pdf
SankarTerli
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
SamyakJain710491
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
Trad5
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
omkarbhabal
 
avl.ppt
avl.pptavl.ppt
avl.ppt
cotisa2402
 
avl.ppt
avl.pptavl.ppt
avl.ppt
plagcheck
 
Tree
TreeTree
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 

Similar to Lect 13, 14 (final)AVL Tree and Rotations.pdf (20)

4. avl
4. avl4. avl
4. avl
 
Avl trees
Avl treesAvl trees
Avl trees
 
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
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
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
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
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
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
 
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.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
Tree
TreeTree
Tree
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Lec27
Lec27Lec27
Lec27
 

More from MuhammadUmerIhtisham

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
MuhammadUmerIhtisham
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
MuhammadUmerIhtisham
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
MuhammadUmerIhtisham
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
MuhammadUmerIhtisham
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
MuhammadUmerIhtisham
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdf
MuhammadUmerIhtisham
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdf
MuhammadUmerIhtisham
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdf
MuhammadUmerIhtisham
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
MuhammadUmerIhtisham
 

More from MuhammadUmerIhtisham (14)

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdf
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdf
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdf
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
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
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
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 approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
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
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
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
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
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
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
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 approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
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...
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
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
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 

Lect 13, 14 (final)AVL Tree and Rotations.pdf

  • 1. Data Structures & Algorithm CS-102 Lecture 13,14 AVL TREES Lecturer: Syeda Nazia Ashraf 1
  • 2. Problem with BST • The disadvantage of skewed BST is that the worst case time complexity of a search is O(n)
  • 3. Insertion in Binary Search Tree BST for 3 4 5 7 9 14 15 16 17 18 20 14 15 4 9 7 18 3 5 16 20 17 Linked List! 3 • Data not Symmetric i.e Right Skewed • Less nodes, but Height is more • Go for Linear Search and its efficiency lost • Time complexity for linear search = O(n), which is less efficient and take more time to search
  • 4. Insertion in Binary Search Tree BST for 14, 15, 4, 9,18, 3 14 15 4 9 18 3 4 Level 0, 20= 1 node Level 1, 21= 2 nodes Level 2, 22= 4 nodes Height=3, i.e max 3 comparisons for searching Search 9 ? we move on one side of the Tree according to BST comparison order • For 10 levels, 29=512 nodes (at last level 9) • Sum of all nodes=1023 nodes, i.e need max 10 comparisons (Advantage of BST) • Time complexity for Binary Search= O(log2 n), which is more efficient than Linear Search because it takes less time • To make sure when value inserted in BST its height should be balanced, we use AVL TREE
  • 5. Balanced BST • We should keep the tree balanced. • One idea would be to have the left and right subtrees have the same height 5
  • 6. Balanced BST Does not force the tree to be shallow. 14 15 18 16 20 17 4 9 7 3 5 6
  • 7. Balanced BST • We could insist that every node must have left and right subtrees of same height. • But this requires that the tree be a complete binary tree • To do this, there must have (2d+1 – 1) data items, where d is the depth of the tree. • This is too rigid a condition. 7
  • 8. AVL • There is a need to maintain the binary search tree to be of balanced height, so that it is possible to obtained for the search option a time complexity of O(log n) in the worst case • One of the most popular balanced tree was introduced by Adelson-Velskii and Landis (AVL)
  • 9. AVL Tree • AVL (Adelson-Velskii and Landis) tree (are 2 Russian scientists, proposed technique in their research paper published in 1962 to balance binary tree and to save BST from degenerated form ) • An AVL tree is identical to a BST except ▪ height of the left and right subtrees can differ by at most 1. ▪ height of an empty tree is defined to be (–1). 9
  • 10. AVL Tree • An empty binary tree is an AVL Tree. • A nonempty binary tree T is an AVL Tree iff given TL and TR to be the left and right subtrees of T and h(TL) and h(TR) to be the heights of subtrees TL and TR respectively, TL and TR are AVL Trees and | h(TL) - h(TR) | ≤ 1
  • 11. Balanced Binary Tree • The height of a binary tree is the maximum level of its leaves (also called the depth). • The balance of a node in a binary tree is defined as the height of its left subtree minus height of its right subtree. • In balanced tree, each node has an indicated balance of 1, 0, or –1. 11
  • 12. Balance Factor • h(TL) - h(TR) is called Balance Factor (BF) • For AVL Tree, the balance factor of a node can be either 0, 1, -1
  • 13. AVL Tree 50 T L T R h=2 h=3 BF = h(TL)-h(TR) BF = 3-2 BF = 1
  • 14. Balanced Binary Tree -1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 This tree contains balance factors instead of data 14
  • 15. Balanced Binary Tree Insertions and effect on balance -1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 U1 U2 U3 U4 U5 U6 U7 U8 U9 U10 U11 U12 B B B B B B 15
  • 16. Balanced Binary Tree • Tree becomes unbalanced only if the newly inserted node ▪ is a left descendant (grand child) of a node that previously had a balance of 1 (U1 to U8), ▪ or is a right descendant (grand child) of a node that previously had a balance of –1 (U9 to U12) 16
  • 17. Balanced Binary Tree Insertions and effect on balance -1 0 1 0 0 0 0 -1 0 1 0 0 0 0 0 0 0 U1 U2 U3 U4 U5 U6 U7 U8 U9 U10 U11 U12 B B B B B B 17
  • 18. Inserting New Node in AVL Tree 1 0 A B T1 T3 T2 1 Use triangle notation for rest of the nodes 18
  • 19. Inserting New Node in AVL Tree 2 1 A B T1 T3 T2 new 1 2 Violate AVL condition 19
  • 20. Searching in AVL Tree • Searching an AVL search tree for an element is exactly similar to the method used in a binary search tree.
  • 21. An AVL Tree 5 8 2 4 3 1 7 Height = 4 0 1 2 3 21
  • 22. Not an AVL tree 6 8 1 4 1 5 Height = 4 0 1 2 3 22 • After insertion of node 5, BF of 6 node disturb • Before next insertion, we have to balance BST • To balance BST, we use Rotations • Rotations do shuffling of nodes and make BST balance and transform BFs to -1, 0 and 1
  • 23. Rotations •To perform the rotations it is necessary to identify a specific node A whose balance factor (BF) is neither 0, 1, -1 and which is the nearest ancestor to the inserted node on the path from the inserted node to the root.
  • 24. Rotation Types Note: The insertion occurs on the “outside” (i.e., left-left or right-right rotations) Insertion occurs on the “inside” i.e., right-left or left-right rotations) • LL rotation: Inserted node is in the left subtree of left subtree of node A. • RR rotation: Inserted node is in the right subtree of right subtree of node A. • LR rotation: Inserted node is in the right subtree of left subtree of node A. • RL rotation: Inserted node is in the left subtree of right subtree of node A.
  • 25. Trick to solve Note: We also say, LL and RR rotations can fix through Single Rotation. LR and RL Rotation can fix through Double Rotation • For LL and RR rotations, identify A and B, then make A as a child of B. • FOR LR and RL rotations, identify A, B and C, then make A and B child of C.
  • 26. Example of Rotation • Generate AVL Tree for values: 5, 7, 19, 12, 10, 15, 18, 20, 25, 23 • Solve Example on board….