dslab

38
2. Design, develop, and execute a program in C to convert a given valid parenthesized infix arithmetic expression to postfix expression and then to print both the expressions. The expression consists of single character operands and the binary operators + (plus), - (minus), * (multiply) and / (divide). #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 5 int F(char ch) { switch(ch) { case '+': case '-':return 2; case '*': case '/':return 4; case '^':return 5; case '(':return 0; case '#':return -1; default: return 8; } } int G(char ch) { switch(ch) { case '+': case '-':return 1; case '*': case '/':return 3; case '^':return 6; case '(':return 9; case ')':return 0; default: return 7;

Upload: kindrohit1989

Post on 02-Dec-2014

112 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DSLAB

2. Design, develop, and execute a program in C to convert a given valid parenthesized infix arithmetic expression to postfix expression and then to print both the expressions. The expression consists of single character operands and the binary operators + (plus), - (minus), * (multiply) and / (divide).

#include<stdio.h>#include<conio.h>#include<string.h>

#define MAX 5

int F(char ch){

switch(ch){

case '+':case '-':return 2;case '*':case '/':return 4;case '^':return 5;case '(':return 0;case '#':return -1;default: return 8;

}}

int G(char ch){

switch(ch){

case '+':case '-':return 1;case '*':case '/':return 3;case '^':return 6;case '(':return 9;case ')':return 0;default: return 7;

}}

void in2pos(char infix[],char postfix[]){

Page 2: DSLAB

int top=-1,i,j=0;char symbol,s[100];s[++top]='#';

for(i=0;i<strlen(infix);i++){

symbol=infix[i];

while(F(s[top])>G(symbol)){

postfix[j]=s[top--];j++;

}if (F(s[top])!=G(symbol))

s[++top]=symbol;else

top--;}

while(s[top]!='#'){

postfix[j++]=s[top--];}

postfix[j]='\0';}

void main(){

char infix[100],postfix[100];clrscr();

printf("\nEnter the infix statement..\n");scanf("%s",infix);

in2pos(infix,postfix);

printf("\nThe postfix expression for\n%s is\n%s",infix,postfix);

getch();}/* OUTPUTEnter the infix statement.((1+(2-3)*4)^5+6)The postfix expression for((1+(2-3)*4)^5+6) is

Page 3: DSLAB

123-4*+5^6+*/

3. Design, develop, and execute a program in C to evaluate a valid postfix expression using stack. Assume that the postfix expression is read as a single line consisting of non-negative single digit operands and binary arithmetic operators. The arithmetic operators are + (add), - (subtract), * (multiply) and / (divide).

#include<stdio.h>#include<string.h>#include<conio.h>#include<math.h>#include<ctype.h>

float cal(float op1,char ch,float op2){

switch(ch){

case '+': return (op1+op2);case '-': return (op1-op2);case '*': return (op1*op2);case '/': return (op1/op2);case '^': return (pow(op1,op2));

}}

void main(){

float s[100],res,op1,op2;int i,top=-1;char pos[100],sym;clrscr();

printf("\nEnter the postfix expression..\n");scanf("%s",pos);

for(i=0;i<strlen(pos);i++){

sym=pos[i];

if(isdigit(sym)){

s[++top]=sym-'0';}

Page 4: DSLAB

else{

op2=s[top--];op1=s[top--];res=cal(op1,sym,op2);s[++top]=res;

}}

res=s[top--];printf("\n%s = %f",pos,res);

getch();}

/* OUTPUTEnter the postfix expression..23^6*9-45/78+/+23^6*9-45/78+/+ = 39.053333*/

4. Design, develop, and execute a program in C to simulate the working of a queue of integers using an array. Provide the following operations: a. Insert b. Delete c. Display

#include<stdio.h>#include<conio.h>

#define SIZE 5 /* Size of Queue */

Page 5: DSLAB

int Q[SIZE],f=0,r=-1; /* Global declarations */

Qinsert(int elem) { /* Function for Insert operation */ if( Qfull()) printf("\n\n Overflow!!!!\n\n"); else { ++r; Q[r]=elem; } }

int Qdelete() { /* Function for Delete operation */ int elem; if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");

return(-1); } else { elem=Q[f]; f=f+1; return(elem); } }

int Qfull() { /* Function to Check Queue Full */ if(r==SIZE-1) return 1; return 0; }

int Qempty() { /* Function to Check Queue Empty */ if(f > r) return 1; return 0; }

display() { /* Function to display status of Queue */ int i;

Page 6: DSLAB

if(Qempty()) printf(" \n Empty Queue\n"); else { printf("Front->"); for(i=f;i<=r;i++)

printf("%d ",Q[i]); printf("<-Rear"); } }

main() { /* Main Program */ int opn,elem; do { clrscr(); printf("\n ### Queue Operations ### \n\n"); printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n"); printf("\n Your option ? "); scanf("%d",&opn); switch(opn) { case 1: printf("\n\nRead the element to be Inserted ?");

scanf("%d",&elem); Qinsert(elem); break;

case 2: elem=Qdelete(); if( elem != -1)

printf("\n\nDeleted Element is %d \n",elem); break;

case 3: printf("\n\nStatus of Queue\n\n"); display(); break;

case 4: printf("\n\n Terminating \n\n"); break; default: printf("\n\nInvalid Option !!! Try Again !! \n\n");

break; } printf("\n\n\n\n Press a Key to Continue . . . "); getch(); }while(opn != 4); }

5. Design, develop, and execute a program in C++ based on the following requirements:An EMPLOYEE class is to contain the following data members and

Page 7: DSLAB

member functions: Data members: Employee_Number (an integer), Employee_Name (a string of characters), Basic_Salary (an integer) , All_Allowances (an integer), IT (an integer), Net_Salary (an integer).Member functions: to read the data of an employee, to calculate Net_Salary and to print the values of all the data members. (All_Allowances = 123% of Basic; Income Tax (IT) = 30% of the gross salary (= basic_Salary _ All_Allowance); Net_Salary = Basic_Salary + All_Allowances – IT)

#include<iostream.h>#include<conio.h>#define size 25

class EMPLOYEE //CLASS DEFINITION{

int emp_num;char emp_name[25];int basic, da , it, net_sal;

public:void getdata();void netsal();void display();

};

void EMPLOYEE :: getdata() //READING THE DATA{

cout<<"\nenter EMPLOYEE number:";cin>>emp_num;cout<<"enter EMPLOYEE name:";cin>>emp_name;cout<<"enter basic salary:";cin>>basic;

}

void EMPLOYEE :: netsal() //CALCULATING THE NET SALARY{

da=(1.23*basic);float gsal=basic+da;it=(.30*gsal);net_sal=gsal-it;

}

void EMPLOYEE::display() //DISPLAYING THE OUTPUT{

cout<<"\n\nEMPLOYEE number:"<<emp_num

Page 8: DSLAB

<<"\nEMPLOYEE name:"<<emp_name <<"\nnetsalary:"<<net_sal<<endl;

}

void main() //MAIN FUNCTION{

clrscr();EMPLOYEE obj[size];int n;

cout<<"\nenter number of EMPLOYEEs:";cin>>n;

for(int i=0;i<n;i++){

obj[i].getdata();obj[i].netsal();

}

for(i=0;i<n;i++) obj[i].display();

getch();}

6. Design, develop, and execute a program in C++ to create a class called STRING and implement the following operations. Display the results after every operation by overloading the operator <<. i. STRING s1 = “VTU” ii. STRING s2 = “BELGAUM” iii. STIRNG s3 = s1 + s2; (Use copy constructor)

#include<iostream.h>#include<conio.h>#include<string.h>

class STRING{

char str[40];

public:

Page 9: DSLAB

STRING(); // Default Constructor STRING(char s[]); // Constructor with parameter STRING(STRING &s); // Copy Constructor STRING operator +(STRING); friend ostream& operator <<(ostream&, STRING&);

};

STRING::STRING(){

str[0]='\0'; }

STRING::STRING( char s[]){

strcpy(str,s);}

STRING::STRING(STRING &s){

strcpy(str,s.str);}

STRING STRING :: operator +(STRING s1){

return(strcat(str,s1.str));}

ostream& operator <<(ostream& print,STRING &s1){ print<<s1.str; return print;}

void main(){ clrscr(); char name1[20],name2[20]; cout<<"Enter two Names as your Wish :"; cin>>name1>>name2; STRING s1(name1); //STRING s1="rohit"; cout<<"\nNAME1 : "<<s1;

Page 10: DSLAB

STRING s2(name2); //STRING s2="patil"; cout<<"\nNAME2 : "<<s2; STRING s3=(s1+s2); cout<<"\n\nNAME3 : "<<s3; getch();}

7. Design, develop, and execute a program in C++ to create a class called STACK using an array of integers and to implement the following operations by overloading the operators + and - : i. s1=s1 + element; where s1 is an object of the class STACK and element is an integer to be pushed on to top of the stack. ii. s1=s1- ; where s1 is an object of the class STACK and – operator pops off the top element. Handle the STACK Empty and STACK Full conditions. Also display the contents of the stack after each operation, by overloading the operator <<.

#include<iostream.h>#include<stdlib.h>#include<conio.h>

const int SIZE=5; //Stack size

//class declarationclass stack{

Page 11: DSLAB

int items[SIZE];int top;int full();int empty();public:

stack(){

top=-1;}stack operator--(int);friend stack operator+(stack s1,int elem);friend ostream &operator<<(ostream &os,stack &s1);

};

// checking for Stack overflow

int stack::full(){

if(top==SIZE-1)return 1;

elsereturn 0;

}

//Checking for stack under flow.

int stack::empty(){

if(top==-1)return 1;

elsereturn 0;

}

//function for element deletion from the stack

stack stack::operator--(int ){

if(empty()){

cout<<"Stack underflow\n";}else{

Page 12: DSLAB

cout<<"\nThe element deleted is :"<<items[top];stack t;t.top=--top;for(int i=0;i<=top;i++)

t.items[i]=items[i];}return *this;

}ostream &operator<<(ostream &os,stack &s1){

for(int i=s1.top;i>=0;i--)os<<s1.items[i]<<"\n";

return os;}

//function for element insertion on to the stackstack operator+(stack s1,int elem){

if(s1.full())cout<<"\nStack overflow\n";

elses1.items[++(s1.top)]=elem;

return s1;}/*Main function*/void main(){

stack s1;int choice,elem;clrscr();

for(;;){

cout<<"\n1:PUSH 2:POP 3:DISPLAY 4:EXIT\n"<<"enter your choice:";

cin>>choice;

switch(choice){

case 1:cout<<"Enter the element to be inserted:";cin>>elem;s1=s1+elem;break;

Page 13: DSLAB

case 2:s1=s1--;break;

case 3:cout <<"The contents of the stack are :\

n"<<s1;break;

case 4: exit(0);default:

cout <<"Invalid choice\n";getch();exit(0);

}}

}

OUTPUT

1:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:2Stack underflow1:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:1Enter the element to be inserted:201:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:1Enter the element to be inserted:451:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:1Enter the element to be inserted:511:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:1Enter the element to be inserted:621:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:1Enter the element to be inserted:771:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:3The contents of the stack are :77625145201:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:2

Page 14: DSLAB

The element deleted is :771:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:2The element deleted is :621:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:3The contents of the stack are :5145201:PUSH 2:POP 3:DISPLAY 4:EXITenter your choice:4

8. Design, develop, and execute a program in C++ to create a class called LIST (linked list) with member functions to insert an element at the front of the list as well as to delete an element from the front of the list. Demonstrate all the functions after creating a list object.

#include<stdlib.h>#include<iostream.h>#include<conio.h>

/* structure declaration*/struct node{

int info;node *next;

};

/* class declaration*/class list{

node *head;public:

/* constructor */list(){

head=NULL;}

/* Member function to insert an element to the linked list*/void insert(){

Page 15: DSLAB

int val;

cout<<"enter the element:";cin>>val;

node *newnode= new node;

newnode->info=val;newnode->next=head;head=newnode;

}

/*Member function to delete an element from the linked list*/

void remove(){

node *temp=head;

if(temp==NULL)cout<<"list is empty\n";

else{

cout<<"the item to be deleted is "<<temp->info<<endl;

head=head->next;delete temp;

}}

/*Member function to display the contents of the linked list*/

void display(){

node *temp=head;

while(temp!=NULL){

cout<<temp->info<<"-->";temp=temp->next;

}

cout<<"NULL\n";}

};

Page 16: DSLAB

/* main program */

void main(){

int choice;clrscr();

list link;do{

cout<<"\n\t MENU\n1:insert at front 2:delete at front"<<"\n3:display 4:exit\n";cout<<"enter your choice:";cin>>choice;

switch(choice){

case 1:link.insert();break;

case 2:link.remove();break;

case 3:link.display();break;

case 4:return;

}//getch();

}while(choice<4);}

OUTPUT

MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:2list is emptyMENU1:insert at front 2:delete at front3:display 4:exitenter your choice:3NULLMENU

Page 17: DSLAB

1:insert at front 2:delete at front3:display 4:exitenter your choice:1enter the element:10MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:1enter the element:20MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:1enter the element:30MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:1enter the element:40MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:1enter the element:50MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:350-->40-->30-->20-->10-->NULLMENU1:insert at front 2:delete at front3:display 4:exitenter your choice:2the item to be deleted is 50MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:2the item to be deleted is 40MENU1:insert at front 2:delete at front3:display 4:exitenter your choice:330-->20-->10-->NULL

Page 18: DSLAB

9. Design, develop, and execute a program in C to read a sparse matrix of integer values and to search the sparse matrix for an element specified by the user. Print the result of the search appropriately. Use the triple <row, column, value> to represent an element in the sparse matrix.

10. Design, develop, and execute a program in C to create a max heap of integers by accepting one element at a time and by inserting it immediately in to the heap. Use the array representation for the heap. Display the array at the end of insertion phase.

11. Design, develop, and execute a program in C to implement a doubly linked list where each node consists of integers. The program should support the following operations: i. Create a doubly linked list by adding each node at the front. ii. Insert a new node to the left of the node whose key value is read as an input. iii. Delete the node of a given data if it is found, otherwise display appropriate message. iv. Display the contents of the list. (Note: Only either (a,b and d) or (a, c and d) may be asked in the examination)

#include<stdio.h>#include<conio.h>#include<stdlib.h>

void display();

struct node{

int info;struct node*next;struct node*prev;

};

struct node*f=NULL,*temp,*cur;

void insert(){

temp=(struct node *)malloc(sizeof(struct node));

if(temp==NULL)printf("\n no memory\n");

Page 19: DSLAB

else{

printf("\n enter the element\n");scanf("%d",&temp->info);temp->next=temp->prev=NULL;

}}

void in_front(){

printf("\n inserting at the front of the list\n");insert();

if(f==NULL){

f=temp;return;

}

temp->next=f;f->prev=temp;f=temp;display();

return;}

void ins_key(){

int n;

printf("\n enter element before which node is to be inserted\n");scanf("%d",&n);

insert();

cur=f;

if(cur->info==n){

temp->next=cur;cur->prev=temp;f=temp;display();return;

}

Page 20: DSLAB

while(cur->info!=n&&cur!=NULL)cur=cur->next;

if(cur==NULL){

printf("\n element not found\n");free(cur);return;

}

cur->prev->next=temp;temp->prev=cur->prev;temp->next=cur;cur->prev=temp;display();return;

}

void del_id(){

int n;

printf("\n enter the element to be deleted\n");scanf("%d",&n);

cur=f;

if(cur->info==n){

printf("\n node containing element %d is deleted\n",cur->info);

if(f->next==NULL){

free(cur);f=NULL;

}else{

f=f->next;f->prev=NULL;free(cur);

}display();return;

}

Page 21: DSLAB

while(cur->info!=n&&cur!=NULL)cur=cur->next;

if(cur==NULL){

printf("\n element not found\n");free(cur);return;

}

cur->prev->next=cur->next;cur->next->prev=cur->prev;display();return;

}

void display(){

struct node *p;

if(f==NULL){

printf("\n list is empty\n");return;

}p=f;printf("\n list\n");

while(p!=NULL){

printf(" %d-",p->info);p=p->next;

}return;

}

void main(){

int ch=1;clrscr();

while(ch){

printf("\n enter your choice\n");printf("\n 1:insert at front \n 2:insert to left of given data");printf("\n 3:delete \n 4:display \n 5:exit\n");

Page 22: DSLAB

scanf("%d",&ch);

switch(ch){

case 1:in_front();break;

case 2:ins_key();break;

case 3:del_id();break;

case 4:display();break;

case 5:exit(0);default:printf("invalid choice\n");

break;}

}getch();

}

/* OUTPUTenter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exitinserting at the front of the listenter the element10enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit2enter element before which node is to be inserted20enter the element30element not foundenter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit2enter element before which node is to be inserted10enter the element20list20

Page 23: DSLAB

10enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit4list2010enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit3enter element to be deleted10list20enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit3enter element to be deleted10element not foundenter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit5*/

12. Design, develop, and execute a program in C++ to create a class called DATE with methods to accept two valid dates in the form dd/mm/yy and to implement the following operations by overloading the operators + and -. After every operation the results are to be displayed by overloading the operator <<.i. no_of_days = d1 – d2; where d1 and d2 are DATE objects, d1 >=d2 and no_of_days is an integer.ii. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer.

Page 24: DSLAB

#include <iostream.h>#include <conio.h>

class date {

int dd,mm,yy;

public:void read() {

cin>>dd>>mm>>yy; }int operator - (date);date operator + (int);friend ostream &operator<<(ostream &put,date &d);

}; int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int date::operator-(date d2) { date res; int nod,noly=0,temp=d2.yy;

for (temp;temp<yy;temp++) if(temp%4==0)

noly++;

temp=yy; for (temp;temp>d2.yy;temp--) if(temp%4==0)

noly++;

res.dd=dd-d2.dd; if(res.dd<0) {

res.dd=dd+a[mm];res.mm=mm--;

} res.mm=mm-d2.mm; if(res.mm<0) {

res.mm=mm+12;res.yy=yy--;

} res.yy=yy-d2.yy;

Page 25: DSLAB

if(yy<0)return -1;

nod=res.dd+(res.yy*365);

int months=d2.mm; for(int i=1;i<=res.mm;i++)

nod+=a[months++];

nod+=noly; return nod; }

date date::operator+(int ndays) { date d; d.dd=dd; d.mm=mm; d.yy=yy; for(int i=1;i<=ndays;i++)

{ d.dd++; if(d.yy%4==0) {

if(d.dd>b[d.mm]) { d.dd=1; d.mm++; }

} else { if(d.dd>a[d.mm])

{ d.dd=1; d.mm++; }

} if(d.mm>12)

{ d.mm=1; d.yy++;}

} return d;

} ostream &operator<<(ostream &print,date &d) {

print<<d.dd<<"/"<<d.mm<<"/"<<d.yy;return print;

}

Page 26: DSLAB

void main() {

date d1,d2; int ndays; clrscr(); cout<<"Enter two dates, date1 should be greater than date2\n\

n"; cout<<"Enter Date1 (dd mm yyyy)::"; d1.read(); cout<<"Enter Date2 (dd mm yyyy)::"; d2.read(); clrscr(); ndays=d1-d2; cout<<"\n\nNumber of Days between the\n\n\t\tDate1 ::

“<<d1<<"\nand" <<"\t\tDate2 :: " <<d2<<" is =>"<<ndays; cout<<"\n\n\nEnter no. of days to be added to the Date1 ::"; cin>>ndays; d1=d1+ndays; cout<<"\n\n\t\tDate1 after adding "<<ndays<<" days

is ::"<<d1;

getch(); }

13. Design, develop, and execute a program in C++ to create a class called OCTAL, which has the characteristics of an octal number. Implement the following operations by writing an appropriate constructor and an overloaded operator +. i. OCTAL h = x ; where x is an integer ii. int y = h + k ; where h is an OCTAL object and k is an integer.Display the OCTAL result by overloading the operator <<. Also display the values of h and y.

#include<iostream.h>#include<conio.h>#include<math.h>class OCTAL{

int oct[15] , count; public:

OCTAL(int); int operator +(int);

Page 27: DSLAB

friend ostream& operator <<(ostream& ,OCTAL)};

OCTAL :: OCTAL(int x){ int i=0,rem,a[15]; while(x!=0)

{ rem=x%8; x=x/8; a[i++]=rem;

} count=i;

int n=count-1;

for(i=0;i<count;i++) { oct[i]=a[n]; n--; }}

int OCTAL :: operator +(int k){ int x=0;int j=count-1; for(int i=0;i<count;i++) {

x=x+oct[j]*pow(8,i);j--;

} return(x+k);}

ostream& operator <<(ostream& print,OCTAL o){ for(int i=0;i<o.count;i++) print<<o.oct[i]; return print;}

void main(){ clrscr(); int x,k,y=0;

Page 28: DSLAB

cout<<"\nEnter the Integer value of x (In Decimal) : "; cin>>x;

OCTAL h=OCTAL(x);

cout<<"The corresponding Octal Equivalent of "<< x <<" is : "<<h;

cout<<"\n\nEnter the integer to be added to the Octal value : "; cin>>k;

y=h+k; cout<<"\t\t\t"<<h<<" (Octal) + "<<k<<" (Decimal) = "<<y<<" (Decimal)";

getch();}

14. Design, develop, and execute a program in C++ to create a class called BIN_TREE that represents a Binary Tree, with member functions to perform inorder, preorder and postorder traversals. Create a BIN_TREE object and demonstrate the traversals.

#include <iostream.h>#include <stdlib.h>

class node{

public: int info;node *left;node *right;

};

typedef node *NODE;

/* declaring a class */class BIN_TREE{

Page 29: DSLAB

public: node *root;

public: BIN_TREE(){

root=NULL;}

/*function to get inorder expression*/void inorder(node *root){

if(root!=NULL){

inorder(root->left);cout<<root->info<<"\t";inorder(root->right);

}}

/*function to get preorder expression*/void preorder(node *root){

if(root!=NULL){

cout<<root->info<<"\t";preorder(root->left);preorder(root->right);

}}

/*function to get preorder expression*/void postorder(node *root){

if(root!=NULL){

postorder(root->left);postorder(root->right);cout<<root->info<<"\t";

}}

/*function to insert elements into binary tree*/

NODE insert(int item,node *root){

node *temp,*prev,*cur;

Page 30: DSLAB

temp=new node;temp->info=item;temp->left=NULL;temp->right=NULL;

if(root==NULL)return temp;

prev=NULL;cur=root;

while(cur!=NULL){

prev=cur;if(item==cur->info){

cout<<"Duplicate elements are not allowed"<<endl;delete(temp);return root;

}cur=(item<cur->info)?cur->left:cur->right;

}

if(item<prev->info) prev->left=temp;

else prev->right=temp;

return root;}};

/* main function */void main(){

BIN_TREE bin;int choice,item;

for(;;){

cout<<"1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit"<<endl;

cout<<"Enter choice:";cin>>choice;switch(choice){

case 1: cout<<"Enter item:";

Page 31: DSLAB

cin>>item;bin.root=bin.insert(item,bin.root);break;

case 2: if(bin.root==NULL)cout<<"Tree is empty"<<endl;else{

cout<<"Inorder Traveresal:"<<endl;bin.inorder(bin.root);cout<<endl;

}break;

case 3: if(bin.root==NULL)cout<<"Tree is empty"<<endl;

else{

cout<<"Preorder Traveresal:"<<endl;bin.preorder(bin.root);cout<<endl;

}break;

case 4: if(bin.root==NULL)cout<<"Tree is empty"<<endl;

else{

cout<<"Postorder Traveresal:"<<endl;bin.postorder(bin.root);cout<<endl;

}break;

default: exit(0);}

}}

OUTPUT1.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:1Enter item:501.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:1Enter item:251.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:1Enter item:231.Insert 2.Inorder 3.Preorder 4.Postorder 5.Exit

Page 32: DSLAB

Enter choice:1Enter item:341.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:1Enter item:601.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:1Enter item:561.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:1Enter item:701.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:2Inorder Traveresal:23 25 34 50 56 60 701.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:3Preorder Traveresal:50 25 23 34 60 56 701.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:4Postorder Traveresal:23 34 25 56 70 60 501.Insert 2.Inorder 3.Preorder 4.Postorder 5.ExitEnter choice:5Press any key to continue