0% found this document useful (0 votes)
421 views

Implementation of Singly Linked List

The document describes a C program to convert an infix expression to a postfix expression. The algorithm involves pushing operators onto a stack based on their priority and popping operators to apply them when encountering operands or parentheses. The program defines functions to push and pop from the stack, check priorities, and convert the infix expression to postfix while iterating through the infix string.

Uploaded by

Deelip Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
421 views

Implementation of Singly Linked List

The document describes a C program to convert an infix expression to a postfix expression. The algorithm involves pushing operators onto a stack based on their priority and popping operators to apply them when encountering operands or parentheses. The program defines functions to push and pop from the stack, check priorities, and convert the infix expression to postfix while iterating through the infix string.

Uploaded by

Deelip Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 57

IMPLEMENTATION OF SINGLY LINKED LIST

AIM:

To write a c program to implement singly linked list

ALGORITHM:

Step 1: Create a list of element one linked to the other using create function

Step 2: To insert any new element to the list, we have to get the position

where to insert it and do the insertion using insert function

Step 3: After create the list if we have to delete any element we can do it by

getting the position to be deleted

Step 4: Display the contents using display function

Step 5: Exit the program


PROGRAM CODING:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

int i;

struct node;

typedef struct node *list;

typedef struct node *pos;

struct node

int e;

pos next;

};

void deletelist(list L)

pos p,tmp;

p=L->next;

L->next=NULL;

while(p!=NULL)

tmp=p->next;

free(p);

p=tmp;

}
}

list makeempty(list L)

if(L!=NULL)

deletelist(L);

L=malloc(sizeof(struct node));

L->next=NULL;

return L;

int isempty(list L)

return L->next==NULL;

int islast(pos p)

return p->next==NULL;

pos find(int x,list L)

pos p;

p=L->next;

while(p!=NULL&&p->e!=x)

p=p->next;

return p;
}

void insert(int x,pos p)

pos tmpcell;

tmpcell=malloc(sizeof(struct node));

tmpcell->e=x;

tmpcell->next=p->next;

p->next=tmpcell;

pos header(list L)

{return L;

pos advance(pos p)

{return p->next;

int retrieve(pos p)

{return p->e;

void printlist(const list L)

{pos p=header(L);

do

p=advance(p);

printf("%d",retrieve(p));
}while(!islast(p));

printf("\n");

int main()

{list L;

pos p;

int i;

clrscr();

L=makeempty(L);

p=header(L);

for(i=0;i<10;i++)

insert(i,p);

printlist(L);

p=advance(p);

printf("Enter the number you wish to find");

scanf("%d" ,&i);

if(find(i,L)!=NULL)

printf("Element found");

else

printf("Element not found");

p=header(L);

deletelist(L);
if(isempty(L))

printf("\nDeletion successfully completed");

getch();

return 0;

OUTPUT:

01

012

0123

01234

012345

0123456

01234567

012345678

0123456789

Enter the number you wish to find:2

Element found

Deletion successfully completed

RESULT:

Thus the c program for implementation of linked list was executed and the

output was verified.


Ex.No:

Date:

IMPLEMENTATION OF DOUBLY LINKED LIST

AIM:

To write a c program to implement doubly linked list

ALGORITHM:

Step 1: Create a list of element one linked to the other using create function

Step 2: To insert any new element to the list, we have to get the position

where to insert it and do the insertion using insert function

Step 3: After create the list if we have to delete any element we can do it by

getting the position to be deleted

Step 4: Display the contents using display function

Step 5: Exit the program


PROGRAM CODING:

#include<stdio.h>

#include<stdlib.h>

typedef struct node *ptrtonode;

typedef ptrtonode list;

typedef ptrtonode position;

typedef int elementtype;

struct node

elementtype element;

position Next,Prev;

};

list

makeempty(list L)

if(L!=NULL)

deletelist(L);

L=malloc(sizeof(struct node));

if(L==NULL)

printf("Out of Memory!");

L->Next=NULL;

L->Prev=NULL;

return L;

}
int isempty(list L)

return (L->Next==NULL && L->Prev==NULL);

int islast(position P,list L)

return P->Next==NULL;

position find(elementtype X,list L)

position P;

P=L->Next;

while(P!=NULL && P->element!=X)

P=P->Next;

return P;

void

delete(elementtype X,list L)

position P;

P=find(X,L);

P->Next->Prev=P->Prev;

P->Prev->Next=P->Next;

free(P);
}

void

insert(elementtype X,list L,position P)

position tempcell;

tempcell=malloc(sizeof(struct node));

if(tempcell==NULL)

printf("Out of space!!!");

tempcell->element=X;

tempcell->Next=P->Next;

tempcell->Prev=P;

P->Next->Prev=tempcell;

P->Next=tempcell;

int deletelist(list L)

position P,tmp;

P=L->Next;

L->Next=NULL;

L->Prev=NULL;

while(P!=NULL)

tmp=P->Next;

free(P);
P=tmp;

return 0;

position

header(list L)

return L;

position

first(list L)

return L->Next;

position

advance(position P)

return P->Next;

elementtype retrieve(position P)

return P->element;

}
void

printlist(const list L)

position P=header(L);

if(isempty(L))

printf("Empty List\n");

else

do

P=advance(P);

printf("%d",retrieve(P));

while(!islast(P,L));

printf("\n");

main()

list L;

position P;

int i;

clrscr();

L=makeempty(NULL);
P=header(L);

printlist(L);

for(i=0;i<10;i++)

insert(i,L,P);

printlist(L);

P= advance(P);

for(i=0;i<10;i+=2)

delete(i,L);

printf("Finished deletion\n");

printlist(L);

printf("Enter the Number you want to find:\t");

scanf("%d",&i);

if(find(i,L)!=NULL)

printf("Element found");

else

printf("element not found");

deletelist(L);

getch();

return 0;

}
OUTPUT:

Empty list

01

012

0123

01234

012345

0123456

01234567

012345678

0123456789

Finished Deletion

13579

Enter the number you want to find:1

Element found

RESULT:

Thus the c program for implementation of linked list was executed and the

output was verified.


Ex.No:

Date:

IMPLEMENTATION OF POLYNOMIAL ADDITION

AIM:

To write a c program to illustrate polynomial addition

ALGORITHM:

STEP 1: Create a function. Get the input from user of the coefficients and
the power and create node for it

STEP 2: Continue step1 until the user press a key other than y or Y

Step 3: Create a function show to display the details

STEP 4: Create a function named poly. Check for the greater number
between numbers having the same number.

STEP 5: Print the details

STEP 6: Exit the program


PROGRAM CODING:

#include<stdio.h>

#include<malloc.h>

#include<conio.h>

struct link

int coeff,pow;

struct link *next;

};

struct link *poly1=NULL,*poly=NULL,*poly2=NULL;

void creat(struct link *node)

char ch;

do

printf("\n Enter Coef:\t") ;

scanf("%d",&node->coeff);

printf(" Enter Power :\t ");

scanf("%d",&node->pow);

node->next=(struct link *)malloc(sizeof(struct link)) ;

node=node->next;

node->next=NULL;

printf("\n\t\t Continue (y/n)\n\n");

ch=getch();
}

while(ch=='y'||ch=='Y');

void show(struct link *node)

while(node->next!=NULL)

printf(" %dx^%d",node->coeff,node->pow);

node=node->next;

if(node->next!=NULL)

printf(" +");

void polyadd(struct link *poly1,struct link *poly2,struct link *poly)

while((poly1 ->next) && (poly2->next))

if(poly1->pow > poly2->pow)

poly->pow=poly1->pow;

poly->coeff=poly1->coeff;

poly1=poly1->next;

else if(poly1->pow < poly2->pow)


{

poly->pow=poly2->pow;

poly->coeff=poly2->coeff;

poly2=poly2->next;

else

poly->pow=poly1->pow;

poly->coeff=(poly1->coeff)+(poly2->coeff);

poly1=poly1->next;

poly2=poly2->next;

poly->next=(struct link *)malloc(sizeof(struct link));

poly=poly->next;

poly->next=NULL;

while(poly1->next||poly2->next)

if(poly1->next)

poly->pow=poly1->pow;

poly->coeff=poly1->coeff;

poly1=poly1->next;

}
if(poly2->next)

poly->pow=poly2->pow;

poly->coeff=poly2->coeff;

poly2=poly2->next;

poly->next=(struct link *)malloc(sizeof(struct link));

poly=poly->next;

poly->next=NULL;

void main()

char ch;

clrscr();

do

poly1=(struct link*)malloc(sizeof(struct link));

poly2=(struct link*)malloc(sizeof(struct link));

poly=(struct link*)malloc(sizeof(struct link));

printf("\n\t\t\t\t Enter 1st No.\n\n");

creat(poly1);

printf("\n\t\t\t\tEnter 2nd No.\n\n");

creat(poly2);
printf("\n 1st No.");

show(poly1);

printf("\n 2nd No.");

show(poly2);

polyadd(poly1,poly2,poly);

printf("\n\n\t Added Polynomial: \n f(x)=");

show(poly);

printf("\n\n\t\t Add more number(y/n)");

ch=getch();

while(ch=='y'||ch=='Y');

OUTPUT

Enter 1st No.

Enter Coef: 2

Enter Power : 3

Continue (y/n)

Enter Coef: 52

Enter Power : 2

Continue (y/n)

Enter 2nd No.

Enter Coef: 52
Enter Power : 3

Continue (y/n)

Enter Coef: 10

Enter Power : 2

Continue (y/n)

1st No. 2x^3 + 52x^2

2nd No. 52x^3 + 10x^2

Added Polynomial:

f(x)= 54x^3 + 62x^2

Add more number(y/n)

RESULT

Thus the program for Addition of polynomial is executed and output is


verified
Ex.No:

Date:

INFIX TO POSTFIX CONVERSION

AIM:

To write a c program to convert infix expression to postfix expression

ALGORITHM:

STEP 1: Start

STEP 2: Check the conditions whether the stack is empty or not to

insert an element

STEP 3: If the stack is not full push the element onto the stack

STEP 4: Repeat the Step 3 until delimiter or end appears. Check for

the priority of operators while pushing

STEP 5: If the element to be pushed has higher priority than the

operator in the stack push the element onto the stack

STEP 6: ELSE pop out the two contents at the top from the top and

apply the operator

STEP 7: Execute the program

STEP 8: Stop
PROGRAM CODING:

#include<stdio.h>
#include<conio.h>
#define operand(x) (x>='a'&&x<='z'||x>='A'&&x<='Z')
void infixtopostfix();
int priority(char);
void push(char);
char pop();
int top=-1;
char infix[20],stack[20],postfix[20];
void main()
{
clrscr();
printf("enter the expression");
scanf("%s",infix);
infixtopostfix();
printf("%s",postfix);
getch();
}
void infixtopostfix()
{
int j,l=0;
char x,y;
for(j=0;(x=infix[j])!='\0';j++)
{
if(operand(x))
postfix[l++]=x;
else if(x=='(')
push(x);
else if(x==')')
{
while((y=pop())!='(')
postfix[l++]=y;
}
else
{
while(priority(stack[top])>=priority(x)&&stack[top]!='(')
postfix[l++]=pop();
push(x);
}
}
while(top>=0)
postfix[l++]=pop();
}
void push(char n)
{
stack[++top]=n;
}

int priority(char x)
{
int y;
y=(x=='('?3:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:-1);
return y;
}
char pop()
{
char n;
n=stack[top];
top--;
return(n);
}

OUTPUT:

Enter the expression a+b-c/d

ab+cd/-

RESULT:

Thus the conversion from infix expression to postfix was executed and the
output was verified.
Ex.No:

Date:

IMPLEMENTATION OF DOUBLE ENDED QUEUE

AIM:

To write a program to implement double ended queue

ALGORITHM:

STEP 1: Create a queue and check whether the queue is empty or not

STEP 2: Write a function to insert element in the rear end

STEP 3: Create a function to insert the element in the front end

STEP 4: Create two functions to delete the element from queue from

both rear and front end

Step 5: Display the result

STEP 6: Execute the program

STEP 7: Stop
PROGRAM CODING:

#include<stdio.h>
#include<conio.h>
#define SIZE 20
typedef struct dq_t
{
int front,rear;
int item[SIZE];
}deque;
void create(deque *);
void display(deque *);
void insert_rear(deque *, int);
void insert_front(deque *, int);
int delete_front(deque *, int);
int delete_rear(deque *, int);
void main()
{
int x,data,ch;
deque DQ;
clrscr();
create(&DQ);
printf(“\n\t\t Program shows working of double ended queue”);
do
{
printf(“\n\t\t Menu”);
printf(“\n\t\t 1: insert at rear end”);
printf(“\n\t\t 2: insert at front end”);
printf(“\n\t\t 3: delete from front end”);
printf(“\n\t\t 4: delete from rear end”);
printf(“\n\t\t 5: exit. “);
printf(“\n\t\t Enter choice :”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
if (DQ.rear >= SIZE)
{
printf(“\n Deque is full at rear end”);
continue;
}
else
{
printf(“\n Enter element to be added at rear end :”);
scanf(“%d”,&data);
insert_rear(&DQ,data);
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 2:

if (DQ.front <=0)
{
printf(“\n Deque is full at front end”);
continue;
}
else
{
printf(“\n Enter element to be added at front end :”);
scanf(“%d”,&data);
insert_front(&DQ,data);
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 3:
x = delete_front(&DQ,data);
if (DQ.front==0)
{
continue;
}
else
{
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 4:
x = delete_rear(&DQ,data);
if (DQ.rear==0)
{
continue;
}
else
{
printf(“\n Elements in a deque are :”);
display(&DQ);
continue;
}
case 5: printf(“\n finish”); return;
}
}while(ch!=5);
getch();
}
void create(deque *DQ)
{
DQ->front=0;
DQ->rear =0;
}
void insert_rear(deque *DQ, int data)
{
if ((DQ->front == 0) &&(DQ->rear == 0))
{
DQ->item[DQ->rear] = data;
DQ->rear = DQ->rear +1;
}
else
{
DQ->item[DQ->rear] = data;
DQ->rear = DQ->rear +1;
}
}
int delete_front(deque *DQ, int data)
{
if ((DQ->front == 0) && (DQ->rear == 0))
{
printf(“\n Underflow”);
return(0);
}
else
{
data = DQ->item[DQ->front];
printf(“\n Element %d is deleted from front :”,data);
DQ->front = DQ->front +1;
}
if (DQ->front==DQ->rear)
{
DQ->front =0;
DQ->rear = 0;
printf(“\n Deque is empty (front end)”);
}
return data;
}
void insert_front(deque *DQ, int data)
{
if(DQ->front > 0)
{
DQ->front = DQ->front-1;
DQ->item[DQ->front] = data;
}
}
int delete_rear(deque *DQ, int data)
{
if (DQ->rear == 0)
{
printf(“\n Underflow”);
return(0);
}
else
{
DQ->rear = DQ->rear -1;
data = DQ->item[DQ->rear];
printf(“\n Element %d is deleted from rear:”,data);
}
if (DQ->front==DQ->rear)
{
DQ->front =0;
DQ->rear = 0;
printf(“\n Deque is empty(rear end)”);

}
return data;
}
void display(deque *DQ)
{
int x;

for(x=DQ->front;xrear;x++)
{
printf(“%d\t”,DQ->item[x]);
}
printf(“\n\n”);
}

OUTPUT:

Program shows working of double ended queue


Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :1
Enter element to be added at rear end :11
Elements in a deque are :11
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :1
Enter element to be added at rear end :22
Elements in a deque are :11 22
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :1
Enter element to be added at rear end :33
Elements in a deque are :11 22 33
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :2
Deque is full at front end
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :3
Element 11 is deleted from front :
Elements in a deque are :22 33
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :2
Element 11 is deleted from front :
Elements in a deque are :22 33
Menu
1: insert at rear end
2: insert at front end
3: delete from front end
4: delete from rear end
5: exit.
Enter choice :5

RESULT:

Thus the program for deque operation was executed and the output

was verified.

Ex.No:
Date:

IMPLEMENTATION OF EXPRESSION TREE

AIM:

To write a ‘C’ program to implement expression tree.

ALGORITHM:

STEP 1: Create a tree and check whether it is empty or not

STEP 2: Then get the expression

STEP 3: Convert the given expression in prefix and postfix form

STEP 4: Display the result

STEP 5: Exit the program

PROGRAM CODING:
#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<ctype.h>

#define size 20

typedef struct node

char data;

struct node *left;

struct node *right;

}btree;

btree *stack[size];

int top;

void main()

btree *root;

char exp[80];

btree *create(char exp[80]);

void inorder(btree *root);

void preorder(btree *root);

void postorder(btree *root);

clrscr();

printf("\nEnter the postfix expression:");

scanf("%s",exp);
top=-1;

root=create(exp);

printf("\nThe tree is created");

printf("\nThe inorder traversal is :\n");

inorder(root);

printf("\nThe preorder traversal is :\n");

preorder(root);

printf("\nThe postorder traversal is :\n");

postorder(root);

getch();

tree *create(char exp[])

btree *temp;

int pos;

char ch;

void push(btree *);

btree *pop();

pos=0;

ch=exp[pos];

while(ch!='\0')

temp=(btree*)malloc(sizeof(btree));

temp->left=temp->right=NULL;
temp->data=ch;

if(isalpha(ch))

push(temp);

else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')

temp->right=pop();

temp->left=pop();

push(temp);

else

printf("Invalid character\n");

pos++;

ch=exp[pos];

temp=pop();

return(temp);

void push(btree *node)

if(top+1>=size)

printf("\nstack is Full");

top++;

stack[top]=node;

}
btree *pop()

btree *node;

if(top==-1)

printf("\nStack is empty");

node=stack[top];

top--;

return(node);

void inorder(btree *root)

btree *temp;

temp=root;

if(temp!=NULL)

inorder(temp->left);

printf("%c",temp->data);

inorder(temp->right);

} }

void preorder(btree *root)

btree *temp;

temp=root;

if(temp!=NULL)
{

printf("%c",temp->data);

preorder(temp->left);

preorder(temp->right);

} }

void postorder(btree *root)

btree *temp;

temp=root;

if(temp!=NULL)

postorder(temp->left);

postorder(temp->right);

printf("%c",temp->data);

} }

OUTPUT:
Enter the postfix expression:ab*cd/+

The tree is created

The inorder traversal is:

a*b+c/d

The preorder traversal is:

+*ab/cd

The postorder traversal is:

ab*cd/+

RESULT:

Thus the implementation of Expression tree is done and the program is


executed.

Ex.No:
Date:

IMPLEMENTATION OF BINARY SEARCH TREE

AIM:
To write a c program to implement binary search tree

ALGORITHM:

Step 1: Start

Step 2: Create a structure named treenode

Step 3: Write a function to find the min element and the maximum element

Step 4: Write a function to insert, delete and to display the elements in

inorder traversal

Step 5: Exit the program

PROGRAM CODING:
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define NULL 0

struct treenode

{ int element;

struct treenode *left;

struct treenode *right;

};

typedef struct treenode *position,*searchtree;

searchtree insert(int x,searchtree t)

if(t==NULL)

t=(struct treenode*)malloc(sizeof(struct treenode));

if(t==NULL)

exit(0);

else

t->element=x;

t->left=t->right=NULL;

else
if(x<t->element)

t->left=insert(x,t->left);

else

if(x>t->element)

t->right=insert(x,t->right);

return t;

position findmin(searchtree t)

if(t==NULL)

return NULL;

else

if(t->left==NULL)

return t;

else

return findmin(t->left);

position findmax(searchtree t)

if(t==NULL)

return NULL;

else

if(t->right==NULL)

return t;
else

return findmax(t->right);

searchtree rem(int x,searchtree t)

position temp;

if(t==NULL)

printf("\nElement not found");

else

if(x<t->element)

t->left=rem(x,t->left);

else

if(x>t->element)

t->right=rem(x,t->right);

else

if(t->left&&t->right)

temp=findmin(t->right);

t->element=temp->element;

t->right=rem(t->element,t->right);

}
else

temp=t;

if(t->left==NULL)

t=t->right;

else

if(t->right==NULL)

t=t->left;

free(temp);

return t;

void intrav(searchtree head)

if(head==NULL)

return;

if(head->left!=NULL)

intrav(head->left);

printf("%d\t",head->element);

if(head->right!=NULL)

intrav(head->right);

}
void main()

int n,i,dat,ch;

searchtree t=NULL;

position node;

clrscr();

printf("Enter no of elements:\n");

scanf("%d",&n);

printf("Enter the elements:\n");

for(i=1;i<=n;i++)

scanf("%d",&dat);

t=insert(dat,t);

intrav(t);

do

printf("\n\n");

printf("\n ****MENU****\n");

printf("\nEnter 1 -> Insert a node\n");

printf("\n 2 -> Delete a node\n");

printf("\n 3 -> Find Minimum\n");

printf("\n 4 -> Find Maximum\n");

printf("\n 5 -> Display(Inorder Traversal\n");


printf("\n 6 -> Exit\n");

printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nEnter the element to be inserted:");

scanf("%d",&dat);

t=insert(dat,t);

break;

case 2:printf("\n Enter the node to be deleted:");

scanf("%d",&dat);

t=rem(dat,t);

break;

case 3:node=findmin(t);

printf("\nThe minimum element is %d",node->element);

break;

case 4:node=findmax(t);

printf("\nThe maximum element is %d",node->element);

break;

case 5:intrav(t);

break;

case 6:exit(0);

}while(ch!=6);
getch();

OUTPUT:

Enter no of elements:

Enter the elements:

45

15

33

2 7 15 33 45

****MENU****

Enter 1 -> Insert a node

2 -> Delete a node

3 -> Find Minimum


4 -> Find Maximum

5 -> Display(Inorder Traversal

6 -> Exit

Enter your choice:5

2 7 15 33 45

****MENU****

Enter 1 -> Insert a node

2 -> Delete a node

3 -> Find Minimum

4 -> Find Maximum

5 -> Display(Inorder Traversal


6 -> Exit

Enter your choice:1

Enter the element to be inserted : 18

****MENU****

Enter 1 -> Insert a node

2 -> Delete a node

3 -> Find Minimum

4 -> Find Maximum

5 -> Display(Inorder Traversal

6 -> Exit

Enter your choice:3

The minimum element is 2


****MENU****

Enter 1 -> Insert a node

2 -> Delete a node

3 -> Find Minimum

4 -> Find Maximum

5 -> Display(Inorder Traversal

6 -> Exit

Enter your choice:2

Enter the node to be deleted:33

****MENU****

Enter 1 -> Insert a node

2 -> Delete a node


3 -> Find Minimum

4 -> Find Maximum

5 -> Display(Inorder Traversal

6 -> Exit

Enter your choice:5

2 7 15 18 45

RESULT:

Thus the implementation of Binary search tree is done and the program
is executed.

Ex.No:
Date:

IMPLEMENTATION OF AVL TREE


Aim:

To Implement Insertion in AVL Trees

Algorithm:

1. Create a class AVl tree and declare all its member variables and functions

2. Get the user option if tge choice is 1, create a binary tree, with the values
and considering the Balance factor

3. The balance factor=no of left child-no of right child

4. If the balance factor is not equal to 1, 0,-1 then we have to rotate the
tree one time left or right or Double rotate

5. If the option is 2, display the tree contents in preorder and postorder


formats

6. If the option is 3, get the element to be searched and display whether the
element is present or Not.

7. If the option is 4, exit the program

Program coding:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
typedef struct node *pos;
typedef struct node *avltree;
avltree t;
struct node
{
int element;
avltree left;
avltree right;
int height;
};
avltree insert(int,avltree);
int max(int,int);
int height(pos p);

pos singlerotatewithleft(pos);
pos singlerotatewithright(pos);
pos doublerotatewithleft(pos);
pos doublerotatewithright(pos);
void display(avltree);

void main()
{
int ch,ele;
clrscr();
t=NULL;
do
{
printf("\nMENU\n");
printf("\n1. Insertion\n");
printf("\n2. Display\n");
printf("\n3. Exit\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the element to be inserted:");
scanf("%d",&ele);
t=insert(ele,t);
printf("\nThe element is inserted");
break;
case 2: printf("\nThe elements in the AVL TREE are:");
display(t);
break;
case 3: exit(0);
break;
}
}while(ch<4);
getch();
}

avltree insert(int x,avltree t)


{
if(t==NULL)
{
t=(node *)malloc(sizeof(struct node));
if(t==NULL)
exit(0);
else
{
t->element=x;t->height=0;
t->left=t->right=NULL;
}
}
else
if(x<t->element)
{
t->left=insert(x,t->left);
if(height(t->left)-height(t->right)==2)
if(x<t->left->element)
t=singlerotatewithleft(t);
else
t=doublerotatewithleft(t);
}
else
if(x>t->element)
{
t->right=insert(x,t->right);
if(height(t->right)-height(t->left)==2)
if(x>t->left->element)
t=singlerotatewithright(t);
else
t=doublerotatewithright(t);
}
t->height=max(height(t->left),height(t->right))+1;
return t;
}

int height(pos p)
{
if(p==NULL)
return -1;
else
return p->height;
}

int max(int p,int q)


{
if(p>q)
return p;
else
return q;
}

pos singlerotatewithleft(pos k2)


{
pos k1;
k1=k2->left;
k2->left=k1->right;
k1->right=k2;
k2->height=max(height(k2->left),height(k2->right))+1;
k1->height=max(height(k1->left),k2->height)+1;
return k1;
}
pos singlerotatewithright(pos k1)
{
pos k2;
k2=k1->right;
k1->right=k2->left;
k2->left=k1;
k1->height=max(height(k1->left),height(k1->right))+1;
k2->height=max(k1->height,height(k2->right))+1;
return k2;
}
pos doublerotatewithleft(pos k3)
{
k3->left=singlerotatewithright(k3->left);
return singlerotatewithleft(k3);
}
pos doublerotatewithright(pos k1)
{
k1->right=singlerotatewithleft(k1->right);
return singlerotatewithright(k1);
}
void display(avltree t)
{
if(t!=NULL)
{
display(t->left);
printf("\n%d",t->element);
display(t->right);
}
}

OUTPUT:

MENU
1. Insertion
2. Display
3. Exit

Enter your choice:1


Enter the element to be inserted:33
The element is inserted

MENU
1. Insertion
2. Display
3. Exit

Enter your choice:1


Enter the element to be inserted:6
The element is inserted

MENU
1. Insertion
2. Display
3. Exit

Enter your choice:1


Enter the element to be inserted:12
The element is inserted

MENU
1. Insertion
2. Display
3. Exit

Enter your choice:1


Enter the element to be inserted:17
The element is inserted

MENU
1. Insertion
2. Display
3. Exit

Enter your choice:2


The elements in the AVL TREE are:
6
12
17
33

RESULT:

Thus the C program To Implement Insertion in AVL Trees was written and
verified.

You might also like