DS Lab
DS Lab
1. Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and also display
its first occurrence.
2. Given {5, 3,1,6,0,2,4} order the numbers in ascending order using Quick Sort.
3. Perform the Merge sort on the input {75,8,1,16,48,3,7,0} and display the output in
descending order
4. Write a program to insert the elements 61,16,8,27 into singly linked list and delete
8,61,27 from the list. Display your list after each insertion and deletion.
5. Write a program to add 6x3+10x2+0x+5 and 4x2+2x+1 using linked list.
6. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack,
also display the popped numbers.
7. Write a recursive program to find GCD of 4,6,8.
8. Write a program to inert the elements {5,7,0,6,3,9} into circular queue and delete
6,9&5 from it(using linked list implementation).
9. Given S1={“Flowers”} ; S2={“are beautiful”} ,
a) Find the length of S1.
b) Concatenate S1 and S2.
c) Extract the substring “low” from S1.
d) Find “are” in S2 and replace it with “is”.
10. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix
expression.
11. Write a program to evaluate a postfix expression 5 3+8 2 - *.
12. Write a program to create a binary tree with the elements 18,15,40,50,30,17,41
after creation insert 45 and 19 into tree and delete 15,17 and 41 from tree. Display
the tree on each insertion and deletion operation.
13. Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and
perform inorder, preorder and post order traversal.
14. Write a program to Sort the following elements using heap sort
{9.16,32,8,4,1,5,8,0}.
1.Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and also display its first
occurrence.
#include<stdio.h>
int middle;
if(first>last)
return(-1);
else
middle=(first+last)/2;
if(item<A[middle])
binsearch(A,item,first,middle-1);
else if(item>A[middle])
binsearch(A,item,middle+1,last);
else
return(middle);
void main()
int A[20],n,i,item,result;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
scanf("%d",&item);
result=binsearch(A,item,0,n-1);
if(result==1)
else
getch();
INPUT&OUTPUT
*************************************************
2.Given {5,3,1,6,0,2,4} order the numbers in ascending order using Quick Sort.
#include<stdio.h>
void main()
int i,n,a[20],lb,ub;
clrscr();
printf("+++++++++++++++++++++++++\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
lb=1;
ub=n;
printf("\n***********************\n");
for(i=1;i<=n;i++)
printf("%d",a[i]);
quick(a,lb,ub);
for(i=1;i<=n;i++)
printf("%d",a[i]);
printf("\n+++++++++++++++++++++++\n");
getch();
int up,down,temp,key;
int flag=1;
if(lb<ub)
up=lb;
down=ub;
key=a[lb];
while(flag)
up++;
while(a[up]<key)
up++;
while(a[down]>key)
down--;
if(up<down)
{
temp=a[up];
a[up]=a[down];
a[down]=temp;
else
flag=0;
temp=a[lb];
a[lb]=a[down];
a[down]=temp;
quick(a,lb,down-1);
quick(a,down+1,ub);
return;
}
3. Perform the Merge sort on the input {75,8,1,16,48,3,7,0} and display the output in descending
order.
#include<stdio.h>
#include<conio.h>
mergesort(int *,int,int);
merge(int *,int,int,int);
void main()
int a[20],n,i;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
mergesort(a,0,n-1);
for(i=n-1;i>=0;i--)
printf("%d\t",a[i]);
getch();
int mid;
if(lb<ub)
mid=(lb+ub)/2;
mergesort(a,lb,mid);
mergesort(a,mid+1,ub);
merge(a,lb,mid,ub);
int i,j,k,c[20];
i=lb;
k=lb;
j=mid+1;
while((i<=mid)&&(j<=ub))
if(a[i]<a[j])
c[k]=a[i];
k++;
i++;
else
{
c[k]=a[j];
k++;
j++;
while(i<=mid)
c[k]=a[i];
k++;
i++;
while(j<=ub)
c[k]=a[j];
k++;
j++;
for(i=lb;i<=k-1;i++)
a[i]=c[i];
}
4. Write a program to insert the elements 61,16,8,27 into singly linked list and delete 8,61,27
from the list. Display your list after each insertion and deletion.
#include<stdio.h>
#include<conio.h>
struct link
int item;
};
void addfirst();
void addlast();
void addmid();
void delfirst();
void dellast();
void delmid();
void display();
node *head=NULL;
void main()
int ch;
clrscr();
do
scanf("%d",&ch);
switch(ch)
case 1:
addfirst();
display();
break;
case 2:
addmid();
display();
break;
case 3:
addlast();
display();
break;
case 4:
delfirst();
display();
break;
case 5:
delmid();
display();
break;
case 6:
dellast();
display();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Invalid Choice\n");
while(ch<=8);
getch();
void addfirst()
node *temp;
temp=(node *)malloc(sizeof(node));
scanf("%d",&temp->item);
temp->next=head;
head=temp;
void addmid()
int i=1,pos;
node *cur=head,*temp;
scanf("%d",&pos);
while(pos!=i+1&&cur!=NULL)
cur=cur->next;
i++;
if(pos==i+1)
temp=(node *)malloc(sizeof(node));
scanf("%d",&temp->item);
temp->next=cur->next;
cur->next=temp;
void addlast()
node *temp,*cur=head;
temp=(node *)malloc(sizeof(node));
scanf("%d",&temp->item);
while(cur->next!=NULL)
cur=cur->next;
temp->next=cur->next;
cur->next=temp;
void delfirst()
node *temp=head;
head=head->next;
free(temp);
void delmid()
int i=1,pos;
node *cur=head,*temp;
scanf("%d",&pos);
while(pos!=i+1&&cur->next!=NULL)
{
cur=cur->next;
i++;
if(pos==i+1)
temp=cur->next;
cur->next=temp->next;
free(temp);
void dellast()
node *temp,*cur=head;
while(cur->next->next!=NULL)
cur=cur->next;
temp=cur->next;
cur->next=NULL;
free(temp);
void display()
{
node *cur=head;
printf("\nHead->");
while(cur!=NULL)
printf("\t%d",cur->item);
cur=cur->next;
printf("<-NULL\n");
---------------------------------------------------------------------------------------------------------------------
Output:
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Head-> 61<-NULL
SINGLY LINKED LIST OPERATIONS
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Head->61 16<-NULL
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Head->61 16 8<-NULL
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Head->61 16 8 27<-NULL
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Enter your option: 5
Deleted item is 8
Head->61 16 27<-NULL
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Deleted item is 61
Head->16 27<-NULL
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Deleted item is 27
Head-> 16<-NULL
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
Head-> 16<-NULL
1.Addfirst
2.AddMid
3.AddLast
4.DeleteFirst
5.DeleteMiddle
6.DeleteLast
7.Display
8.Exit
5. Write a program to add 6x3 +10x2 +0x+5 and 4x2 +2x+1 using linked list.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
int coeff;
int pow;
};
*poly = temp;
do{
scanf("%d", &coeff);
scanf("%d", &exp);
temp->coeff = coeff;
temp->pow = exp;
scanf("%d", &cont);
if(cont)
temp = temp->next;
temp->next = NULL;
}while(cont);
while(poly != NULL)
poly = poly->next;
if(poly != NULL)
printf("+");
}
void addPolynomials(struct Node** result, struct Node* first, struct Node* second)
temp->next = NULL;
*result = temp;
if(first->pow> second->pow)
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
else
temp->pow = first->pow;
first = first->next;
second=second->next;
temp = temp->next;
temp->next = NULL;
while(first || second)
temp = temp->next;
temp->next = NULL;
if(first)
temp->coeff = first->coeff;
temp->pow = first->pow;
first = first->next;
else if(second)
temp->coeff = second->coeff;
temp->pow = second->pow;
second = second->next;
void main()
clrscr();
printf("\nFirst polynomial:\n");
readPolynomial(&first);
printf("\nFirst");
displayPolynomial(first);
printf("\nSecond polynomial:\n");
readPolynomial(&second);
printf("\nSecond");
displayPolynomial(second);
printf("\n ------------------------------------------------\n");
printf("\n First");
displayPolynomial(first);
displayPolynomial(second);
printf("\n ------------------------------------------------\n");
addPolynomials(&result, first, second);
printf("\nAdded");
displayPolynomial(result);
getch();
6. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also display
the popped numbers.
#include<stdio.h>
#include<conio.h>
struct node
int info;
*top=NULL;
void main()
int choice;
while(1)
printf("STACK OPERATION\n");
printf("---------------\n");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong Choice!!!\n");
push()
{
struct node *NEWNODE;
int pushed_item;
scanf("%d",&pushed_item);
NEWNODE ->info=pushed_item;
NEWNODE ->link=top;
top=NEWNODE;
pop()
if(top==NULL)
printf("Stack is Empty!!!");
else
NEWNODE = top;
top=top->link;
free(NEWNODE);
display()
if(top==NULL)
printf("Stack is empty\n");
else
while(ptr!=NULL)
printf("%d\n",ptr->info);
ptr= ptr->link;
Output:
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
----------------
1. Push
2. Pop
3. Display
4. Quit
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
32
17
34
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
Poped item is 32
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
Poped item is 17
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
Poped item is 34
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
STACK OPERATION
----------------
1. Push
2. Pop
3. Display
4. Quit
*****************************************************************************
#include<stdio.h>
#include<conio.h>
return a;
return b;
return c;
void main()
Int m,n,o,result;
clrscr();
scanf("%d",&m);
scanf("%d",&n);
scanf("%d",&o);
{
if(m%result==0 && n%result==0 && o%result==0)
break;
getch();
******************************************************************************
8. Write a program to inert the elements {5,7,0,6,3,9} into circular queue and delete 6,9&5 from
it(using linked list implementation).
#include<stdio.h>
#include<conio.h>
struct cq
int data;
struct cq *next;
}*f=NULL,*r=NULL,*n,*temp,*temp1;
void cq_ins();
void cq_del();
void cq_dis();
int main()
int choice;
clrscr();
while(1)
printf("\n##########################");
scanf("%d",&choice);
switch(choice)
case 1:
cq_ins();
cq_dis();
break;
case 2:
cq_del();
break;
case 3:
cq_dis();
break;
case 4:
exit(0);
getch();
break;
default:
}
}
return 0;
void cq_ins()
int i;
scanf("%d",&n->data);
if(f==NULL)
f=n;
else
r->next=n;
r=n;
r->next=f;
void cq_del()
int x;
temp=f;
if(f==NULL)
{
else
if(f==r)
x=f->data;
free(temp);
f=NULL;
r=NULL;
else
x=temp->data;
f=f->next;
r->next=f;
free(temp);
printf("\nElement %d is Deleted",x);
cq_dis();
void cq_dis()
{
temp=f;
temp1=NULL;
if(f==NULL)
else
while(temp!=temp1)
printf("%d ",temp->data);
temp=temp->next;
temp1=f;
Output:
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
57
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
570
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
5706
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
57063
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
570639
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
Element 5 is Deleted
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
Element 7 is Deleted
0639
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
Element 0 is Deleted
639
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
639
MAIN MENU
#################################################
1. Insert
2. Delete
3. Display
4. Exit
******************************************************************************
#include<stdio.h>
#include<conio.h>
void main()
char str[20],str1[25],str2[25],str3[25],og[50],ss[50];
int l,x,y,st,len,pos,ch;
do
clrscr();
printf("\n5. Exit\n");
printf("\n----------------------------------");
switch(ch)
scanf("%s",&str);
l=stringln(str);
break;
case 2: fflush(stdin);
scanf("%[^\n]s",&str1);
fflush(stdin);
scanf("%[^\n]s",&str2);
concat(str1,str2);
break;
case 3:fflush(stdin);
scanf("%[^\n]s",&og);
scanf("%d%d",&st,&len);
extract(og,ss,st,len);
break;
case 4:fflush(stdin);
scanf("%[^\n]s",&str1);
fflush(stdin);
scanf("%[^\n]s",&str2);
pos=strfind(str1,str2);
if(pos>0)
fflush(stdin);
scanf("%[^\n]s",&str3);
strreplace(str1,str3,pos);
else
break;
case 5: exit(0);
break;
if(ch!=5) getch();
}
while(ch!=5);
int count=0;
while(*p!='\0')
count++;
p++;
return count;
while(*s1!='\0')
s1++;
while(*s2!='\0')
*s1=*s2;
s1++;
s2++;
*s1='\0';
int i;
str1=str1+(s-1);
*str2++=*str1++;
*str2='\0';
int len1,len2,i,j,f,pos=0;
len1=stringln(p1);
len2=stringln(p2);
for(i=0;i<(len1);i++)
f=1;
pos++;
for(j=0;j<len2;j++)
if(*(p1+i+j)!=*(p2+j))
f=0;
break;
}
}
if(f==1)
return(pos);
return (0);
int i,len;
len=stringln(p3);
for(i=pos-1;i<(len+pos)-1;i++)
*(p1+i)=*p3;
p3++;
input / output
1. Length of a string
-------------------------------
flowers
***
flowers
are beautiful
***
flowers
2 3
***
are
is
******************************************************************************
10. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression
#define SIZE 50
#include<ctype.h>
char s[SIZE];
int top=-1;
s[++top]=elem;
char pop()
return(s[top--]);
{
switch(elem)
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
void main()
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
clrscr();
scanf("%s",infx);
push('#');
if(ch=='(')
push(ch);
else
if(isalnum(ch))
pofx[k++]=ch;
else
if(ch==')')
while(s[top]!='(')
pofx[k++]=pop();
elem=pop();
else
while(pr(s[top])>=pr(ch))
pofx[k++]=pop();
push(ch);
while(s[top]!='#')
pofx[k++]=pop();
pofx[k]='\0';
getch();
output :
Read the Infix expression : x^y/(5*z)+2
******************************************************************************
----------------------------------------------------------
5 5
3 5 3
+ 8
8 8 8
2 8 8 2
- 8 6
* 48
------------------------------------------------
#include<stdio.h>
#include<ctype.h>
int s[50];
s[++top] = elem;
}
int pop()
return s[top--];
void main()
char pofx[50];
char *ch;
int op1,op2,num;
clrscr();
scanf("%s",pofx);
ch = pofx;
while(*ch != '\0')
if(isdigit(*ch))
push(num);
else
op1 = pop();
op2 = pop();
switch(*ch)
ch++;
getch();
output:
******************************************************************************
12. Write a program to create a binary tree with the elements 18,15,40,50,30,17,41 after
creation insert 45 and 19 into tree and delete 15,17 and 41 from tree. Display the tree on each
insertion and deletion operation.
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>
struct node
{
struct node*rlink;
int data;
};
void main()
int s,d;
head=NULL;
clrscr();
do
printf("1-Insertion\n\n");
printf("2-Deletion\n\n");
printf("3-Display\n\n");
printf("4-Exit\n\n");
printf("---------------------\n");
printf("Enter Choice:\n");
scanf("%d",&s);
switch(s)
case 1://insertion
if(head==NULL)
head=finsert();
else
insert(head);
break;
case 2://Deletion
if(head==NULL)
else
scanf("%d",&d);
t=head;
head=NULL;
free(t);
else
head = delenode(head,d);
}
break;
if(head==NULL)
printf("Binary TreeEmpty....");
else
inorder(head);
break;
case 4://exit
exit(0);
}while(s<5 ||s>0);
getch();
scanf("%d",&head->data);
head->llink=NULL;
head->rlink=NULL;
return head;
}
void insert(struct node*head)
t=head;
printf("Enter TheElements:");
scanf("%d",&n->data);
n->llink=NULL;
n->rlink=NULL;
while(t->llink!=NULL ||t->rlink!=NULL)
if(t->llink!=NULL)
t=t->llink;
if(t->rlink!=NULL)
if(n->data>=t->data)
t=t->rlink;
t->rlink->data))
break;
t->llink->data))
break;
if((n->data<t->data)&&(t->llink==NULL))
t->llink=n;
if((n->data>t->data)&&(t->rlink==NULL))
t->rlink=n;
if(head!=NULL)
inorder(head->llink);
printf("%d ",head->data);
inorder(head->rlink);
int f=0,f1=0;
t=head;
while(t!=NULL)
if(t->data==d)
f=1;
x=t;
break;
if(t->data > d)
p=t;
t=t->llink;
p=t;
t=t->rlink;
if(f==0)
return head;
if(p->rlink==x)
p->rlink=NULL;
else
p->llink=NULL;
free(x);
return head;
p=x;
t1=x->rlink;
while(t1->llink!=NULL)
p=t1; f1=1;
t1=t1->llink;
x->data=t1->data;
if(f1==1)
p->llink=t1->llink;
if(f1==0)
x->rlink=t1->rlink;
free(t1);
return head;
if(t1->rlink!=NULL)
{
x->data=t1->data;
if(f1==1)
p->llink=t1->rlink;
if(f1==0)
p->rlink=t1->rlink;
free(t1);
return head;
if(p->llink==x)
p->llink=x->rlink;
else
p->rlink=x->rlink;
free(x);
return head;
if(p->llink==x)
p->llink=x->llink;
else
p->rlink=x->llink;
free(x);
return head;
head=x->llink;
free(p);
return head;
head=x->rlink;
free(p);
return head;
-------------------------------------------------------------------------------------------------------------------
Output:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
3
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
15
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
17
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
41
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Coice:
1-Insertion
2-Deletion
3-Display
4-Exit
---------------------------------------
Enter Choice:
4
13.Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform
inorder, preorder and post order traversal.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int data;
TNODE;
void main()
TNODE *root=NULL;
int opn,elem,n,i;
do
clrscr();
scanf("%d", &opn);
switch(opn)
case 1:
root=NULL;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&elem);
root=CreateBST(root,elem);
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
break;
default:
break;
getch();
while(opn!=5);
if(root==NULL)
{
root=(TNODE *)malloc(sizeof(TNODE));
root->left=root->right=NULL;
root->data=elem;
return root;
else
if(elem<root->data)
root->left=CreateBST(root->left,elem);
root->right=CreateBST(root->right,elem);
else
return(root);
}------
if(root!=NULL)
inorder(root->left);
printf("%d \t",root->data);
inorder(root->right);
}
}
if(root!=NULL)
printf("%d \t",root->data);
preorder(root->left);
preorder(root->right);
if(root!=NULL)
postorder(root->left);
postorder(root->right);
printf("%d \t",root->data);
-------
Output:
2 - Traversal In Inorder
3 - Traversal In Preorder
4 - Traversal In Postorder
5 - Exit
------------------------------------------------------------------------------------
2 - Traversal In Inorder
3 - Traversal In Preorder
4 - Traversal In Postorder
5 - Exit
0123569
Press any key to continue...
--------------------------------------------------------------------------------------
2 - Traversal In Inorder
3 - Traversal In Preorder
4 - Traversal In Postorder
5 - Exit
2105396
-----------------------------------------------------------------------------------------------------------------------------
2 - Traversal In Inorder
3 - Traversal In Preorder
4 - Traversal In Postorder
5 - Exit
0136952
Press any key to continue...
-----------------------------------------------------------------------------------------------------------------------------
2 - Traversal In Inorder
3 - Traversal In Preorder
4 - Traversal In Postorder
5 - Exit
Terminating
***************************************************************************
LAB14 . Write a program to Sort the following elements using heap sort {9.16,32,8,4,1,5,8,0}
#include<stdio.h>
#include<conio.h>
createheap(int[],int);
heapsort(int[],int);
void main()
{
int k[10],i,n;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&k[i]);
for(i=1;i<=n;i++)
printf("%4d",k[i]);
heapsort(k,n);
for(i=1;i<=n;i++)
printf("%4d",k[i]);
getch();
createheap(int k[],int n)
int temp,q,i,j,key;
for(q=2;q<=n;q++)
i=q;
key=k[q];
j=i/2;
while((i>1)&&(key>k[j]))
{
temp=k[j];
k[j]=k[i];
k[i]=temp;
i=j;
j=i/2;
if(j<1)
j=1;
k[i]=key;
heapsort(int k[],int n)
int temp,q,i,j,key;
createheap(k,n);
for(q=n;q>=2;q--)
temp=k[q];
k[q]=k[1];
k[1]=temp;
i=1;
j=2;
key=k[1];
if((j+1)<q)
{
if(k[j+1]>k[j])
j++;
while((j<=(q-1))&&(k[j]>key))
temp=k[j];
k[j]=k[i];
k[i]=temp;
i=j;
j=2*i;
if(j+1<q)
if(k[j+1]>k[j])
j++;
else
if(j>n)
j=n;
k[i]=key;
9,16,32,8,4,1,5,8,0
original list
9 16 32 8 4 1 5 8 0
sorted list
0 1 4 5 8 8 9 16 32