You are on page 1of 25

DEARTMENT OF CS & SE DATA STRUCTURES LAB

1. Write a C program for sorting a list using Bubble sort and then apply binary search.
2. Write a C program to implement the operations on stacks.
3. Write a C program to implement the operations on circular queues.
4. Write a C program for evaluating a given postfix expression using stack.
5. Write a C program for converting a given infix expression to postfix form using stack.
6. Write a C program for implementing the operations of a dequeue
7. Write a C program for the representation of polynomials using circular linked list and for the
addition of two such polynomials
8. Write a C program for quick sort
9. Write a C program for Merge sort.
10. Write a C program for Heap sort
11. Write a C program to create a binary search tree and for implementing the in order,
preorder, post order traversal using recursion
12. a)Write a C program for finding the transitive closure of a digraph
b)Write a C program for finding the shortest path from a given source to any vertex in a
digraph using Dijkstra’s algorithm.
13. a)Write a C program for finding the Depth First Search of a graph.
b)Write a C program for finding the Breadth First Search of a graph.

Program 1

Write C programs that implement Bubble sort, to sort a given list of integers in
ascending order

void main()
{
int a[10],t, i, j, n;
clrscr();
printf("ENTER NUMBER OF ELEMENTS IN A LIST:");
scanf("%d", &n);
printf("ENTER %d ELEMENTS :\n", n);
for(i=0;i<n;i++)
scanf("%d", &a[i]);
for(i=0; i<n; i++)
for(j=0; j<n-i; j++)
{
if(a[j] > a[j+1])
{
t =a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
printf("AFTER BUBBLE SORT THE GIVE %d ELEMENTS ARE:\n",n);
for(i=0; i<n; i++)
printf("%3d", a[i]);
}

1
DEARTMENT OF CS & SE DATA STRUCTURES LAB

DESCRITPTION:
Sorting means arranging the elements in the given array either inascending order or
descending order. In the bubble sort technique it will compare the elements with the
neighbouring elements and gives the largest element in the uppermost position. The
timecomplexity of this sorting is O(n2).

OUTPUT:
ENTER NUMBER OF ELEMENTS IN A LIST: 5
ENTER 5 ELEMENTS : 4 2 7 1 8
AFTER BUBBLE SORT THE GIVE 5 ELEMENTS ARE: 1 2 4 7 8

Program 8

Write C programs that implement Quick sort, to sort a given list of integers in ascending
order
main()
{
int i,n,lv,hv,x[10];
clrscr();
printf("\nENTER THE RANGE OF THE LIST: ");
scanf("%d",&n);
printf("ENTER %d ELEMENTS:\n",n);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
lv=0;
hv=n-1;
qs(x,lv,hv);
printf("\nTHE SORTED ELEMENTS AFTER QUICK SORT ARE:\n ");
for(i=0;i<n;i++)
printf("%3d",x[i]);
getch();
}
/*quick sort function*/
qs(int a[],int l,int r)
{
int newl,newr,piv,t;
newl=l;
newr=r;
piv=a[(newl+newr)/2];
do
{
while(a[newl]<piv && newl<r)
newl++;
while(piv<a[newr] && newr>l)
newr--;

2
DEARTMENT OF CS & SE DATA STRUCTURES LAB

if(newl<=newr)
{
t=a[newl];
a[newl]=a[newr];
a[newr]=t;
newl++;
newr--;
}
} while(newl<=newr);
if(l<newr) qs(a,l,newr);
if(newl<r) qs(a,newl,r);
}

DESCRIPTION:
This quicksort technique is one of the best sorting technique. The time complexity for
quick sort is O(nlog2n). Here the technique is ,initially it will take one pivot element, and
that will be compared with the left and rightmost elements. The pivot element is
compared with the left most element until its greater than the pivot element, and then
compared with the rightmost element until its less than the pivot element. Whenever this
condition got satisfied we just swapping the left position element and the right position
element. Whenever the left and right positions are equal or right postion crossed the left
position then we will swap the rightmost position element with the pivot element. This
process will repeat unitl we get the list in sorted order.

OUTPUT:
ENTER THE RANGE OF THE LIST: 10
ENTER 10 ELEMENTS: 3 1 4 6 2 8 9 7 0 5
THE SORTED ELEMENTS AFTER QUICK SORT ARE: 0 1 2 3 4 5 6 7 8 9

Program 11
Write C programs that implement heap sort, to sort a given list of integers in ascending
order
#include<stdio.h>
void heap(int a[],int n)
{
int i,parent,child,next;
for(i=2;i<=n;i++)
{
next=a[i];
child=i;
parent=child/2;
while(child>1&&next>a[parent])
{
a[child]=a[parent];
child=parent;

3
DEARTMENT OF CS & SE DATA STRUCTURES LAB

parent=child/2;
if(parent<1)
parent=1;
}
a[child]=next;
}
}
void main()
{
int n,i,last,temp,a[20];
clrscr();
printf("\nENTER THE N VALUE FOR THE RANGE OF VALUES: ");
scanf("%d",&n);
printf("\nENTER %d ELEMENTS INTO THE LIST:",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heap(a,n);
for(last=n;last>=2;last--)
{
temp=a[1];
a[1]=a[last];
a[last]=temp;
heap(a,last-1);
}
printf("\n*** THE SORTED ELEMETNS AFTER HEAP SORT ARE: *** \n\n");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
getch();
}

DESCRIPTION:
The heap sort technique sorts the elements in a different way, here it will construct a tree
by taking maximum heap or minimum heap.

OUTPUT:
ENTER THE N VALUE FOR THE RANGE OF VALUES: 6
ENTER 6 ELEMENTS INTO THE LIST: 3 1 5 2 8 9
*** THE SORTED ELEMETNS AFTER HEAP SORT ARE: ***
123589

Program 10

4
DEARTMENT OF CS & SE DATA STRUCTURES LAB

Write C programs that implement merge sort to sort a given list of integers in ascending
order.
int a[20],tmp[20];
main()
{
int i,n;
clrscr();
printf("\n\tENTER N VALUE FOR THE RANGE OF VALUES: ");
scanf("%d",&n);
printf("\n\tENTER %d VALUES INTO THE LIST:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
printf("\n\t*** THE SORTED LIST AFTER MERGE SORT IS: ***\n\n\\t");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

mergesort(int low,int high)


{
if(low<high)
{
int mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
merge(int low,int mid,int high)
{
int k,h=low, i=low,j=mid+1;
while((h<=mid) && (j<=high))
{
if(a[h]<=a[j])
{
tmp[i]=a[h];
h++;
}
else
{
tmp[i]=a[j];
j++;
}
i++;
}

5
DEARTMENT OF CS & SE DATA STRUCTURES LAB

if(h>mid)
for(k=j;k<=high;i++,k++) tmp[i]=a[k];
else
for(k=h;k<=mid;i++,k++) tmp[i]=a[k];
for(k=low;k<=high;k++) a[k]=tmp[k];
}

DESCRIPTION:
This is the natural merge sort techinique, here it will find the mid element then the
comparisions will be done by the mid element. This process will repeat until we get the
elements in sorted order.

OUTPUT:
ENTER N VALUE FOR THE RANGE OF VALUES: 8
tENTER 8 VALUES INTO THE LIST: 3 2 1 5 6 7 9 4
*** THE SORTED LIST AFTER MERGE SORT IS: ***
12345679

Program 2

Write C programs that implement stack (its operations) using arrays

#include <stdio.h>
#include<ctype.h>
# define MAXSIZE 5
int stack[MAXSIZE];
int top=-1; /*index pointing to the top of stack*/
void main()
{
void push(int);
int pop();
void display();
int choice=1,i,num;
clrscr();
while(choice ==1)
{
printf("******* MAIN MENU *******\n");
printf("1.Add element to stack \n");
printf("2.Delete element from the stack \n");
printf("3.Display stack Elements \n");
printf("4.Exit\n");
printf("*************************\n");
printf(“Enter the choice:\n”);
scanf("%d",&choice);
switch(choice)
{

6
DEARTMENT OF CS & SE DATA STRUCTURES LAB

case 1:
printf("Enter the data... ");
scanf("%d",&num);
push(num);
break;
case 2: i=pop();
printf(" Value returned from pop function is : %d ",i);
break;
case 3: display();
break;
case 4: exit(1);
break;
default: printf("Invalid Choice . ");
}
printf("\n Do you want continue do operations on Stack Press 1 ELSE Press any
key");
scanf("%d" , &choice);
} /* end of outer while */
} /* end of main */
void push(int y)
{
if(top>MAXSIZE-1)
{
printf(" STACK FULL");
return;
}
else
{
top++;
stack[top]=y;
}
}
int pop()
{
int a;
if(top<0)
{
printf("---STACK IS EMPTY---");
return 0;
}
else
{
a=stack[top];
top--;
}
return(a);

7
DEARTMENT OF CS & SE DATA STRUCTURES LAB

}
void display()
{
int i;
if(top<0)
{
printf("TEH STACK IS EMPTY");
return;
}
printf("THE STACK ELEMENTS ARE:\n");
for(i=top; i>=0; i --)
printf("%d\n", stack[i]);
}

DESCRIPTION:
A stack is one of the most important and useful non-primitive linear data structure in
computer science. It is an ordered collection of items into which new data items may be
added/inserted and from which items may be deleted at only one end, called the top of the
stack. As all the addition and deletion in a stack is done from the top of the stack, the last
added element will be first removed from the stack. That is why the stack is also called
Last-in-First-out (LIFO). Note that the most frequently accessible element in the stack
is the top most elements, whereas the least accessible element is the bottom of the stack.
The primitive operations performed on the stack are as follows: PUSH: The process of
adding (or inserting) a new element to the top of the stack is called PUSH operation.
Pushing an element to a stack will add the new element at the top. After every push
operation the top is incremented by one. If the array is full and no new element can be
accommodated, then the stack overflow condition occurs. POP: The process of deleting
(or removing) an element from the top of stack is called POP operation. After every pop
operation the stack is decremented by one. If there is no element in the stack and the pop
operation is performed then the stack underflow condition occurs.

OUTPUT:
******* MAIN MENU *******
1.Add element to stack
2.Delete element from the stack
3.Display stack Elements
4.Exit
*************************
Enter the choice: 1
Enter the data...5
******* MAIN MENU *******
1.Add element to stack
2.Delete element from the stack
3.Display stack Elements
4.Exit
*************************

8
DEARTMENT OF CS & SE DATA STRUCTURES LAB

Enter the choice: 1


Enter the data...7
******* MAIN MENU *******
1.Add element to stack
2.Delete element from the stack
3.Display stack Elements
4.Exit
*************************
Enter the choice: 1
Enter the data...8
******* MAIN MENU *******
1.Add element to stack
2.Delete element from the stack
3.Display stack Elements
4.Exit
*************************
Enter the choice: 3
THE STACK ELEMENTS ARE:
578

Program 5

Write a C program that uses Stack operations to Convert infix expression into postfix
expression
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
char stack[MAX];
int top=-1;
char pop();
void push(char item);
int prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
break;
case '*':
case '/':return 4;
break;
case '^':
case '$':return 6;
break;
case '(':

9
DEARTMENT OF CS & SE DATA STRUCTURES LAB

case ')':
case '#':return 1;
break;
}
}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':return 1;
break;
default:return 0;
}
}

void convertip(char infix[],char postfix[])


{
int i,symbol,j=0;
stack[++top]='#';

for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else {
if(symbol=='(')
push(symbol);
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();/*pop out (.*/

10
DEARTMENT OF CS & SE DATA STRUCTURES LAB

}
else
{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';/*null terminate string.*/
}

void main()
{

char infix[20],postfix[20];
clrscr();
printf("Enter the valid infix string:\n");
gets(infix);
convertip(infix,postfix);
printf("The corresponding postfix string is:\n");
puts(postfix);
getch();
}

void push(char item)


{
top++;
stack[top]=item;
}

char pop()
{
char a;

11
DEARTMENT OF CS & SE DATA STRUCTURES LAB

a=stack[top];
top--;
return a;
}

DESCRIPTION:
The method of converting infix expression A + B * C to postfix form is:
A + B * C Infix Form
A + (B * C) Parenthesized expression
A + (B C *) Convert the multiplication
A (B C *) + Convert the addition
A B C * + Postfix form
The rules to be remembered during infix to postfix conversion are:
1. Parenthesize the expression starting from left to light.
2. During parenthesizing the expression, the operands associated with operator
having higher precedence are first parenthesized. For example in the above expression
B * C is parenthesized first before A + B.
3. The sub-expression (part of expression), which has been converted into postfix,
is to be treated as single operand.
4. Once the expression is converted to postfix form, remove the parenthesis.
Problem 3.4.2.1. Give postfix form for A + [ (B + C) + (D + E) * F ] / G
Solution. Evaluation order is
A + { [ (BC +) + (DE +) * F ] / G}
A + { [ (BC +) + (DE + F *] / G}
A + { [ (BC + (DE + F * +] / G} .
A + [ BC + DE + F *+ G / ]
ABC + DE + F * + G / + Postfix Form

OUTPUT:
Enter the valid infix string: A+(B*C)
The corresponding postfix string is: ABC*+

QUEUE PROGRAM

Write C programs that implement Queue (its operations) using arrays.

#include <stdio.h>
#include<ctype.h>
# define MAXSIZE 5
int q[MAXSIZE];
int front, rear,run=1;
void main()
{
void insert(int);int del();void display();
int ch,i,num;
front=-1; rear =-1;

12
DEARTMENT OF CS & SE DATA STRUCTURES LAB

clrscr();
printf("\nProgram for queue demonstration through array\n");
while(run==1)
{
printf("\n\t****MAIN MENU****\n\n");
printf("\t1.Add element to queue\n");
printf("\t2.Delete element from the queue\n");
printf("\t3.Display Queue Elements\n");
printf("\t4.Exit\n");
printf("\n\t*******************\n");
printf("\tEnter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the data in Queue... ");
scanf("%d",&num);
insert(num); break;
case 2: i=del();
printf(" Value returned from Delete function is = %d",i); break;
case 3: display(); break;
case 4: exit(1);break;
default: printf("Invalid Choice ... ");
}
printf("\nDo you want to do more operations on Queue press 1 or another key to exit):");
scanf("%d",&run);
} /*end of outer while*/
} /*end of main*/
void insert(int a)
{
if(front==-1)
front=0;
if(rear==MAXSIZE-1)
{
printf("QUEUE FULL IS FULL");
return;
}
else
{
rear=rear+1;
q[rear]=a;
printf(" Value of rear = %d and the value of front is %d",rear,front);
}
}

int del()

13
DEARTMENT OF CS & SE DATA STRUCTURES LAB

{
int a;
if(front==-1)
{
printf(" QUEUE IS EMPTY\n");
return 0;
}
a=q[front];
q[front]=NULL;

if(front == rear)
front=rear=-1;
else
front=front+1;
return a;
}
void display()
{
int i;
if(front==-1)
{
printf(" QUEUE IS EMPTY\n");
}
printf("THE QUEUE ELEMENTS ARE:");
for(i=front;i<=rear;i++)
printf("%d ",q[i]);
}

DESCRIPTION:
A queue is logically a first in first out (FIFO or first come first serve) linear data
structure. The concept of queue can be understood by our real life problems. For example
a customer come and join in a queue to take the train ticket at the end (rear) and the ticket
is issued from the front end of queue. That is, the customer who arrived first will receive
the ticket first. It means the customers are serviced in the order in which they arrive at the
service centre. It is a homogeneous collection of elements in which new elements are
added at one end called rear, and the existing elements are deleted from other end called
front. The basic operations that can be performed on queue are
1. Insert (or add) an element to the queue (push)
2. Delete (or remove) an element from a queue (pop)
Push operation will insert (or add) an element to queue, at the rear end, by
incrementing the array index. Pop operation will delete (or remove) from the front end by
decrementing the array index and will assign the deleted value to a variable. Total number
of elements present in the queue is front-rear+1, when implemented using arrays.

14
DEARTMENT OF CS & SE DATA STRUCTURES LAB

OUTPUT:
****MAIN MENU****
1.Add element to queue
2.Delete element from the queue
3.Display Queue Elements
4.Exit
*******************
Enter Your Choice: 1
Value of rear = 4 and the value of front is 4
****MAIN MENU****
1.Add element to queue
2.Delete element from the queue
3.Display Queue Elements
4.Exit
*******************
Enter Your Choice: 1
Value of rear = 7 and the value of front is 7
****MAIN MENU****
1.Add element to queue
2.Delete element from the queue
3.Display Queue Elements
4.Exit
*******************
Enter Your Choice: 1
Value of rear = 9 and the value of front is 9
****MAIN MENU****
1.Add element to queue
2.Delete element from the queue
3.Display Queue Elements
4.Exit
*******************
Enter Your Choice: 3
THE QUEUE ELEMENTS ARE:
479

AVL Tree Implementation in C

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define false 0

15
DEARTMENT OF CS & SE DATA STRUCTURES LAB

#define true 1
struct avlnode
{
int data;
int balfact;
struct avlnode*left;
struct avlnode*right;
};
struct avlnode*insert(struct avlnode*,int,int*);
struct avlnode*deldata(struct avlnode*,int,int*);
struct avlnode*del(struct avlnode*,struct avlnode*,int*);
struct avlnode*balr(struct avlnode*,int*);
struct avlnode*ball(struct avlnode*,int*);
void display(struct avlnode *);
void displaypre(struct avlnode *);
void displaypost(struct avlnode *);
void deltree(struct avlnode *);
void main()
{
int i,data,del;
struct avlnode *avl=NULL;
int h,n;
clrscr();
printf("enter number of elements:");
scanf("%d",&n);
printf("\n enter the elements in the avl tree:\n");
for(i=0;i<n;i++)
{
scanf("%d",&data);
avl=insert(avl,data,&h);
}
printf("\n avl tree:\n");
printf("\n in-order:");
display(avl);
printf("\n pre-order:");
displaypre(avl);
printf("\n post-order:");
displaypost(avl);
printf("\n enter the element you want to delete:");
scanf("%d",&del);

16
DEARTMENT OF CS & SE DATA STRUCTURES LAB

avl=deldata(avl,del,&h);
printf("\n avl tree after deletion of a node:\n");
display(avl);
deltree(avl);
getch();
}
struct avlnode *insert(struct avlnode*root,int data,int *h)
{
struct avlnode *node1,*node2;
if(!root)
{
root=(struct avlnode*)malloc(sizeof(struct avlnode));
root->data=data;
root->left=NULL;
root->right=NULL;
root->balfact=0;
*h=true;
return(root);
}
if(data<root->data)
{
root->left=insert(root->left,data,h);
if(*h)
{
switch(root->balfact)
{
case 1:
node1=root->left;
if(node1->balfact==1)
{
printf("\n right rotation along %d.",root->data);
root->left=node1->right;
node1->right=root;
root->balfact=0;
root=node1;
}
else
{
printf("\n double rotation,left along %d",node1->data);
node2=node1->right;

17
DEARTMENT OF CS & SE DATA STRUCTURES LAB

node1->right=node2->left;
printf("then right along %d.\n",root->data);
node2->left=node1;
root->left=node2->right;
node2->right=root;
if(node2->balfact==1)
root->balfact=-1;
else
root->balfact=0;
if(node2->balfact==-1)
node1->balfact=1;
else
node1->balfact=0;
root=node2;
}
root->balfact=0;
*h=false;
break;
case 0:
root->balfact=1;
break;
case -1:
root->balfact=0;
*h=false;
}
}
}
if(data>root->data)
{
root->right=insert(root->right,data,h);
if(*h)
{
switch(root->balfact)
{
case 1:
root->balfact=0;
*h=false;
break;
case 0:
root->balfact=-1;

18
DEARTMENT OF CS & SE DATA STRUCTURES LAB

break;
case -1:
node1=root->right;
if(node1->balfact==-1)
{
printf("\n left rotation along %d.",root->data);
root->right=node1->left;
node1->left=root;
root->balfact=0;
root=node1;
}
else
{
printf("\n double rotation,right along %d",node1->data);
node2=node1->left;
node1->left=node2->right;
node2->right=node1;
printf("then left along %d.\n",root->data);
root->right=node2->left;
node2->left=root;
if(node2->balfact==-1)
root->balfact=1;
else
root->balfact=0;
if(node2->balfact==1)
node1->balfact=-1;
else
node1->balfact=0;
root=node2;
}
root->balfact=0;
*h=false;
}
}
}
return(root);
}
struct avlnode *deldata(struct avlnode *root,int data,int *h)
{
struct avlnode*node;

19
DEARTMENT OF CS & SE DATA STRUCTURES LAB

if(!root)
{
printf("\n element not found!\n");
return(root);
}
else
{
if(data<root->data)
{
root->left=deldata(root->left,data,h);
if(*h)
root=balr(root,h);
}
else
{
if(data>root->data)
{
root->right=deldata(root->right,data,h);
if(*h)
root=ball(root,h);
}
else
{
node=root;
if(node->right==NULL)
{
root=node->left;
*h=true;
free(node);
}
else
{
node->right=del(node->right,node,h);
if(*h)
root=ball(root,h);
}
}
}
}
return(root);

20
DEARTMENT OF CS & SE DATA STRUCTURES LAB

}
struct avlnode *del(struct avlnode*succ,struct avlnode*node,int*h)
{
struct avlnode*temp=succ;
if(succ->left!=NULL)
{
succ->left=del(succ->left,node,h);
if(*h)
succ=balr(succ,h);
}
else
{
temp=succ;
node->data=succ->data;
succ=succ->right;
free(temp);
*h=true;
}
return(succ);
}
struct avlnode*balr(struct avlnode*root,int *h)
{
struct avlnode*node1,*node2;
switch(root->balfact)
{
case 1:
root->balfact=0;
break;
case 0:
root->balfact=-1;
*h=false;
break;
case -1:
node1=root->right;
if(node1->balfact<=0)
{
printf("\n left rotation along %d.",root->data);
root->right=node1->left;
node1->left=root;
if(node1->balfact==0)

21
DEARTMENT OF CS & SE DATA STRUCTURES LAB

{
root->balfact=-1;
node1->balfact=1;
*h=false;
}
else
{
root->balfact=node1->balfact=0;
}
root=node1;
}
else
{
printf("\n double rotation,right along %d",node1->data);
node2=node1->left;
node1->left=node2->right;
node2->right=node1;
printf("then left along %d.\n",root->data);
root->right=node2->left;
node2->left=root;
if(node2->balfact==-1)
root->balfact=1;
else
root->balfact=0;
if(node2->balfact==1)
node1->balfact=-1;
else
node1->balfact=0;
root=node2;
node2->balfact=0;
}
}
return(root);
}
struct avlnode*ball(struct avlnode*root,int*h)
{
struct avlnode *node1,*node2;
switch(root->balfact)
{
case -1:

22
DEARTMENT OF CS & SE DATA STRUCTURES LAB

root->balfact=0;
break;
case 0:
root->balfact=1;
*h=false;
break;
case 1:
node1=root->left;
if(node1->balfact>=0)
{
printf("\n right rotation along %d.",root->data);
root->left=node1->right;
node1->right=root;
if(node1->balfact==0)
{
root->balfact=1;
node1->balfact=-1;
*h=false;
}
else
{
root->balfact=node1->balfact=0;
}
root=node1;
}
else
{
printf("\n double rotation,left along %d",node1->data);
node2=node1->right;
node1->right=node2->left;
node2->left=node1;
printf("then right along %d.\n",root->data);
root->left=node2->right;
node2->right=root;
if(node2->balfact==1)
root->balfact=-1;
else
root->balfact=0;
if(node2->balfact==-1)
node1->balfact=1;

23
DEARTMENT OF CS & SE DATA STRUCTURES LAB

else
node1->balfact=0;
root=node2;
node2->balfact=0;
}
}
return(root);
}
void display(struct avlnode*root)
{
if(root!=NULL)
{
display(root->left);
printf("%d\t",root->data);
display(root->right);
}
}
void displaypre(struct avlnode *root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
displaypre(root->left);
displaypre(root->right);
}
}
void displaypost(struct avlnode*root)
{
if(root!=NULL)
{
displaypost(root->left);
displaypost(root->right);
printf("%d\t",root->data);
}
}
void deltree(struct avlnode*root)
{
if(root!=NULL)
{
deltree(root->left);

24
DEARTMENT OF CS & SE DATA STRUCTURES LAB

deltree(root->right);
}
free(root);
}

DESCRIPTION:
This algorithm was developed in 1962 by two Russian Mathematicians, G.M. Adel’son
Vel’sky and E.M. Landis; here the tree is called AVL Tree. An AVL tree is a binary tree in
which the left and right sub tree of any node may differ in height by at most 1, and in
which both the sub trees are themselves AVL Trees. Each node in the AVL Tree possesses
any one of the following properties :
(a) A node is called left heavy, if the largest path in its left sub tree is one level larger
than the largest path of its right sub tree.
(b) A node is called right heavy, if the largest path in its right sub tree is one level
larger than the largest path of its left sub tree.
(c) The node is called balanced, if the largest paths in both the right and left sub
trees are equal.

OUTPUT :
Enter the number of elements: 5
Enter the elements in the AVL tree:
54 78 25 32 65

AVL tree:
In-order: 25 32 54 65 78
Pre-order: 54 25 32 78 65
Post-order: 32 25 65 78 54
Enter the element you want to delete: 78
AVL tree after deletion of a node:25 32 54 65

25