0% found this document useful (0 votes)
18 views23 pages

Avl Tree Project-Ds GRP Report

Uploaded by

lowkhyanandini34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

Avl Tree Project-Ds GRP Report

Uploaded by

lowkhyanandini34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

K L UNIVERSITY

FRESHMAN ENGINEERING DEPARTMENT


A Project Based Lab Report

On

AVL TREE

SUBMITTED BY:

I.D NUMBER NAME

2300032206 VALETI PUJASRI

2300032132 YAKKALA SRI PUJITHA

2300032210 PALAPARTHI HARSHINI

2300032245 KUDIPUDI PAVAN VENKATA KISHORE

UNDER THE ESTEEMED GUIDANCE OF

MR.BVNV .KIRSHNA SURESH

ASSISTANT PROFESSOR

KL UNIVERSITY
Green fields, Vaddeswaram – 522 502
Guntur Dt., AP, India.

1|P a ge
DEPARTMENT OF BASIC ENGINEERING SCIENCES

CERTIFICATE

This is to certify that the project based laboratory report entitled


“AVL TREE” submitted by Mr./Ms. VALETI PUJASRI ,YAKKALA SRI
PUJITHA , PALAPARTHI HARSHINI ,KUDIPUDI PAVAN VENKATA
KISHORE bearing Regd. No. 2300032206 ,2300032132 ,2300032210 ,2300032245 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 him/her under my supervision during the academic year 2023–
2024.

PROJECT SUPERVISOR HEAD OF THE DEPARTMENT

Mr.BVNV KRISHNA SURESH DR.D HARITHA

2|P a ge
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. JAGADEESH 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 Mr.BVNV KRISHNA


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

I.D NUMBER NAME

2300032206 VALETI PUJASRI

2300032132 YAKKALA SRI PUJITHA

2300032210 PALAPARTHI HARSHINI

2300032245 KUDIPUDI PAVAN VENKATA

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

S.NO TITLE PAGE NO


1. Introduction 6

2. Aim of the Project 7


2.1 Advantages & Disadvantages 8
2.2 Future Implementation
3. Software & Hardware Details 9

4. Data Flow Diagram 10

5. Implementation 12-26

6. Algorithm for each module 11

7. Integration and System Testing 27-28

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:

The code provides various operations to manipulate the AVL tree:

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.

Traversal functions: Includes inorder, preorder, and postorder functions to perform


different types of tree traversals.

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.

Disadvantages:- Complexity: Implementing and maintaining AVL trees involves


more complex logic compared to simpler data structures like binary search trees. The
need for balancing operations adds complexity to insertion and deletion algorithms.

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:

Implement concurrency support to allow multiple threads or processes to access and


manipulate the AVL tree concurrently. This could involve using synchronization
primitives such as locks or exploring lock-free data structures for thread-safe
operations.

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:

Explore memory-efficient data structures and techniques to reduce the memory


overhead of AVL trees. This could involve implementing compressed or sparse
representations for tree nodes, especially in scenarios with a large number of nodes.

Dynamic Rebalancing Strategies:

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.

Persistent AVL Trees:

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

Processor : The processor's speed and architecture influence the performance


of AVL tree operations, especially for intensive tasks such as insertion, deletion, and
rebalancing.

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.

Function Prototypes: Declares function prototypes for creating a node, inserting,


deleting, searching, rotating left and right, calculating balance factor and height, and
traversing the tree in inorder, preorder, and postorder.

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:

create: Creates a new node with the given data.

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.

inorder, preorder, and postorder: Perform corresponding tree traversals.

11 | 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* 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;

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


{
printf("\n\n------- AVL TREE --------\n");
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);
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");
}

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


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

// 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);

// return the new node after rotation


return right_child;
}

// rotates to the right


struct node* rotate_right(struct node* root)

14 | P a g e
{
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)
{
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)
lh = 0;
else
lh = 1 + root->left->ht;
if (root->right == NULL)
rh = 0;
else
rh = 1 + root->right->ht;

15 | P a g e
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;
}
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
{
// 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)

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

// deletes a node from the AVL tree


struct node * delete(struct node *root, int x)
{
struct node * temp = NULL;

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

// search a node in the AVL tree


struct node* search(struct node* root, int key)
{

18 | P a g e
if (root == NULL)
{
return NULL;
}

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

if(key > root->data)


{
search(root->right, key);
}
else
{
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;
}

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


preorder(root->left);
preorder(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

You might also like