0% found this document useful (0 votes)
3 views4 pages

AVLEXP

The document contains a C program that implements an AVL tree, allowing for insertion, deletion, and inorder traversal of nodes. It reads integer data from a specified file, inserts it into the AVL tree, and provides options for displaying the inorder traversal or deleting a node. The program also includes balancing functions to maintain the AVL tree properties after insertions and deletions.

Uploaded by

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

AVLEXP

The document contains a C program that implements an AVL tree, allowing for insertion, deletion, and inorder traversal of nodes. It reads integer data from a specified file, inserts it into the AVL tree, and provides options for displaying the inorder traversal or deleting a node. The program also includes balancing functions to maintain the AVL tree properties after insertions and deletions.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

typedef struct avl {


int data;
struct avl *left;
struct avl *right;
} node;

node* insert(node*, int);


node* delete(node*, int);
node* LL(node*);
node* RR(node*);
node* LR(node*);
node* RL(node*);
node* balance(node*);
int height(node*);
int factor(node*);
void inorder(node*);

FILE *fp;
char file[30];

void main() {
int n, x;
char str[30];
node *root = NULL;
char ch, ch1;

clrscr(); // Only works in Turbo C/DOSBox

printf("\nEnter file name with extension to read: ");


scanf("%s", file);

fp = fopen(file, "r");
while (fscanf(fp, "%s", str) != EOF) {
printf("%s", str);
root=insert(root,atoi(str));
}
fclose(fp);

printf("\nData inserted from the file into the tree");

do {
printf("\nSelect the Options:");
printf("\n1. Inorder");
printf("\n2. Delete");
printf("\n3. Exit:\n");
scanf("%d", &n);

switch (n) {
case 1:
printf("\nDo you want to store inorder data into file? (y/n): ");
scanf(" %c", &ch1);
if (ch1 == 'y') {
printf("\nEnter file name to store inorder info: ");
scanf("%s", file);
fp = fopen(file, "w");
inorder(root);
fclose(fp);
} else {
printf("\nOk....");
inorder(root);
}
break;

case 2:
printf("\nEnter an element to delete: ");
scanf("%d", &x);
root = delete(root, x);
printf("\nInorder traversal after deletion:\n");
inorder(root);
break;

case 3:
exit(0);
break;

default:
printf("\nWrong choice.");
break;
}

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


ch = getch();
} while (ch == 'y');

getch();
}

node *insert(node *root, int x) {


if (root == NULL) {
root = (node*) malloc(sizeof(node));
root->data = x;
root->left = NULL;
root->right = NULL;
return root;
} else if (x < root->data) {
root->left = insert(root->left, x);
root = balance(root);
} else if (x > root->data) {
root->right = insert(root->right, x);
root = balance(root);
}
return root;
}

node* balance(node *root) {


int k = factor(root);
if (k == 2) {
if (factor(root->left) == 1)
root = LL(root);
else
root = LR(root);
} else if (k == -2) {
if (factor(root->right) == -1)
root = RR(root);
else
root = RL(root);
}
return root;
}

node* LL(node *root) {


node *t = root->left;
root->left = t->right;
t->right = root;
return t;
}

node* RR(node *root) {


node *t = root->right;
root->right = t->left;
t->left = root;
return t;
}

node* LR(node *root) {


node *t = root->left;
root->left = RR(t);
return LL(root);
}

node* RL(node *root) {


node *t = root->right;
root->right = LL(t);
return RR(root);
}

int height(node *root) {


int h = -1, m, l, r;
if (root != NULL) {
l = height(root->left);
r = height(root->right);
m = (l > r) ? l : r;
h = m + 1;
}
return h;
}

int factor(node *root) {


int l = height(root->left);
int r = height(root->right);
return l - r;
}

node* delete(node *root, int x) {


node *p;
if (root == NULL) {
return NULL;
}
if (x < root->data) {
root->left = delete(root->left, x);
} else if (x > root->data) {
root->right = delete(root->right, x);
} else {
if (root->left == NULL) {
node *temp = root->right;
free(root);
return balance(temp);
} else if (root->right == NULL) {
node *temp = root->left;
free(root);
return balance(temp);
} else {
p = root->right;
while (p->left != NULL) {
p = p->left;
}
root->data = p->data;
root->right = delete(root->right, p->data);
}
}
return balance(root);
}

void inorder(node *root) {


if (root != NULL) {
inorder(root->left);
printf("\n%3d ", root->data);
if (fp) {
fprintf(fp, "\n%3d", root->data);
}
printf(" b%3d", factor(root));
if (fp) {
fprintf(fp, " b%3d", factor(root));
}
printf(" h%3d", height(root));
if (fp) {
fprintf(fp, " h%3d", height(root));
}
inorder(root->right);
}
}

You might also like