Team 10 - AVL Tree (FS Mini Project)
Team 10 - AVL Tree (FS Mini Project)
CERTIFICATE
This is to certify that the File Structures Project work entitled “AVL Tree” has been carried
out by Umar Farouque (1CR18IS162), Varun C Acharya (1CR18IS168) and Yuvraj
Gandhi (1CR18IS177) bonafide students of CMR Institute of Technology in partial
fulfillment for the award of Bachelor of Engineering in Information Science and
Engineering of the Visvesvaraya Technological University, Belgaum during the year
2018-2019. It is certified that all corrections/suggestions indicated for Internal
Assessment have been incorporated in the Report deposited in the departmental library.
This File
Structures mini Project Report has been approved as it satisfies the academic requirements in
respect of project work prescribed for the said degree.
----------------------- ----------------------
Signature of Guide Signature of HOD
External Viva
Name of the examiners Signature with date
1.
2.
TABLE OF CONTENT
Acknowledgement................................................................................................................ I
Abstract................................................................................................................................. II
Chapter 1
Introduction..................................................................................................................1
1.1 Brief History of AVL Tree...................................................................................................1
Chapter 2
Problem Statement...................................................................................................2-5
2.1 AVL Tree Data Structure.................................................................................................2-3
2.2 AVL Tree Rotations........................................................................................................ 3-5
2.2.1 Single Left Rotation (LL Rotation)....................................................................3-4
2.2.2 Single Right Rotation (RR Rotation).............................................................4
2.2.3 Left Right Rotation (LR Rotation).................................................................4-5
2.2.4 Right Left Rotation (RL Rotation).................................................................5
Chapter 3
Operations on AVL Tree...........................................................................................6-11
3.1 Search Operations........................................................................................................... 6
3.2 Insertion Operations........................................................................................................ 7-9
3.3 Deletion Operations......................................................................................................... 9
3.4 Tree Traversals..............................................................................................................10-11
3.4.1 Inorder Traversal........................................................................................10
3.4.2 Preorder Traversal......................................................................................10-11
3.4.3 Postorder Traversal....................................................................................11
Chapter 4
Code Snippet & Implementation….......................................................................12-17
Chapter 5
Results & Analysis................................................................................................18-22
Chapter 6
Conclusion.................................................................................................................23
References.................................................................................................................24
ACKNOWLEDGEMENT
The satisfaction and euphoria that accompany a successful completion of any task would
be incomplete without the mention of people who made it possible. Success is the epitome of
hard work and perseverance, but steadfast of all is encouraging guidance.
So with gratitude I acknowledge all those whose guidance and encouragement served as
beacon of light and crowned our effort with success.
I would like to thank Dr. Sanjay Jain, Principal, CMRIT, Bangalore for providing an
excellent academic environment in the college and his never-ending support for the B.E program.
I would also like to thank Dr. M Farida Begam, Assoc. Professor and HOD, Department
of Information Science and Engineering, CMRIT, Bangalore who shared his opinions and
experiences through which I received the required information crucial for the project.
I would also like to thank all the faculty members who have always been very Co-
operative and generous. Conclusively, I also thank all the non-teaching staff and all others who
have done immense help directly or indirectly during my project
Umar Farouque
Varun C Acharya
Yuvraj Gandhi
i
ABSTRACT
AVL Tree is the first dynamic tree in data structure which minimizes its height during insertion
and deletion operations. This is because search time is directly proportional to the height of the
binary search tree (BST).
When insertion operation is performed it may result in increasing the height of the tree and when
deletion is performed it may result in decreasing the height of the tree. To make the BST a height
balance tree (AVL Tree) creators of AVL Tree proposed various rotations. This paper tells us
about the AVL Tree, the operations that are performed such as rotations, insertion, deletion and
tree traversals.
ii
AVL Tree
Chapter 1
INTRODUCTION
AVL Tree is a height-balanced binary tree. Each node is associated with a balanced factor
which is calculated as the difference between the height of its left subtree and the right
subtree.
In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a
self-balancing binary search tree. It was the first such data structure to be invented. In an
AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any
time they differ by more than one, rebalancing is done to restore this property. Lookup,
insertion, and deletion all take O(log n) time in both the average and worst cases, where n is
the number of nodes in the tree prior to the operation. Insertions and deletions may require
the tree to be rebalanced by one or more tree rotations.
The AVL tree is named after its two Soviet inventors, Georgy Adelson-Velsky and Evgenii
Landis, who published it in their 1962 paper "An algorithm for the organization of
information".
AVL trees are often compared with red–black trees because both support the same set of
operations and take O(log n) time for the basic operations. For lookup-intensive applications,
AVL trees are faster than red–black trees because they are more strictly balanced. Similar to
red–black trees, AVL trees are height-balanced. Both are, in general, neither weight-balanced
nor μ-balanced for any μ ≤ 1/2 that is, sibling nodes can have hugely differing numbers of
descendants.
Problem Statement
2.1 AVL Tree Data Structure
AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary
search tree but it is a balanced tree. A binary tree is said to be balanced if the difference
between the heights of left and right subtrees of every node in the tree is either -1, 0 or +1. In
other words, a binary tree is said to be balanced if the height of left and right children of
every node differ by either -1, 0 or +1. In an AVL tree, every node maintains an extra
information known as balance factor. The AVL tree was introduced in 1962 by G.M.
Adelson-Velsky and E.M. Landis.
An AVL tree is a balanced binary search tree. In an AVL tree, the balance factor of
every node is either -1, 0 or +1.
Balance factor of a node is the difference between the heights of the left and right subtrees of
that node. The balance factor of a node is calculated either height of left subtree - height of
right subtree (OR) height of right subtree - height of left subtree. In the following
explanation, we calculate as follows…
Rotation is the process of moving nodes either to left or to right to make the tree
balanced.
There are four rotations and they are classified into two types:
In an AVL tree, the search operation is performed with O(log n) time complexity. The search
operation in the AVL tree is similar to the search operation in a Binary search tree. We use the
following steps to search for an element in the AVL tree…
● Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
● Step 2 - After insertion, check the Balance Factor of every node.
● Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for the next
operation.
● Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is
said to be imbalanced. In this case, perform a suitable Rotation to make it balanced
and go for the next operation.
Uses of Inorder
In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing
order. To get nodes of BST in non-increasing order, a variation of Inorder traversal
where Inorder traversal is reversed can be used.
Example: Inorder traversal for the above-given figure is 4 2 5 1 3.
Uses of Preorder
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to
get prefix expressions on an expression tree.
Example: Preorder traversal for the above given figure is 1 2 4 5 3.
Uses of Postorder
Postorder traversal is used to delete the tree.
Example: Postorder traversal for the above given figure is 4 5 2 3 1.
Chapter 4
Implementation
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
//Node Declaration
struct avl_node
{
int data;
struct avl_node * left;
struct avl_node * right;
}* root;
//Class Declaration
class avlTree
{ public:
int height(avl_node * );
int diff(avl_node * );
avl_node * rr_rotation(avl_node * );
avl_node * ll_rotation(avl_node * );
avl_node * lr_rotation(avl_node * );
avl_node * rl_rotation(avl_node * );
avl_node * balance(avl_node * );
avl_node * insert(avl_node * , int);
avlTree() {
root = NULL;
}
};
int main() {
int choice, item;
avlTree avl;
while (1) {
cout << "\n--------------------" << endl;
cout << "AVL Tree Implementation" << endl;
cout << "--------------------" << endl;
cout << "1.Insert Element into the tree" << endl;
cout << "2.Display Balanced AVL Tree" << endl;
cout << "3.InOrder traversal" << endl;
cout << "4.PreOrder traversal" << endl;
cout << "5.PostOrder traversal" << endl;
cout << "6.Exit" << endl;
cout << "Enter your Choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to be inserted: ";
cin >> item;
root = avl.insert(root, item);
break;
case 2:
if (root == NULL) {
cout << "Tree is Empty" << endl;
continue;
}
cout << "Balanced AVL Tree:" << endl;
avl.display(root, 1);
break;
case 3:
cout << "Inorder Traversal:" << endl;
avl.inorder(root);
cout << endl;
break;
case 4:
cout << "Preorder Traversal:" << endl;
avl.preorder(root);
cout << endl;
break;
case 5:
cout << "Postorder Traversal:" << endl;
avl.postorder(root);
cout << endl;
break;
case 6:
exit(1);
break;
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
if (temp != NULL) {
int l_height = height(temp -> left);
int r_height = height(temp -> right);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}
//Height Difference
return temp;
}
return ll_rotation(parent);
}
return rr_rotation(parent);
}
if (bal_factor > 1) {
if (diff(temp -> left) > 0)
temp = ll_rotation(temp);
else
temp = lr_rotation(temp);
} else if (bal_factor < -1) {
if (diff(temp -> right) > 0)
temp = rl_rotation(temp);
else
temp = rr_rotation(temp);
}
return temp;
}
return root;
} else if (value < root -> data) {
root -> left = insert(root -> left, value);
root = balance(root);
} else if (value >= root -> data) {
root -> right = insert(root -> right, value);
root = balance(root);
}
return root;
}
if (ptr != NULL) {
display(ptr -> right, level + 1);
printf("\n");
if (ptr == root)
cout << "Root -> ";
{ if (tree == NULL)
return;
Conclusion
AVL trees are balanced binary trees that are mostly used in database indexing.
All the operations performed on AVL trees are similar to those of binary search trees but the
only difference in the case of AVL trees is that we need to maintain the balance factor i.e. the
data structure should remain a balanced tree as a result of various operations. This is achieved
by using the AVL Tree Rotation operation.
They have efficient time and space complexities. As a programmer, it is key to understand the
functioning and the implementation of an AVL tree such that it can be mapped to a real world
scenario.
1. AVL trees are mostly used for in-memory sorts of sets and dictionaries.
2. AVL trees are also used extensively in database applications in which insertions and
deletions are fewer but there are frequent lookups for data required.
3. It is used in applications that require improved searching apart from the database
applications.
References
● https://en.wikipedia.org
● Youtube videos on AVL Tree insertion, deletion and rotation operations.
● https://www.geeksforgeeks.org