SlideShare a Scribd company logo
Class Roll: 19CSE001 to 19CSE014
AVL
TREE
TABLE OF
CONTENTS
Basic Concept of AVL Tree
01
03
02
04
06
08
Rotation in AVL Tree
Insertion in AVL Tree
Search in AVL
Tree
AVL Tree Deletion
Advantages, Disadvantages &
Applications
Code in AVL Tree
05
07 Complexities of AVL Tree
Basic Concept of
AVL Tree
01
Introduction to
AVL
● Named after inventors Adelson-Velsky and Landis invented in
1962.
● Self Balanced Binary Search Tree(Height Balancing of BST)
● Solve BST’s worst case situation(Left or Right Skewed BST)
BF = (height of left subtree) - (height of right
subtree)
● Uses “Balancing Factor(BF)” for height balancing
● BF only allows three values those are - 0 , 1 , -1
Fig(a): Balanced
BST
Fig(b): Left skewed BST
AVL Tree Rotation
02
❏ The tree is defined as a balanced AVL tree when the balance factor of each node is
between -1 and 1. On the other hand, when the balance factor is less than -1 or greater
than 1, then the tree is known as an unbalanced tree and needs to be balanced to get
the perfect AVL tree.
Balance Factor-
In AVL tree,
● Balance factor is defined for every node.
● Balance factor of a node = Height of its left subtree – Height of its right
subtree
● Rotation Operations in AVL Tree
Rotation is performed in AVL Tree to turn the unbalanced tree into a balanced
tree by performing various rotation operations.
In general, there are four types of Rotations in the AVL tree:
1. Left Rotation
2. Right Rotation
3. Left-Right Rotation
4. Right-Left Rotation
The first two rotations are known as single rotations, and the next two are known
as double rotations.
1. Right Rotation (RR):
When a node is inserted into the left subtree or deleted from the left subtree, the
AVL tree becomes unbalanced, and we need to balance it using LL rotation. This
LL rotation is also known as clockwise rotation applied on edge, which has the
highest balance factor.
2. Left Rotation(LR):
When a node gets inserted into the right subtree or deleted from the right subtree, the AVL tree
becomes unbalanced, and we need to balance it by rotating the node in the anti-clockwise
direction.
3. Left-Right Rotation(LR):
Left-Right Rotation is the combination of RR rotation and LL rotation. At first, RR rotation
is performed on the subtree then, LL rotation is performed on the part of the full tree from
inserted node to the first node
4. Right-Left Rotation(RL):
Right-left Rotation is the combination of LL rotation and RR rotation. In this case, the first
LL rotation is performed on the subtree where the change has been made; then, the RR
rotation is performed on the part of the full tree from the inserted node to the top of the
tree, that is, the first node.
03
AVL Tree Insertion
● Insertion is performed in the same way as in a binary search tree.
● The new node is added into AVL tree as the leaf node. The tree can be
balanced by applying rotations.
● Rotation is required only if, the balance factor of any node is disturbed
upon inserting the new node, otherwise the rotation is not required.
Insertion
Steps to follow for insertion
● Let the newly inserted node be w
○ Perform standard BST insert for w.
○ Starting from w, travel up and find the first unbalanced node. Let z be the first
unbalanced node, y be the child of z that comes on the path from w to z and x be the
grandchild of z that comes on the path from w to z.
○ Re-balance the tree by performing appropriate rotations on the subtree rooted with z.
There can be 4 possible cases that needs to be handled as x, y and z can be arranged in 4
ways. Following are the possible 4 arrangements:
■ y is left child of z and x is left child of y (Left Left Case)
■ y is left child of z and x is right child of y (Left Right Case)
■ y is right child of z and x is right child of y (Right Right Case)
■ y is right child of z and x is left child of y (Right Left Case)
Insertion
Insertion
Insertion
Insertion
Deletion in AVL Tree
04
Source Code
Explanation
05
After all There has been included a Implementation of Insertion and deletion procedure in C
language.
The source code link is:
https://drive.google.com/file/d/1m7bzOiNRaZtda6jogbD56n1WS7MCp-p9/view?usp=sharing
Let’s describe the code from code blocks-
Search in AVL Tree
06
● The search operation in an AVL tree makes it better than binary search tree.
● The searching time complexity of the AVL tree is only O(log N).
● Even in the worst-case, time complexity of searching operation in an AVL tree
is O(log(N)), where N is the number of nodes of the tree.
● Because the height of the AVL tree is always balanced with self-balancing
capabilities.
Steps to follow the search operation:
● Start from the root node.
● If the root node is NULL, return false.
● Check if the current node’s value is equal to the value of the node to be
searched. If yes, return true.
● If the current node’s value is less than searched key then recur to the right
subtree.
● If the current node’s value is greater than searched key then recur to the
left subtree.
● If the searched key is not found, then the key is not present in the tree.
Search Operation in AVL
Search (11), (61) & (22)
Implementation of Function to find a key in the AVL tree:
bool AVL_search(struct AVL_withparent* root, int key)
{
if (root == NULL) // If root is NULL
return false;
else if (root->key == key) // If found, return true
return true;
// Recur to the left subtree if the current node's value is greater than key
else if (root->key > key) {
bool val = AVL_search(root->left, key);
return val;
}
else {
bool val = AVL_search(root->right, key); // Otherwise, recur to the right subtree
return val;
}}
Implementation of main to find a key in the AVL tree:
int main()
{
struct AVL_withparent* root;
root = NULL;
root = Insert(root, NULL, 10); // Function call to insert the nodes
root = Insert(root, NULL, 20);
root = Insert(root, NULL, 30);
root = Insert(root, NULL, 40);
bool found = AVL_search(root, 40); // Function call to search for a node
if (found)
cout << "value found";
else
cout << "value not found";
return 0;
}
Time complexity & Space
Complexity
07
Time Complexity for Insertion
➢ Inserting an element into an AVL tree requires rotations, calculating the
balance factor and updating the height after insertion.
➢ Time for rotation —> constant time
➢ Time for calculating the balance factor —> constant time
➢ Traversing the tree for updating height —> O(log n)
SO the time complexity of insertion is O(log n).
Time Complexity for Search
➢ For search operation only need to traversing the tree
➢ Traversing the tree —> O(log n)
SO the time complexity for search is O(log n).
Time Complexity for Deletion
➢ Deleting an element from an AVL tree also requires rotations, calculating
the balance factor and updating the height after insertion.
➢ Time for rotation —> constant time
➢ Time for calculating the balance factor —> constant time
➢ Traversing the tree for updating height —> O(log n)
SO the time complexity of deletion is O(log n).
Space Complexity of AVL
O(log n)
OPERATION BEST CASE AVERAGE CASE WORST CASE
Insert O (log n) O (log n) O (log n)
Delete O (log n) O (log n) O (log n)
Search O (1) O (log n) O (log n)
Traversal O (log n) O (log n) O (log n)
BEST CASE AVERAGE CASE WORST CASE
O (n) O (n) O (n)
Space Complexity:
Advantages,
Disadvantages &
Application
08
Advantages of AVL Tree
- The height of the AVL tree is always balanced.
- The height never grows beyond log N, where N is the
total number of nodes in the tree.
- It gives better search time complexity when compared to
simple Binary Search trees.
- AVL trees have self-balancing capabilities.
Disadvantages of AVL Tree
- AVL trees can be difficult to implement.
- AVL trees have high constant factors for some
operations.
- Most STL implementations of the ordered associative
containers (sets, multisets, maps, and multimaps) use
red-black trees instead of AVL trees. Unlike AVL trees,
red-black trees require only one restructuring for removal
or insertion.
Application of AVL Tree
- In-memory sorts of sets and dictionaries.
- Database applications in which insertions and deletions
are fewer but there are frequent lookups for data
required.
- The applications that require improved searching apart
from the database applications.
- AVL tree is a balanced binary search tree which
employees rotation to maintain balance.
Problem-01:
In this problem you are given two type of query
1. Insert an integer to the list.
2. Given an integer x, you're about to find an integer k which represent x's index if the list is sorted in ascending order. Note that in
this problem we will use 1-based indexing.
As the problem title suggest, this problem intended to be solved using Balanced Binary Search Tree, one of its example is AVL Tree.
Input
The first line contains an integer Q, which denotes how many queries that follows.
The next Q lines will be one of the type queries which follow this format:
1 x means insert x to the list
2 x means find x's index if the list is sorted in ascending order.
Output
For each query type 2, print a line containing an integer as the answer or print "Data tidak ada" no quotes if the requested number does
not exist in the current lis.
Link–> https://www.spoj.com/problems/SDITSAVL/
Problem-01: Solution
1. typedef struct node {
2. int key;
3. struct node *left;
4. struct node *right;
5. int left_child;
6. int right_child;
7. int height;
8. } node;
1. int solve (node *root, int key) {
2. if (root != NULL) {
3. if (key > root -> key) return root -> left_child + 1 + solve(root -> right, key);
4. else if (key < root -> key) return solve(root -> left, key);
5. else return root -> left_child;
6. } else {
7. flag = 1;
8. return -1;
9. };
10. }
Link–> https://ideone.com/wNBots
THANKS

More Related Content

What's hot

1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
Krish_ver2
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdf
MaryJacob24
 
Binary tree
Binary tree Binary tree
Binary tree
Rajendran
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
Mahabubur Rahaman
 
Normalization
NormalizationNormalization
Normalization
Altafsoomro
 
Avl trees
Avl treesAvl trees
Avl trees
Mannan Masood
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 
Etl - Extract Transform Load
Etl - Extract Transform LoadEtl - Extract Transform Load
Etl - Extract Transform Load
ABDUL KHALIQ
 
FORESTS
FORESTSFORESTS
Red Black Tree
Red Black TreeRed Black Tree
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
SIVAKUMARM603675
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
Chhatra Thapa
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Zaid Hameed
 
Etl process in data warehouse
Etl process in data warehouseEtl process in data warehouse
Etl process in data warehouse
Komal Choudhary
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 

What's hot (20)

1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdf
 
Binary tree
Binary tree Binary tree
Binary tree
 
Introduction of sql server indexing
Introduction of sql server indexingIntroduction of sql server indexing
Introduction of sql server indexing
 
Normalization
NormalizationNormalization
Normalization
 
Avl trees
Avl treesAvl trees
Avl trees
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Etl - Extract Transform Load
Etl - Extract Transform LoadEtl - Extract Transform Load
Etl - Extract Transform Load
 
FORESTS
FORESTSFORESTS
FORESTS
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Etl process in data warehouse
Etl process in data warehouseEtl process in data warehouse
Etl process in data warehouse
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 

Similar to AVL Tree.pptx

Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
PRAKASH RANJAN SINGH
 
4. avl
4. avl4. avl
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
Malikireddy Bramhananda Reddy
 
AVL Tree
AVL TreeAVL Tree
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
Editor IJCTER
 
Avl tree
Avl treeAvl tree
AVL Tress
AVL TressAVL Tress
AVL Tress
Saifur Rahman
 
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
 
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 detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
Dr Sandeep Kumar Poonia
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
meenamadhuvandhi2
 
Ie
IeIe
Avl tree
Avl treeAvl tree
Avl tree
Van Pham
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
SamyakJain710491
 
Av ltrees
Av ltreesAv ltrees
Av ltrees
Jeet Poria
 
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
 
avl.ppt
avl.pptavl.ppt
avl.ppt
cotisa2402
 
avl.ppt
avl.pptavl.ppt
avl.ppt
plagcheck
 

Similar to AVL Tree.pptx (20)

Avl trees
Avl treesAvl trees
Avl trees
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
4. avl
4. avl4. avl
4. avl
 
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
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
Avl tree
Avl treeAvl tree
Avl tree
 
AVL Tress
AVL TressAVL Tress
AVL Tress
 
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
 
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
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
 
Ie
IeIe
Ie
 
Avl tree
Avl treeAvl tree
Avl tree
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
 
Av ltrees
Av ltreesAv ltrees
Av ltrees
 
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
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl.ppt
avl.pptavl.ppt
avl.ppt
 

Recently uploaded

Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 

Recently uploaded (20)

Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 

AVL Tree.pptx

  • 1. Class Roll: 19CSE001 to 19CSE014 AVL TREE
  • 2. TABLE OF CONTENTS Basic Concept of AVL Tree 01 03 02 04 06 08 Rotation in AVL Tree Insertion in AVL Tree Search in AVL Tree AVL Tree Deletion Advantages, Disadvantages & Applications Code in AVL Tree 05 07 Complexities of AVL Tree
  • 4. Introduction to AVL ● Named after inventors Adelson-Velsky and Landis invented in 1962. ● Self Balanced Binary Search Tree(Height Balancing of BST) ● Solve BST’s worst case situation(Left or Right Skewed BST) BF = (height of left subtree) - (height of right subtree) ● Uses “Balancing Factor(BF)” for height balancing ● BF only allows three values those are - 0 , 1 , -1
  • 7. ❏ The tree is defined as a balanced AVL tree when the balance factor of each node is between -1 and 1. On the other hand, when the balance factor is less than -1 or greater than 1, then the tree is known as an unbalanced tree and needs to be balanced to get the perfect AVL tree. Balance Factor- In AVL tree, ● Balance factor is defined for every node. ● Balance factor of a node = Height of its left subtree – Height of its right subtree
  • 8. ● Rotation Operations in AVL Tree Rotation is performed in AVL Tree to turn the unbalanced tree into a balanced tree by performing various rotation operations. In general, there are four types of Rotations in the AVL tree: 1. Left Rotation 2. Right Rotation 3. Left-Right Rotation 4. Right-Left Rotation The first two rotations are known as single rotations, and the next two are known as double rotations.
  • 9. 1. Right Rotation (RR): When a node is inserted into the left subtree or deleted from the left subtree, the AVL tree becomes unbalanced, and we need to balance it using LL rotation. This LL rotation is also known as clockwise rotation applied on edge, which has the highest balance factor.
  • 10. 2. Left Rotation(LR): When a node gets inserted into the right subtree or deleted from the right subtree, the AVL tree becomes unbalanced, and we need to balance it by rotating the node in the anti-clockwise direction.
  • 11. 3. Left-Right Rotation(LR): Left-Right Rotation is the combination of RR rotation and LL rotation. At first, RR rotation is performed on the subtree then, LL rotation is performed on the part of the full tree from inserted node to the first node 4. Right-Left Rotation(RL): Right-left Rotation is the combination of LL rotation and RR rotation. In this case, the first LL rotation is performed on the subtree where the change has been made; then, the RR rotation is performed on the part of the full tree from the inserted node to the top of the tree, that is, the first node.
  • 13. ● Insertion is performed in the same way as in a binary search tree. ● The new node is added into AVL tree as the leaf node. The tree can be balanced by applying rotations. ● Rotation is required only if, the balance factor of any node is disturbed upon inserting the new node, otherwise the rotation is not required. Insertion
  • 14. Steps to follow for insertion ● Let the newly inserted node be w ○ Perform standard BST insert for w. ○ Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z. ○ Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4 possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4 arrangements: ■ y is left child of z and x is left child of y (Left Left Case) ■ y is left child of z and x is right child of y (Left Right Case) ■ y is right child of z and x is right child of y (Right Right Case) ■ y is right child of z and x is left child of y (Right Left Case)
  • 19. Deletion in AVL Tree 04
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 31. After all There has been included a Implementation of Insertion and deletion procedure in C language. The source code link is: https://drive.google.com/file/d/1m7bzOiNRaZtda6jogbD56n1WS7MCp-p9/view?usp=sharing Let’s describe the code from code blocks-
  • 32. Search in AVL Tree 06
  • 33. ● The search operation in an AVL tree makes it better than binary search tree. ● The searching time complexity of the AVL tree is only O(log N). ● Even in the worst-case, time complexity of searching operation in an AVL tree is O(log(N)), where N is the number of nodes of the tree. ● Because the height of the AVL tree is always balanced with self-balancing capabilities.
  • 34. Steps to follow the search operation: ● Start from the root node. ● If the root node is NULL, return false. ● Check if the current node’s value is equal to the value of the node to be searched. If yes, return true. ● If the current node’s value is less than searched key then recur to the right subtree. ● If the current node’s value is greater than searched key then recur to the left subtree. ● If the searched key is not found, then the key is not present in the tree.
  • 35. Search Operation in AVL Search (11), (61) & (22)
  • 36. Implementation of Function to find a key in the AVL tree: bool AVL_search(struct AVL_withparent* root, int key) { if (root == NULL) // If root is NULL return false; else if (root->key == key) // If found, return true return true; // Recur to the left subtree if the current node's value is greater than key else if (root->key > key) { bool val = AVL_search(root->left, key); return val; } else { bool val = AVL_search(root->right, key); // Otherwise, recur to the right subtree return val; }}
  • 37. Implementation of main to find a key in the AVL tree: int main() { struct AVL_withparent* root; root = NULL; root = Insert(root, NULL, 10); // Function call to insert the nodes root = Insert(root, NULL, 20); root = Insert(root, NULL, 30); root = Insert(root, NULL, 40); bool found = AVL_search(root, 40); // Function call to search for a node if (found) cout << "value found"; else cout << "value not found"; return 0; }
  • 38. Time complexity & Space Complexity 07
  • 39. Time Complexity for Insertion ➢ Inserting an element into an AVL tree requires rotations, calculating the balance factor and updating the height after insertion. ➢ Time for rotation —> constant time ➢ Time for calculating the balance factor —> constant time ➢ Traversing the tree for updating height —> O(log n) SO the time complexity of insertion is O(log n).
  • 40. Time Complexity for Search ➢ For search operation only need to traversing the tree ➢ Traversing the tree —> O(log n) SO the time complexity for search is O(log n).
  • 41. Time Complexity for Deletion ➢ Deleting an element from an AVL tree also requires rotations, calculating the balance factor and updating the height after insertion. ➢ Time for rotation —> constant time ➢ Time for calculating the balance factor —> constant time ➢ Traversing the tree for updating height —> O(log n) SO the time complexity of deletion is O(log n).
  • 42. Space Complexity of AVL O(log n)
  • 43. OPERATION BEST CASE AVERAGE CASE WORST CASE Insert O (log n) O (log n) O (log n) Delete O (log n) O (log n) O (log n) Search O (1) O (log n) O (log n) Traversal O (log n) O (log n) O (log n) BEST CASE AVERAGE CASE WORST CASE O (n) O (n) O (n) Space Complexity:
  • 45. Advantages of AVL Tree - The height of the AVL tree is always balanced. - The height never grows beyond log N, where N is the total number of nodes in the tree. - It gives better search time complexity when compared to simple Binary Search trees. - AVL trees have self-balancing capabilities.
  • 46. Disadvantages of AVL Tree - AVL trees can be difficult to implement. - AVL trees have high constant factors for some operations. - Most STL implementations of the ordered associative containers (sets, multisets, maps, and multimaps) use red-black trees instead of AVL trees. Unlike AVL trees, red-black trees require only one restructuring for removal or insertion.
  • 47. Application of AVL Tree - In-memory sorts of sets and dictionaries. - Database applications in which insertions and deletions are fewer but there are frequent lookups for data required. - The applications that require improved searching apart from the database applications. - AVL tree is a balanced binary search tree which employees rotation to maintain balance.
  • 48. Problem-01: In this problem you are given two type of query 1. Insert an integer to the list. 2. Given an integer x, you're about to find an integer k which represent x's index if the list is sorted in ascending order. Note that in this problem we will use 1-based indexing. As the problem title suggest, this problem intended to be solved using Balanced Binary Search Tree, one of its example is AVL Tree. Input The first line contains an integer Q, which denotes how many queries that follows. The next Q lines will be one of the type queries which follow this format: 1 x means insert x to the list 2 x means find x's index if the list is sorted in ascending order. Output For each query type 2, print a line containing an integer as the answer or print "Data tidak ada" no quotes if the requested number does not exist in the current lis. Link–> https://www.spoj.com/problems/SDITSAVL/
  • 49. Problem-01: Solution 1. typedef struct node { 2. int key; 3. struct node *left; 4. struct node *right; 5. int left_child; 6. int right_child; 7. int height; 8. } node; 1. int solve (node *root, int key) { 2. if (root != NULL) { 3. if (key > root -> key) return root -> left_child + 1 + solve(root -> right, key); 4. else if (key < root -> key) return solve(root -> left, key); 5. else return root -> left_child; 6. } else { 7. flag = 1; 8. return -1; 9. }; 10. } Link–> https://ideone.com/wNBots