Avl Tree Project-Ds GRP Report
Avl Tree Project-Ds GRP Report
On
AVL TREE
SUBMITTED BY:
ASSISTANT PROFESSOR
KL UNIVERSITY
Green fields, Vaddeswaram – 522 502
Guntur Dt., AP, India.
1|P a ge
DEPARTMENT OF BASIC ENGINEERING SCIENCES
CERTIFICATE
2|P a ge
ACKNOWLEDGEMENTS
I express the sincere gratitude to our principal Dr. A. JAGADEESH for his
administration towards our academic growth.
KISHORE
3|P a ge
ABSTRACT
This code implements an AVL tree, a self-balancing binary search tree. Here's a
breakdown of the abstract:
Structure Definition:
Defines a structure node representing a node in the AVL tree. Each node contains an integer
data, pointers to the left and right children, and an integer ht representing the height of the
node.
Global Initialization:
Initializes the root of the AVL tree to NULL.
Function Prototypes:
Prototypes for functions used in the AVL tree operations are declared. These functions
include:
create: Creates a new node.
insert: Inserts a node into the AVL tree.
delete: Deletes a node from the AVL tree.
search: Searches for a node with a given key in the AVL tree.
rotate_left and rotate_right: Perform left and right rotations respectively to balance the AVL
tree.
balance_factor: Calculates the balance factor of a node.
height: Calculates the height of a node.
Traversal functions: inorder, preorder, and postorder for traversing the AVL tree.
Main Function:
Presents a menu-driven interface allowing users to perform various operations on the AVL
tree.
Operations include insertion, deletion, searching, and different traversal methods.
The loop continues until the user decides to exit.
4|P a ge
INDEX
5. Implementation 12-26
8. Conclusion 29
5|P a ge
INTRODUCTION
AVL Tree:
An AVL tree is a type of binary search tree where the heights of the two child subtrees
of any node differ by at most one. This property ensures that the tree remains balanced
and prevents degeneration into a linked list, which would result in poor performance
for search, insertion, and deletion operations.
Structure Definition:
The struct node defines the structure of a node in the AVL tree. Each node contains an
integer value (data), pointers to its left and right children, and an integer ht
representing the height of the node.
Operations:
Insertion (insert function): Inserts a new node into the AVL tree while maintaining its
balance by performing rotations when necessary.
Deletion (delete function): Deletes a node from the AVL tree while ensuring that the
tree remains balanced by performing rotations if required.
Search (search function): Searches for a node with a specified key in the AVL tree.
Balancing Operations:
The code implements left and right rotations (rotate_left and rotate_right functions) to
balance the tree when necessary. These rotations are performed based on the balance
factor calculated for each node.
User Interface:
The code provides a simple command-line interface that allows users to interact with
the AVL tree. Users can choose from various options such as insertion, deletion,
searching, and different types of tree traversals.
6|P a ge
AIM
The aim is to create a robust and efficient implementation of an AVL tree
data structure that can be easily understood, utilized, and extended for various
applications requiring dynamic ordered data storage and retrieval.
Advantages:- Balanced Structure: AVL trees maintain balance, ensuring that the
height difference between the left and right subtrees of any node is at most one. This
property facilitates efficient search, insertion, and deletion operations with a time
complexity of O(log n).
Efficient Operations: Due to their balanced nature, AVL trees offer efficient search,
insertion, and deletion operations compared to unbalanced binary search trees. This
efficiency is crucial for applications requiring frequent data manipulation.
Memory Overhead: AVL trees require additional memory overhead to store balance
information (e.g., height) for each node. This overhead may be significant for large
trees with millions of nodes, impacting memory usage efficiency.
Future enhancements:-
Concurrency Support:
Iterative Algorithms:
Optimize recursive algorithms for tree operations (such as insertion, deletion, and
traversal) to iterative versions. Iterative algorithms can reduce function call overhead
and stack usage, potentially improving performance, especially for large trees.
Memory Efficiency:
7|P a ge
Experiment with dynamic rebalancing strategies that adaptively adjust the balancing
thresholds based on the tree's structure and workload characteristics. This could
potentially reduce the frequency of rebalancing operations and improve overall
performance.
Extend the AVL tree implementation to support persistence, allowing the tree to be
serialized to disk or other storage mediums. Persistent AVL trees enable efficient disk-
based storage and retrieval of large datasets, suitable for applications requiring
durability and scalability.
8|P a ge
SYSTEM REQUIREMENTS
➢ SOFTWARE REQUIREMENTS:
The major software requirements of the project are as follows:
Language : Turbo-C
Operating system: Windows Xp or later.
➢ HARDWARE REQUIREMENTS:
The hardware requirements that map towards the software are as follows:
RAM : The amount of RAM required depends on the size of the AVL
tree and the operations performed on it. For small to moderate-sized trees and typical
usage scenarios, a few megabytes of RAM should be sufficient
9|P a ge
DATA FLOW DIAGRAM
10 | P a g e
ALGORITHM
Node Structure: Defines a structure for AVL tree nodes, including data, left and
right child pointers, and a height parameter.
Global Variables: Initializes a global variable root as the root of the AVL tree.
Main Function: Provides a menu-driven interface for users to interact with the AVL
tree.
Insertion (insert function): Inserts a new node into the AVL tree and performs
rotations to maintain balance.
Deletion (delete function): Deletes a node from the AVL tree and rebalances if
necessary.
Search (search function): Searches for a node with a given key in the AVL tree.
Traversal options: Inorder, preorder, and postorder traversal of the AVL tree.
Helper Functions:
rotate_left and rotate_right: Perform left and right rotations to balance the tree.
balance_factor and height: Calculate the balance factor and height of a node.
11 | P a g e
IMPLEMENTATION
#include<stdio.h>
#include<stdlib.h>
// function prototyping
struct node* create(int);
struct node* insert(struct node*, int);
struct node* delete(struct node*, int);
struct node* search(struct node*, int);
struct node* rotate_left(struct node*);
struct node* rotate_right(struct node*);
int balance_factor(struct node*);
int height(struct node*);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);
int main()
{
int user_choice, data;
char user_continue = 'y';
struct node* result = NULL;
12 | P a g e
printf("\n\nEnter Your Choice: ");
scanf("%d", &user_choice);
switch(user_choice)
{
case 1:
printf("\nEnter data: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("\nEnter data: ");
scanf("%d", &data);
root = delete(root, data);
break;
case 3:
printf("\nEnter data: ");
scanf("%d", &data);
result = search(root, data);
if (result == NULL)
{
printf("\nNode not found!");
}
else
{
printf("\n Node found");
}
break;
case 4:
inorder(root);
break;
case 5:
preorder(root);
break;
case 6:
postorder(root);
break;
case 7:
printf("\n\tProgram Terminated\n");
return 1;
13 | P a g e
default:
printf("\n\tInvalid Choice\n");
}
return 0;
}
14 | P a g e
{
struct node* left_child = root->left;
root->left = left_child->right;
left_child->right = root;
15 | P a g e
if (lh > rh)
return (lh);
return (rh);
}
16 | P a g e
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
// update the heights of the nodes
root->ht = height(root);
return root;
}
if (root == NULL)
{
return NULL;
}
if (x > root->data)
{
root->right = delete(root->right, x);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else if (x < root->data)
{
root->left = delete(root->left, x);
if (balance_factor(root) == -2)
{
17 | P a g e
if (balance_factor(root->right) <= 0)
{
root = rotate_left(root);
}
else
{
root->right = rotate_right(root->right);
root = rotate_left(root);
}
}
}
else
{
if (root->right != NULL)
{
temp = root->right;
while (temp->left != NULL)
temp = temp->left;
root->data = temp->data;
root->right = delete(root->right, temp->data);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else
{
return (root->left);
}
}
root->ht = height(root);
return (root);
}
18 | P a g e
if (root == NULL)
{
return NULL;
}
if(root->data == key)
{
return root;
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
19 | P a g e
// postorder traversal of the tree
void postorder(struct node* root)
{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
20 | P a g e
INTEGRATION AND SYSTEM TESTING
OUTPUTS
Screen Shots:
21 | P a g e
22 | P a g e
CONCLUSION
This AVL tree project in C provides a functional implementation of a self-balancing binary
search tree.
Functionality: The project allows users to insert, delete, search for nodes, and perform various
traversals (inorder, preorder, postorder) on the AVL tree.
Balancing: It includes balancing operations (rotations) to ensure that the AVL tree remains
balanced after insertions and deletions, maintaining the AVL property (balance factor of each
node is either -1, 0, or 1).
User Interface: The menu-driven interface in the main function makes it user-friendly, enabling
users to interact with the AVL tree operations easily.
Error Handling: The code includes basic error handling, such as checking for memory
allocation errors during node creation and handling cases where nodes or keys are not found
during deletion or search operations.
Tree Traversals: It provides functionalities for three different types of tree traversals, allowing
users to observe the structure of the AVL tree.
23 | P a g e