0% found this document useful (0 votes)
48 views29 pages

Avl Project

The document is a project-based lab report on AVL trees submitted by SAILAJA SRAVANI at KL University, detailing the implementation and operations of AVL trees in Java. It includes sections on the introduction, advantages and disadvantages, system requirements, implementation code, and an algorithm for AVL tree operations. The report concludes that AVL trees are efficient for lookup-intensive applications compared to other tree structures.

Uploaded by

Sailaja Sravani
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)
48 views29 pages

Avl Project

The document is a project-based lab report on AVL trees submitted by SAILAJA SRAVANI at KL University, detailing the implementation and operations of AVL trees in Java. It includes sections on the introduction, advantages and disadvantages, system requirements, implementation code, and an algorithm for AVL tree operations. The report concludes that AVL trees are efficient for lookup-intensive applications compared to other tree structures.

Uploaded by

Sailaja Sravani
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/ 29

K L UNIVERSITY

FRESHMAN ENGINEERING DEPARTMENT

A Project Based Lab Report

On

AVL TREE

SUBMITTED BY:

I.D NUMBER NAME

2200031778 SAILAJA SRAVANI

UNDER THE ESTEEMED GUIDANCE OF

P.SAI SUDHA

ASSISTANT PROFESSOR-BES1

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

1
DEPARTMENT OF BASIC ENGINEERING SCIENCES

CERTIFICATE

This is to certify that the project based laboratory report entitled


“AVL TREE” submitted by Ms.SAILAJA SRAVANI bearing Regd. No.
2200031778 to the Department of Basic Engineering Sciences, KL
University in partial fulfillment of the requirements for the completion of a
project in “Data Structures - 22SC1202” 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 2022-23.

PROJECT SUPERVISOR HEAD OF THE DEPARTMENT

P.SAI SUDHA Dr. D.HARITHA

2
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 Director Dr. A. Jagdeesh for his
administration towards our academic growth.

I express sincere gratitude to our Coordinator and HOD-BES1 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
for providing us the efficient faculty and facilities to make our ideas into reality.

I express my sincere thanks to our project supervisor P.Sai Sudha for 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.

Name:SAILAJA SRAVANI
Regd. No: 2200031778

3
ABSTRACT
AVL tree implements the Map abstract data type just like a regular binary
search tree, the only difference is in how the tree performs. To implement our
AVL tree we need to keep track of a balance factor for each node in the tree.
We do this by looking at the heights of the left and right subtrees for each
node.

An AVL tree is a self-balancing binary search tree that maintains


the balance of its height by performing rotations and rebalancing operations
on nodes when new elements are inserted or existing ones are deleted. The
AVL tree was developed by Adelson-Velskii and Landis and is designed to
provide efficient access, insertion, and deletion of elements. Its self-balancing
nature ensures that the heights of the left and right subtrees of each node are
kept balanced, preventing any one subtree from becoming too deep and
ensuring optimal performance. The AVL tree is widely used in computer
science and is a popular choice for implementing associative arrays, sets, and
maps. It is a valuable data structure for a wide range of applications due to its
efficient operations and balanced nature

4
INDEX
S.NO TITLE PAGE NO

1 Introduction 1-8

2 Aim of the Project 9

2.1 Advantages & Disadvantages 9

2.2 Future Implementation 9

3 Software & Hardware Details 10

4 Data Flow Diagram 11-17

5 Implementation 18-24

6 Algorithm for each module 25-26

7 Outputs 27-28

8 Conclusion 29

5
INTRODUCTION
The AVL trees, also called "Height Balanced Trees" were first introduced by
two Russians named Adelson-Velskii and Landis. AVL trees are maintained in
such a way that the trees always remain within one level of being perfectly
balanced. ... The BALANCE FACTOR of a node T in a binary tree bf(T) = h-L - h-
R.

AVL tree is a self-balanced binary search tree. That means, an AVL tree is 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 for every node, height of its children differ by at most one. In an
AVL tree, every node maintains an extra information known as balance factor
to take care of the self-balancing nature of the tree

OPERATIONS ON AVL:

Due to the fact that, AVL tree is also a binary search tree therefore, all the
operations are performed in the same way as they are performed in a binary
search tree. Searching and traversing do not lead to the violation in property
of AVL tree. However, insertion and deletion are the operations which can
violate this property and therefore, they need to be revisited.

AVL Rotations:

We perform rotation in AVL tree only in case if Balance Factor is other than -
1, 0, and 1. There are basically four types of rotations which are as follows:

1. L L rotation: Inserted node is in the left subtree of left subtree of A


2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A

Where node A is the node whose balance Factor is other than -1, 0, 1.

6
The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations. For a tree to be unbalanced,
minimum height must be at least 2, Let us understand each rotation.

1. RR Rotation

When BST becomes unbalanced, due to a node is inserted into the right
subtree of the right subtree of A, then we perform RR rotation, RR rotation is
an anticlockwise rotation, which is applied on the edge below a node having
balance factor -2

In above example, node A has balance factor -2 because a node C is inserted


in the right subtree of A right subtree. We perform the RR rotation on the edge
below A.

2. LL Rotation

When BST becomes unbalanced, due to a node is inserted into the left subtree
of the left subtree of C, then we perform LL rotation, LL rotation is clockwise
rotation, which is applied on the edge below a node having balance factor 2.

7
In above example, node C has balance factor 2 because a node A is inserted in
the left subtree of C left subtree. We perform the LL rotation on the edge
below A.

3. LR Rotation

Double rotations are bit tougher than single rotation which has already
explained above. LR rotation = RR rotation + LL rotation, i.e., first RR rotation
is performed on subtree and then LL rotation is performed on full tree, by full
tree we mean the first node from the path of inserted node whose balance
factor is other than -1, 0, or 1.

4. RL Rotation

As already discussed, that double rotations are bit tougher than single rotation
which has already explained above. R L rotation = LL rotation + RR rotation,
i.e., first LL rotation is performed on subtree and then RR rotation is performed
on full tree, by full tree we mean the first node from the path of inserted node
whose balance factor is other than -1, 0, or 1.

8
AIM
To Implement AVL tree and its operations using java

Advantages:-

1. Since AVL trees are height balance trees, operations like insertion and
deletion have low time complexity. Let us consider an example:

2. To insert a node with a key Q in the binary tree, the algorithm requires seven
comparisons, but if you insert the same key in AVL tree, from the above 1st
figure, you can see that the algorithm will require three comparisons.

Disadvantages:-

1. Slow Inserts and Deletes


The largest disadvantage to using an AVL tree is the fact that in the
event that it is slightly unbalanced it can severely impact the amount of
time that it takes to perform inserts and deletes.
2. Fast Updating Systems
In the event that you’re working on a system that has the tendency to
update quickly then it is advisable to avoid AVL trees.

Future Enhancements:

AVL tree (height-balanced tree) A binary search tree such that for each
node the heights of the left and right subtrees differ by at most one. During
insertion or deletion, a node in an AVL tree may become critical or unbalanced
and then the tree has to be reorganized to maintain its balanced property

9
SYSTEM REQUIREMENTS

➢ SOFTWARE REQUIREMENTS:
The major software requirements of the project are as follows:
Language : C
Operating system: Windows XP or later.
Tools : DEV C++

➢ HARDWARE REQUIREMENTS:
The hardware requirements that map towards the software are as

follows:

RAM : 8GB or More

Processor : Intel i5 8th gen and higher (or)Ryzen 5 or higher

10
DATA FLOW DIAGRAM

11
12
13
14
15
16
17
18
19
IMPLEMENTATION
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_WORD_LENGTH 50

typedef struct node {

char* word;

int count;

struct node* left;

struct node* right;

int height;

} node;

int max(int a, int b) {

return (a > b) ? a : b;

int height(node* n) {

if (n == NULL) {

return 0;

return n->height;

int get_balance(node* n) {

if (n == NULL) {

20
return 0;

return height(n->left) - height(n->right);

node* create_node(char* word) {

node* n = (node*)malloc(sizeof(node));

n->word = (char*)malloc(sizeof(char) * (strlen(word) + 1));

strcpy(n->word, word);

n->count = 1;

n->left = NULL;

n->right = NULL;

n->height = 1;

return n;

node* right_rotate(node* y) {

node* x = y->left;

node* T2 = x->right;

x->right = y;

y->left = T2;

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

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

return x;

node* left_rotate(node* x) {

21
node* y = x->right;

node* T2 = y->left;

y->left = x;

x->right = T2;

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

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

return y;

node* insert_node(node* root, char* word) {

if (root == NULL) {

return create_node(word);

if (strcmp(word, root->word) < 0) {

root->left = insert_node(root->left, word);

else if (strcmp(word, root->word) > 0) {

root->right = insert_node(root->right, word);

else {

root->count++;

return root;

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

int balance = get_balance(root);

22
if (balance > 1 && strcmp(word, root->left->word) < 0) {

return right_rotate(root);

if (balance < -1 && strcmp(word, root->right->word) > 0) {

return left_rotate(root);

if (balance > 1 && strcmp(word, root->left->word) > 0) {

root->left = left_rotate(root->left);

return right_rotate(root);

if (balance < -1 && strcmp(word, root->right->word) < 0) {

root->right = right_rotate(root->right);

return left_rotate(root);

return root;

void print_inorder(node* root) {

if (root != NULL) {

print_inorder(root->left);

printf("%s: %d\n", root->word, root->count);

print_inorder(root->right);

void free_tree(node* root) {

23
if (root != NULL) {

free_tree(root->left);

free_tree(root->right);

free(root->word);

free(root);

int main() {

node* root = NULL;

char word[MAX_WORD_LENGTH];

FILE* fp = fopen("input.txt", "r");

if (fp == NULL) {

printf("Error opening file.\n");

return 1;

while (fscanf(fp, "%s", word) != EOF) {

root = insert_node(root, word);

fclose(fp);

print_inorder(root);

free_tree(root);

return 0;

24
ALGORITHM
STEP 1: START

STEP 2: Define a struct node that represents a node in BST

STEP 3: Define a constant MAX_WORD_LENGTH to set the maximum


length of a word

STEP 4: Define functions max and height that return the maximum value
and height of two nodes respectively.

STEP 5: Define function get_balance that returns the difference in height


between the left and right subtrees of a node.

STEP 6: Define function create_node that creates a new node and initializes
its attributes.

STEP 7: Define functions right_rotate and left_rotate that perform right


and left rotations on a node respectively.

STEP 8: Define function insert_node that inserts a new word into the BST,
using the AVL balancing technique.

STEP 8.1: If the root is null, create a new node with the word and return it.

STEP 8.2: If the word is less than the root word, insert it into the left
subtree.

STEP 8.3: If the word is greater than the root word, insert it into the right
subtree.

STEP 8.4: If the word is equal to the root word, increment the count of the
node and return it.

STEP 8.5: Update the height of the root and get the balance factor.

STEP 8.6: If the balance factor is greater than 1 and the word is less than
the root's left child word, perform a right rotation.

25
STEP 8.7: If the balance factor is less than -1 and the word is greater than
the root's right child word, perform a left rotation.

STEP 8.8: If the balance factor is greater than 1 and the word is greater
than the root's left child word, perform a left rotation on the left child and
then a right rotation on the root.

STEP 8.9: If the balance factor is less than -1 and the word is less than the
root's right child word, perform a right rotation on the right child and then
a left rotation on the root.

STEP 8.10: Return the root.

STEP 9: Define function print_inorder that traverses the BST in order and
prints the word and its count.

STEP 10: Define function free_tree that frees the memory allocated to the
nodes of the BST.

STEP 11: In the main function:

STEP 11.1: Initialize root to null.

STEP 11.2: Open the input file for reading.

STEP 11.3: If the file could not be opened, print an error message and exit.

STEP 11.4: Read each word from the file and insert it into the BST using
insert_node.

STEP 11.5: Close the input file.

STEP 11.6: Print the BST in order using print_inorder.

STEP 11.7: Free the memory allocated to the BST using free_tree.

STEP 11.8: Return 0.

STEP 12: STOP

26
OUTPUTS

27
28
CONCLUSION
AVL trees are often compared with red–black trees
because both support the same set of operations and take 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 that is, sibling nodes can have hugely differing numbers
of descendants.

The algorithm for intersection or difference is similar, but requires the Join2
helper routine that is the same as Join but without the middle key. Based on
the new functions for union, intersection or difference, either one key or
multiple keys can be inserted to or deleted from the AVL tree. Since Split calls
Join but does not deal with the balancing criteria of AVL trees directly, such an
implementation is usually called the "join-based" implementation.

29

You might also like