SlideShare a Scribd company logo
1 of 50
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

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

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 

Recently uploaded (20)

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 

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