You are on page 1of 10

Data Structures and Algorithms

CIS-318
Lab Report no.12
Spring 2023
Obtained Marks
Total Marks

Lab Engineer Signature &


Comments

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

Department of Electrical Engineering


2 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm

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.1 Title: Implementation of AVL Tree Insertion &


Deletion Algorithm
12.2 Abstract:
This lab focused on understanding AVL trees and implementing rotation algorithms for AVL
insertion and deletion operations. AVL trees are self-balancing binary search trees that
maintain a balance criterion called the AVL property, ensuring that the heights of its left and
right subtrees differ by at most 1. The objectives of the lab were to grasp the concepts of AVL
trees, develop algorithms for AVL rotations, and implement these concepts in code.

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.

Key Features of AVL Trees:

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).

Benefits of AVL Trees:

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.

Predictable Performance: The self-balancing property of AVL trees guarantees a predictable


worst-case performance for operations. This makes them suitable for real-time systems or
applications where response time is critical.

Limitations of AVL Trees:


Additional Overhead: AVL trees require additional storage to maintain the balance factor for
each node. This additional overhead can be a disadvantage in memory-constrained
environments.

Costly Rotations: Performing rotations during insertions or deletions can be computationally


expensive, especially in large trees. AVL trees may incur higher overhead compared to simpler
data structures like binary search trees.

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.

Figure1 AVL Tree


5 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm

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.

 Right-Left Rotation (RL): The Right-Left Rotation is a combination of a right rotation


followed by a left rotation. It is performed when the left subtree of a node has a right-heavy
subtree. This rotation helps balance the tree by first rotating the left child's right subtree to the
right and then performing a left rotation.

 Left-Right Rotation (LR): The Left-Right Rotation is a combination of a left rotation


followed by a right rotation. It is performed when the right subtree of a node has a left-heavy
subtree. This rotation helps balance the tree by first rotating the right child's left subtree to the
left and then performing a right rotation.

 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.

12.5 Results and Code


Implement the Right( ), Left( ), Right-Left( ), Left-Right( ), DoubleLeft( ), DoubleRight( )
rotation algorithms in separate functions.
int get_height(Node* node) {
if (node == nullptr)
return 0;
return node->height;
}

int get_balance(Node* node) {


if (node == nullptr)
return 0;
return get_height(node->left) - get_height(node->right);
}

Node* right_rotate(Node* z) {
Node* y = z->left;
Node* T3 = y->right;

y->right = z;
z->left = T3;

z->height = max(get_height(z->left), get_height(z->right)) + 1;


6 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm

y->height = max(get_height(y->left), get_height(y->right)) + 1;

return y;
}

Node* left_rotate(Node* z) {
Node* y = z->right;
Node* T2 = y->left;

y->left = z;
z->right = T2;

z->height = max(get_height(z->left), get_height(z->right)) + 1;


y->height = max(get_height(y->left), get_height(y->right)) + 1;

return y;
}

//Right-Left( ), Left-Right( ), DoubleLeft( ), DoubleRight( ) rotation


algorithms in separate functions.
Node* right_left_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);
}

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;
}

if (key < root->key)


root->left = insert_node(root->left, key);
else
root->right = insert_node(root->right, key);

root->height = max(get_height(root->left), get_height(root->right)) +


1;

int balance = get_balance(root);

if (balance > 1 && key < root->left->key)


return right_rotate(root);

if (balance < -1 && key > root->right->key)


8 Lab 12 : Implementation of AVL Tree Insertion & Deletion Algorithm

return left_rotate(root);

if (balance > 1 && key > root->left->key) {


root->left = left_rotate(root->left);
return right_rotate(root);
}

if (balance < -1 && key < root->right->key) {


root->right = right_rotate(root->right);
return left_rotate(root);
}

return root;
}
2. Result:

Figure 3 Output after inserting 10 20 25 30 40 50 in the tree

3. • Implement the main AVLDeletion( ) Function that after deletion of node checks the
balance and calls rotation Functions accordingly.

Node* find_min_value_node(Node* node) {


Node* current = node;
while (current->left != nullptr)
current = current->left;
return current;
}

Node* AVLDeletion(Node* root, int key) {


if (root == nullptr)
return root;

if (key < root->key)


root->left = AVLDeletion(root->left, key);
else if (key > root->key)
root->right = AVLDeletion(root->right, key);
else {
if ((root->left == nullptr) || (root->right == nullptr)) {
Node* temp = root->left ? root->left : root->right;

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;

root->height = 1 + max(get_height(root->left), get_height(root-


>right));

int balance = get_balance(root);

if (balance > 1 && get_balance(root->left) >= 0)


return right_rotate(root);

if (balance > 1 && get_balance(root->left) < 0) {


root->left = left_rotate(root->left);
return right_rotate(root);
}

if (balance < -1 && get_balance(root->right) <= 0)


return left_rotate(root);

if (balance < -1 && get_balance(root->right) > 0) {


root->right = right_rotate(root->right);
return left_rotate(root);
}

return root;
}

Result:

Figure 1 Output after deleting 30 from the tree

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.6 Discussion and Conclusion:


The objective of this lab was to understand the concepts of AVL trees, including
rotation algorithms for AVL insertion and deletion, and to implement these concepts in code.
The AVL tree is a self-balancing binary search tree that maintains a balance criterion called the
AVL property, ensuring that the heights of its left and right subtrees differ by at most 1. We
explained the basic operations involved in AVL tree rotations. These operations include right
rotation (RR), left rotation (LL), right-left rotation (RL), left-right rotation (LR), double left
rotation (DDL), and double right rotation (DDR). Each rotation algorithm was implemented as
a separate function in the code.
Next, we discussed the implementation of the AVL insertion function. The main
AVLInsertion() function takes a node and a key as input, checks the balance of the tree after
inserting the node, and calls the rotation functions accordingly. The function recursively inserts
the node based on the key value and updates the heights of the nodes. If an imbalance is
detected, the appropriate rotation function is called to restore balance. In the results section,
figures were provided to visualize the AVL tree before and after insertion and deletion
operations. These figures demonstrate how the tree structure is adjusted and balanced through
rotations to maintain the AVL property.
In conclusion, this lab helped in gaining a better understanding of AVL trees, their
rotation algorithms, and their implementation for insertion and deletion operations. By
maintaining balance, AVL trees provide efficient search, insertion, and deletion operations,
making them suitable for various applications where fast data retrieval is crucial. However, it
is important to consider the additional overhead and computational cost associated with AVL
trees in certain scenarios.

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

You might also like