You are on page 1of 31

K L UNIVERSITY

FRESHMAN ENGINEERING DEPARTMENT


A Project Based Lab Report On

Employee management system

SUBMITTED BY:

ID NUMBER NAME

2300080082 Y. Hema Pushpa

2300080053 S. Bhavyanka Sri

2300080104 M. Sai Raghava

23000800102 G. Sri Tanuja

2300080112 Sai Surchit Das

UNDER THE ESTEEMED GUIDANCE OF

L.Ushasree

Ass.professor

KL UNIVERSITY
Green fields, Vaddeswaram – 522 502
Guntur Dt., AP, India.
DEPARTMENT OF BASIC ENGINEERING SCIENCES

CERTIFICATE

This is to certify that the project based laboratory report entitled


AVL TREES AND ITS OPERATIONS submitted by Mr./Ms. Y. Hema Pushpa, S.
Bhavyanka Sri, M. Sai Raghava , G. Sri Tanuja, Sai Surchit Das bearing Regd. No.
2300080082, 2300080053, 23000800104, 23000800102, 2300080112 to the Department of Basic
Engineering Sciences, KL University in partial fulfillment of the requirements for the completion
of a project based Laboratory in ”DATA STRUCTURES-
23SC1202” course in I B Tech II Semester, is a bonafide record of the work carried out by L.USHA
SREE under my supervision during the academic year 2023– 2024.

PROJECT SUPERVISOR HEAD OF THE DEPARTMENT

L.Usha Sree Dr. D.HARITHA

ACKNOWLEDGEMENTS
It is great pleasure for me to express my gratitude to our honorable President Sri. Koneru
Satyanarayana, for giving the opportunity and platform with facilities in accomplishing the project
based laboratory report.

I express the sincere gratitude to our principal Dr. A. Anand Kumar for his administration
towards our academic growth.

I express sincere gratitude to our Coordinator Dr. D.Haritha for her leadership and constant motivation
provided in successful completion of our academic semester.

I record it as my privilege to deeply thank our pioneer Dr. D.Haritha, HOD-BES for providing
us the efficient faculty and facilities to make our ideas into reality.

I express my sincere thanks to our project supervisor L. USHA SREE for his/her novel
association of ideas, encouragement, appreciation and intellectual zeal which motivated us to venture
this project successfully.

Finally, it is pleased to acknowledge the indebtedness to all those who devoted themselves
directly or indirectly to make this project report success.

2300080082 Y HEMA PUSHPA

2300080053 S.BHAVYANKA SRI

2300080104 M.SAI RAGHAVA

2300080102 G.SRI TANUJA

2300080112 Sai SURCHIT DAS


INDEX

S.NO TITLE PAGE NO

1. Abstract 5

2. Introduction 6

3. Aim of the Project 7

2.1 Advantages & Disadvantages 7

2.2 Future Implementation 7

4. Software & Hardware Details 8

5. Data Flow Diagram 9

6. Algorithm for each module 10

7. Implementation 11-13

8. Integration and System Testing 14-15

8. Conclusion 16

4|Page
ABSTRACT
AVL trees are self-balancing binary search trees, crucial in maintaining efficient
search, insertion, and deletion operations. This abstract explores AVL trees, outlining
their structure and fundamental operations.

AVL trees ensure that the height difference between left and right subtrees of any
node (known as the balance factor) remains within [-1, 1], maintaining balance and
guaranteeing logarithmic time complexity for search, insertion, and deletion.

The abstract delves into AVL tree operations, including rotation techniques to
rebalance the tree when necessary. Rotations, such as single and double rotations,
preserve the binary search tree property while restoring balance.

Additionally, the abstract discusses the importance of maintaining AVL tree balance
during insertions and deletions. Insertions and deletions may temporarily violate the
AVL property, requiring rebalancing through rotations to restore balance and
optimize performance.

Furthermore, the abstract touches upon AVL tree variants and enhancements, such as
threaded AVL trees or AVL trees augmented with additional data for specific
applications, emphasizing the adaptability and versatility of AVL trees in various
contexts.

5|Page
INTRODUCTION

AVL trees represent a cornerstone in the realm of data structures, offering a dynamic
and efficient solution for maintaining ordered sets or maps. Introduced by Adelson-
Velsky and Landis in 1962, AVL trees are self-balancing binary search trees designed
to ensure fast search, insertion, and deletion operations by preserving balance
through automatic rebalancing.

The structure of an AVL tree closely resembles that of a binary search tree (BST), with
each node having at most two children: a left child and a right child. However, what
sets AVL trees apart is their ability to maintain balance, defined by the constraint that
the height difference between the left and right subtrees of any node must be within
the range of [-1, 1].

In this introduction, we embark on a journey to explore AVL trees and their


operations comprehensively. We delve into the underlying principles governing AVL
trees, the mechanisms employed to maintain balance, and the intricacies of
operations such as insertion, deletion, and traversal.

By understanding AVL trees and their operations, we gain valuable insights into the
realm of balanced binary search trees, equipping ourselves with powerful tools for
efficiently managing and manipulating large datasets in diverse computational
applications.

6|Page
AIM

Advantages:-

1. Guaranteed Logarithmic Time Complexity


2. Self-Balancing Property
3. Consistent Performance
4. Efficient Insertion and Deletion
5. Versatility and Adaptability
6. Support for Range Queries
7. Minimal Memory Overhead
8. Well-Defined Operations

Disadvantages:-

1. Complexity of Implementation
2. Overhead of Balancing Operations
3. Memory Overhead
4. Difficulty in Real-Time Applications
5. Less Efficient in Highly Dynamic Environments

Future enhancements:-

1. Concurrency Support
2. Cache-conscious Optimizations
3. Adaptive Balancing Strategies
4. Hybrid Data Structures
5. Persistent AVL Trees
6. Adaptive Node Splitting and Merging
7. GPU Acceleration

7|Page
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 : 16GB

Processor : 64bit

8|Page
DATA FLOW DIAGRAM

9|Page
ALGORITHM

Step 1: Define AVL tree structure and global root variable.

Step 2: Include necessary header files.

Step 3: Declare function prototypes.

Step 4: Define main() function.

Step 5: Declare user variables.

Step 6: Print user menu.

Step 7: User interaction loop.

Step 8: Handle user choice.

Step 9: Perform chosen operation.

Step 10: Prompt for continuation.

10 | P a g e
IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>

// structure of the tree node


struct node
{
int data;
struct node *left;
struct node *right;
int ht;
};

// global initialization of root node


struct node *root = NULL;

// function prototyping
struct node *create(int);
struct node *insert(struct node *, int);
struct node *deleteNode(struct node *, int);
struct node *search(struct node *, int);
struct node *rotate_left(struct node *);
struct node *rotate_right(struct node *);

11 | P a g e
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;

printf("\n\n------- AVL TREE --------\n");

while (user_continue == 'y' || user_continue == 'Y')


{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Inorder");
printf("\n5. Preorder");
printf("\n6. Postorder");
printf("\n7. EXIT");
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);
printf("\n%d inserted successfully.", data);
break;

case 2:
printf("\nEnter data to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
printf("\n%d deleted successfully.", data);
break;

case 3:
printf("\nEnter data to search: ");
scanf("%d", &data);
13 | P a g e
result = search(root, data);
if (result == NULL)
{
printf("\n%d not found!", data);
}
else
{
printf("\n%d found.", data);
}
break;

case 4:
printf("\nInorder traversal: ");
inorder(root);
break;

case 5:
printf("\nPreorder traversal: ");
preorder(root);
break;

case 6:
printf("\nPostorder traversal: ");
14 | P a g e
postorder(root);
break;

case 7:
printf("\n\tProgram Terminated\n");
return 1;

default:
printf("\n\tInvalid Choice\n");
}

printf("\n\nDo you want to continue? (y/n): ");


scanf(" %c", &user_continue);
}

return 0;
}

// creates a new tree node


struct node *create(int data)
{
struct node *new_node = (struct node *)malloc(sizeof(struct
node));

15 | P a g e
// if a memory error has occurred
if (new_node == NULL)
{
printf("\nMemory can't be allocated\n");
return NULL;
}
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}

// rotates to the left


struct node *rotate_left(struct node *root)
{
struct node *right_child = root->right;
root->right = right_child->left;
right_child->left = root;

// update the heights of the nodes


root->ht = height(root);
right_child->ht = height(right_child);
16 | P a g e
// return the new node after rotation
return right_child;
}

// rotates to the right


struct node *rotate_right(struct node *root)
{
struct node *left_child = root->left;
root->left = left_child->right;
left_child->right = root;

// update the heights of the nodes


root->ht = height(root);
left_child->ht = height(left_child);

// return the new node after rotation


return left_child;
}

// calculates the balance factor of a node


int balance_factor(struct node *root)
{
17 | P a g e
int lh, rh;
if (root == NULL)
return 0;
if (root->left == NULL)
lh = 0;
else
lh = 1 + root->left->ht;
if (root->right == NULL)
rh = 0;
else
rh = 1 + root->right->ht;
return lh - rh;
}

// calculate the height of the node


int height(struct node *root)
{
int lh, rh;
if (root == NULL)
{
return 0;
}
if (root->left == NULL)
18 | P a g e
lh = 0;
else
lh = 1 + root->left->ht;
if (root->right == NULL)
rh = 0;
else
rh = 1 + root->right->ht;

if (lh > rh)


return (lh);
return (rh);
}

// inserts a new node in the AVL tree


struct node *insert(struct node *root, int data)
{
if (root == NULL)
{
struct node *new_node = create(data);
if (new_node == NULL)
{
return NULL;
}
19 | P a g e
root = new_node;
}
else if (data > root->data)
{
// insert the new node to the right
root->right = insert(root->right, data);

// tree is unbalanced, then rotate it


if (balance_factor(root) == -2)
{
if (data > root->right->data)
{
root = rotate_left(root);
}
else
{
root->right = rotate_right(root->right);
root = rotate_left(root);
}
}
}
else
{
20 | P a g e
// insert the new node to the left
root->left = insert(root->left, data);

// tree is unbalanced, then rotate it


if (balance_factor(root) == 2)
{
if (data < root->left->data)
{
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;
}

// deletes a node from the AVL tree


21 | P a g e
struct node *deleteNode(struct node *root, int x)
{
struct node *temp = NULL;

if (root == NULL)
{
return NULL;
}

if (x > root->data)
{
root->right = deleteNode(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);
}
22 | P a g e
}
}
else if (x < root->data)
{
root->left = deleteNode(root->left, x);
if (balance_factor(root) == -2)
{
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;
23 | P a g e
while (temp->left != NULL)
temp = temp->left;

root->data = temp->data;
root->right = deleteNode(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);
}
}
24 | P a g e
root->ht = height(root);
return (root);
}

// search a node in the AVL tree


struct node *search(struct node *root, int key)
{
if (root == NULL)
{
return NULL;
}

if (root->data == key)
{
return root;
}

if (key > root->data)


{
return search(root->right, key);
}
else
{
25 | P a g e
return search(root->left, key);
}
}

// inorder traversal of the tree


void inorder(struct node *root)
{
if (root == NULL)
{
return;
}

inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

// preorder traversal of the tree


void preorder(struct node *root)
{
if (root == NULL)
{
return;
26 | P a g e
}

printf("%d ", root->data);


preorder(root->left);
preorder(root->right);
}

// postorder traversal of the tree


void postorder(struct node *root)
{
if (root == NULL)
{
return;
}

postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}

27 | P a g e
INTEGRATION AND SYSTEM TESTING

OUTPUTS
Screen Shots:

28 | P a g e
29 | P a g e
30 | P a g e
CONCLUSION:-

In conclusion, AVL trees offer a valuable trade-off. They sacrifice some simplicity

compared to regular binary search trees but gain guaranteed logarithmic time

complexity for search, insertion, and deletion. This makes them ideal for scenarios

where frequent insertions, deletions, and searches are crucial for maintaining

efficient data access.

Here are some additional points to consider:

 AVL trees require storing an extra value (balance factor) for each node

compared to regular binary search trees.

 Implementing AVL tree operations can be more complex than those of binary

search trees due to the rotations involved.

31 | P a g e

You might also like