Professional Documents
Culture Documents
CIS-318
Lab Report no.12
Spring 2023
Obtained Marks
Total Marks
Student Name
1. Anas Zohrab
Section: A (Electronics)
Experiment No: 12 Date of Submission:
May-15-2023
Experiment Title:
Implementation of AVL Tree Insertion & Deletion Algorithm
Batch: Teacher:
BSEE 2019-23 Dr. Kamran Safdar
Semester Lab Engineer:
8th Mr. Ghaznooq Ahmad
Table of Contents
Implementation of AVL Tree Insertion & Deletion Algorithm......................................................................... 1
12.1 Title: Implementation of AVL Tree Insertion & Deletion Algorithm .................................................... 3
12.2 Abstract: .................................................................................................................................................. 3
12.3 Objectives: .............................................................................................................................................. 3
12.4 Introduction:............................................................................................................................................ 3
12.5 Results and Code..................................................................................................................................... 5
1. • Implement the main AVLinsertion( ) Function that after insertion of node checks the balance
and call rotation Functions accordingly. ............................................................................................... 7
2. Result:.................................................................................................................................................... 8
........................................................................................................................................................................ 8
3. • Implement the main AVLDeletion( ) Function that after deletion of node checks the balance
and calls rotation Functions accordingly. .............................................................................................. 8
.................................................................................................................................................................... 9
12.6 Discussion and Conclusion: .................................................................................................................. 10
12.7 References: ............................................................................................................................................ 10
3 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm
12.3 Objectives:
✓ Understanding of AVL tree concepts including rotation algorithms for AVL Insertion &
Deletion function
✓ Developing an algorithm for AVL insertion and Deletion rotations
✓ Implementing the AVL rotation concepts for implementing AVL insertion and deletion
function
12.4 Introduction:
AVL Tree :
AVL trees are self-balancing binary search trees that ensure the tree remains balanced,
providing efficient insertion, deletion, and searching operations. They were named after their
inventors, Adelson-Velskii and Landis. AVL trees maintain a specific balance criterion called
the AVL property, which states that for every node in the tree, the heights of its left and right
subtrees differ by at most 1.
Binary Search Tree (BST) Property: Like other binary search trees, AVL trees maintain the
property that the key in each node is greater than all keys in its left subtree and smaller than all
keys in its right subtree, ensuring efficient search operations.
Balance Factor: AVL trees introduce the concept of a balance factor for each node, which is
the difference between the heights of its left and right subtrees. The balance factor can be -1,
0, or 1 for each node to satisfy the AVL property.
Rotations: AVL trees utilize rotations to maintain balance. When an insertion or deletion
causes an imbalance in the tree, rotations are performed to adjust the structure and restore
balance. The two main types of rotations are left rotations and right rotations.
Self-Balancing Property: The self-balancing property of AVL trees ensures that the tree
remains balanced after every insertion or deletion operation. This property guarantees that thE
4 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm
height of the tree remains logarithmic, resulting in efficient search, insertion, and deletion
operations with a time complexity of O(log n).
Efficient Operations: AVL trees provide fast search, insert, and delete operations with a
balanced height, resulting in a time complexity of O(log n). This makes them suitable for
applications where efficient data retrieval is crucial
Automatic Balancing: Unlike ordinary binary search trees, AVL trees automatically maintain
balance. This eliminates the need for manual rebalancing and ensures that the tree remains
efficient regardless of the order of insertions or deletions.
Restrictive Balance Criterion: The AVL property enforces strict balance criteria, which may
result in frequent rotations and updates, impacting the performance for certain usage patterns.
In some scenarios, other self-balancing tree structures like Red-Black trees may offer better
performance.
Basic Operations:
Right Rotation (RR): The Right Rotation is performed when the left subtree of a node
becomes larger than the right subtree. It restructures the tree by rotating the unbalanced node
and its left child to the right, making the left child the new root.
Left Rotation (LL): The Left Rotation is performed when the right subtree of a node
becomes larger than the left subtree. It restructures the tree by rotating the unbalanced node
and its right child to the left, making the right child the new root.
Double Left Rotation (DDL): The Double Left Rotation is performed when a node's right
subtree is unbalanced and the right child's left subtree is larger than its right subtree. It
involves a left-right rotation to rebalance the tree.
Double Right Rotation (DDR): The Double Right Rotation is performed when a node's left
subtree is unbalanced and the left child's right subtree is larger than its left subtree. It involves
a right-left rotation to rebalance the tree.
Node* right_rotate(Node* z) {
Node* y = z->left;
Node* T3 = y->right;
y->right = z;
z->left = T3;
return y;
}
Node* left_rotate(Node* z) {
Node* y = z->right;
Node* T2 = y->left;
y->left = z;
z->right = T2;
return y;
}
Node* left_right_rotate(Node* z) {
Node* y = z->left;
Node* x = y->right;
Node* T2 = x->left;
x->left = y;
y->right = T2;
z->left = x;
y->height = max(get_height(y->left), get_height(y->right)) + 1;
z->height = max(get_height(z->left), get_height(z->right)) + 1;
x->height = max(get_height(x->left), get_height(x->right)) + 1;
return right_rotate(z);
}
Node* double_left_rotate(Node* z) {
Node* y = z->left;
Node* x = y->right;
7 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm
Node* T2 = x->left;
x->left = y;
y->right = T2;
z->left = x;
y->height = max(get_height(y->left), get_height(y->right)) + 1;
z->height = max(get_height(z->left), get_height(z->right)) + 1;
x->height = max(get_height(x->left), get_height(x->right)) + 1;
return right_rotate(z);
}
Node* double_right_rotate(Node* z) {
Node* y = z->right;
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
z->right = x;
y->height = max(get_height(y->left), get_height(y->right)) + 1;
z->height = max(get_height(z->left), get_height(z->right)) + 1;
x->height = max(get_height(x->left), get_height(x->right)) + 1;
return left_rotate(z);
}
1. • Implement the main AVLinsertion( ) Function that after insertion of node checks the
balance and call rotation Functions accordingly.
Node* insert_node(Node* root, int key) {
if (root == nullptr) {
Node* newNode = new Node();
newNode->key = key;
newNode->left = nullptr;
newNode->right = nullptr;
newNode->height = 1;
return newNode;
}
return left_rotate(root);
return root;
}
2. Result:
3. • Implement the main AVLDeletion( ) Function that after deletion of node checks the
balance and calls rotation Functions accordingly.
if (temp == nullptr) {
temp = root;
root = nullptr;
}
else
9 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm
*root = *temp;
delete temp;
}
else {
Node* temp = find_min_value_node(root->right);
root->key = temp->key;
root->right = AVLDeletion(root->right, temp->key);
}
}
if (root == nullptr)
return root;
return root;
}
Result:
Q3 : Write the time-complexity of both AVL tree insertion and Deletion tree function.
The time complexity of AVL tree insertion and deletion operations is O(log n), where "n" represents the
number of nodes in the tree.
10 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm
12.7 References:
GeeksforGeeks. (n.d.). AVL Tree | Set 1 (Insertion). Retrieved from
https://www.geeksforgeeks.org/avl-tree-set-1-insertion/
Tutorials Point. (n.d.). AVL Tree. Retrieved from
https://www.tutorialspoint.com/data_structures_algorithms/avl_tree_algorithm.htm
Programiz. (n.d.). AVL Tree in C++. Retrieved from https://www.programiz.com/dsa/avl-
tree