0% found this document useful (0 votes)
5 views7 pages

DSA10

AVL tree insertion

Uploaded by

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

DSA10

AVL tree insertion

Uploaded by

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

EX NO 10

IMPLEMENTATION OF AVL TREE


25/09/2024

AIM :-
To implement a student enrollment system for a university where students can register using
their IDs, managed by an AVL tree.
TASK :-
1. Construct the AVL tree based on the following student IDs:
o Student A: 2023001
o Student B: 2023005
○ Student C: 2023003
○ Student D: 2023002
○ Student E: 2023008
○ Student F: 2023004
○ Student G: 2023007
○ Student H: 2023006
○ Student I: 2023009
○ Student J: 2023010
2. Insert the ID’s into the AVL tree one by one in the following order:
3. Ensure that the AVL tree remains balanced after each insertion. The balance factor of
every node must be either -1, 0, or +1. Use AVL tree rotations (left, right, left-right, and
right-left) as needed to maintain balance.
4. After the tree is constructed, perform an in-order traversal of the AVL tree and return
the students based on student id in sorted order.
ALGORITHM :-
Step 1: START

Step 2: Define print_tree Function


- If rt is not NULL Then
- Call print_tree(rt->right, count + 1)
- Print spaces based on count
- Print "ID <student_id>"
- Call print_tree(rt->left, count + 1)
- End If

Step 3: Define clock_rotate Function


- Set ptr1->left as ptr2->right
- Set ptr2->right as ptr1
- Set ptr1->balance to 0
- Set ptr2->balance to 0
- Return ptr2

Step 4: Define anti_rotate Function


- Set ptr1->right as ptr2->left
- Set ptr2->left as ptr1
- Set ptr1->balance to 0
- Set ptr2->balance to 0
- Return ptr2
Step 5: Define insert Function
- If root is NULL Then
- Create a new node and assign dt to newNode->id
- Set newNode->left and newNode->right as NULL
- Set newNode->balance to 0
- Set *ht_in to 1 (height increased)
- Return the new node
- Else If dt < root->id Then
- Recursively call insert on root->left
- If *ht_in == 1 Then
- Switch root->balance:
- Case -1:
- Set root->balance to 0, *ht_in to 0
- Case 0:
- Set root->balance to 1
- Case 1:
- Perform LL or LR rotation based on the balance of the left child
- Set *ht_in to 0
- Else If dt > root->id Then
- Recursively call insert on root->right
- If *ht_in == 1 Then
- Switch root->balance:
- Case 1:
- Set root->balance to 0, *ht_in to 0
- Case 0:
- Set root->balance to -1
- Case -1:
- Perform RR or RL rotation based on the balance of the right child
- Set *ht_in to 0
- Return root

Step 6: Define inorder Function


- If root is not NULL Then
- Call inorder(root->left)
- Print "ID <student_id>"
- Call inorder(root->right)
- End If

Step 7: Main Function


- Initialize root as NULL and choice as 1
- While choice is not 0 Do
- Input student ID (dt)
- Call insert(root, dt, &ht_in)
- Call print_tree(root, 0)
- Input choice to continue or stop
- End While
- Print "Final AVL Tree in In-order Traversal"
- Call inorder(root)
- END
PROGRAM :-
#include<stdio.h>
#include<stdlib.h>

struct student {
int id;
struct student *left;
struct student *right;
int balance;
};

void print_tree(struct student *rt, int count) {


if(rt != NULL) {
print_tree(rt->right, count + 1);
printf("\n");
for(int i = 0; i < count; i++) {
printf(" ");
}
printf("ID %d <", rt->id);
print_tree(rt->left, count + 1);
}
}

struct student* clock_rotate(struct student *ptr1, struct student *ptr2) {


ptr1->left = ptr2->right;
ptr2->right = ptr1;
ptr1->balance = 0;
ptr2->balance = 0;
return ptr2;
}

struct student* anti_rotate(struct student *ptr1, struct student *ptr2) {


ptr1->right = ptr2->left;
ptr2->left = ptr1;
ptr1->balance = 0;
ptr2->balance = 0;
return ptr2;
}

struct student* insert(struct student* root, int dt, int *ht_in) {


struct student *ptr, *ptr2;
if(root == NULL) {
struct student *newNode = (struct student*)malloc(sizeof(struct student));
newNode->id = dt;
newNode->left = NULL;
newNode->right = NULL;
newNode->balance = 0;
*ht_in = 1;
return newNode;
} else if(dt < root->id) {
root->left = insert(root->left, dt, ht_in);
if(*ht_in == 1) {
switch(root->balance) {
case -1:
root->balance = 0;
*ht_in = 0;
break;
case 0:
root->balance = 1;
break;
case 1:
ptr = root->left;
if(ptr->balance == 1) {
printf("\n\tLL Rotation\t\n");
root = clock_rotate(root, ptr);
} else {
printf("\n\tLR Rotation\t\n");
ptr2 = ptr->right;
root->left = anti_rotate(ptr, ptr2);
root = clock_rotate(root, root->left);
}
*ht_in = 0;
}
}
} else if(dt > root->id) {
root->right = insert(root->right, dt, ht_in);
if(*ht_in == 1) {
switch(root->balance) {
case 1:
root->balance = 0;
*ht_in = 0;
break;
case 0:
root->balance = -1;
break;
case -1:
ptr = root->right;
if(ptr->balance == -1) {
printf("\n\tRR Rotation\t\n");
root = anti_rotate(root, ptr);
} else {
printf("\n\tRL Rotation\t\n");
ptr2 = ptr->left;
root->right = clock_rotate(ptr, ptr2);
root = anti_rotate(root, root->right);
}
*ht_in = 0;
}
}
}
return root;
}
void inorder(struct student *root) {
if(root != NULL) {
inorder(root->left);
printf("ID %d ", root->id);
inorder(root->right);
}
}

int main() {
struct student *root = NULL;
int choice = 1, ht_in, dt;

while(choice) {
printf("Enter student ID: ");
scanf("%d", &dt);
root = insert(root, dt, &ht_in);
print_tree(root, 0);
printf("\nEnter 0 to end: ");
scanf("%d", &choice);
}

printf("\nFinal AVL Tree in In-order Traversal:\n");


inorder(root);
printf("\n");

return 0;
}
SAMPLE OUTPUT :-
RESULT :-
Thus, the implementation of the student enrollment system using an AVL tree was
successfully completed and executed.

You might also like