You are on page 1of 19

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>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node
{
int info;
node *llink;
node *rlink;
};

class tree
{
node *root;
public:
tree()
{
root=NULL;
}
void inorder(node *);
void preorder(node *);
void postorder(node *);
void display(int);
void create(int);
};

void tree::display(int choice)
{
switch(choice)
{
case 2:if(root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Preorder traversal is: "<<endl;
preorder(root);
}
break;
case 3:if(root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Inorder traversal is: "<<endl;
inorder(root);
}
break;
case 4:if(root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Postorder traversal is: "<<endl;
postorder(root);
}
break;
}
}

void tree::inorder (node *root)
{
if (root!=NULL)
{
inorder(root->llink);
cout<<root->info<<endl;
inorder(root->rlink);
}
}

void tree::preorder(node *root)
{
if(root!=NULL)
{
cout<<root->info<<endl;
preorder(root->llink);
preorder(root->rlink);
}
}

void tree::postorder(node *root)
{
if (root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
cout<<root->info<<endl;
}
}

void tree::create(int item)
{
node *temp, *cur,*prev;
temp=new node;
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
{
root=temp;
return;
}
cur=root;
prev=NULL;
while(cur!=NULL)
{
prev=cur;
cout<<cur->info;
if(item<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
}

int main()
{
tree ob;
int item,choice;
// clrscr();
for( ; ; )
{
cout<<"1.Insert 2.Preorder 3.Inorder 4.Postorder 5.Exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:cout<<"Enter the item: ";
cin>>item;
ob.create(item);
break;
case 2:
case 3:
case 4:
ob.display(choice);
break;
default:exit(0);
}
}
}







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.


#include <iostream>
using namespace std;
class date
{
int dd,mm,yy;
int a[13];
long double dateno,fday;
int getno( ) ;
public:
date( )
{
a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;
a[4]=a[6]=a[9]=a[11]=30;
a[2]=28;
dd=mm=yy=1;
}
int getdate( );
friend ostream&operator<<(ostream&,date&);
long double operator-(date &);
date operator+(long double);
};
int date::getdate()
{
cout<<"enter date:\n";
cin>>dd;
cout<<"month is:\n";

cin>>mm;
cout<<"yy is:\n";
cin>>yy;
while((yy%4!=0&&dd>a[mm])||(yy%4==0&&mm==2&&dd>29)||(dd<=0)||(dd>31)||(mm>12)||(
mm<=0))
{
cout<<"invalid entry!!";
getdate( );
}
getno( );
}
int date::getno()
{
int m=1;
dateno=(long double) (yy-1)*365+(yy-1)/4;
while(m!=mm)
{
dateno+=a[m];
if(yy%4==0&&m==2)
{
dateno++;
}

m++;
}
}
ostream&operator<<(ostream&out,date&d1)
{
cout<<d1.dd<<"/"<<d1.mm<<"/ "<<d1.yy;
return out;
}
long double date::operator-(date &b)
{
return(dateno-b.dateno);
}
date date::operator+(long double b)
{
for(int i=1;i<=b;i++)
{
dd++;
if(dd>a[mm])
{
mm++;
dd=1;
}
if(mm>12)
{
yy++;
mm=1;
}
if(yy%4==0)
a[2]=29;
}
return (*this);
}
int main()
{
date d1,d2,d3;
d1.getdate();
cout<<"d1="<<d1;
d2.getdate();
cout<<"d2="<<d2;
long double s;
s=d1-d2;
cout<<"the difference between the two date is =\n";
cout<<s;
cout<<"enter the number of days to be added to the date:\n"<<d1;
cin>>s;
d3=d1+s;
cout<<"new date is :\n"<<d3;
}
Using circular representation for a polynomial, design, develop, and execute a program in
C to accept two polynomials, add them, and then print the resulting polynomial.

#include<stdio.h>
#include<conio.h>
struct polynode
{
int coeff,exp;
struct polynode *link;
};
typedef struct polynode pnode;

// function to attach to polynomial equation
pnode *attach(int coeff,int exp,pnode *head)
{
pnode *temp,*curr;
temp=(pnode *)malloc(sizeof(pnode));
temp->coeff=coeff;
temp->exp=exp;
curr=head->link;
while(curr->link!=head)
curr=curr->link;
curr->link=temp;
temp->link=head;
return head;
}
pnode *readpoly(pnode *first)
{
int i,ch,coeff,exp;
for(i=0;i<first->coeff;i++)
{
printf("enter the %d term coeff,expo : ",i+1);
scanf("%d%d",&coeff,&exp);
first=attach(coeff,exp,first);
}
return first;
}


//function to display the polynomial equation
int display(pnode *head)
{
pnode *temp;
if(head->link==head)
{
printf("NULL-empty\n");
return;
}
temp=head->link;
while(temp!=head)
{
if(temp->coeff<0)
printf("%dx%d ",temp->coeff,temp->exp);
else
printf("+%dx%d ",temp->coeff,temp->exp);
temp=temp->link;
}
}

//function to add to two polynomial equation
pnode * polyadd(pnode *head1,pnode *head2,pnode *head3)
{
int coeff;
pnode *poly1,*poly2;
poly1=head1->link;
poly2=head2->link;
while(poly1!=head1 && poly2!=head2)
{
if(poly1->exp==poly2->exp)
{
coeff=poly1->coeff+poly2->coeff;
if(coeff!=0)
head3=attach(coeff,poly1->exp,head3);
poly1=poly1->link;
poly2=poly2->link;
}
else if(poly1->exp > poly2->exp)
{
head3=attach(poly1->coeff,poly1->exp,head3);
poly1=poly1->link;
}
else
{
head3=attach(poly2->coeff,poly2->exp,head3);
poly2=poly2->link;
}
}
while(poly1!=head1)
{
head3=attach(poly1->coeff,poly1->exp,head3);
poly1=poly1->link;
}
while(poly2!=head2)
{
head3=attach(poly2->coeff,poly2->exp,head3);
poly2=poly2->link;
}
return head3;
}

// main program
int main()
{
pnode *head1,*head2,*head3;

head1=(pnode *)malloc(sizeof(pnode));
head2=(pnode *)malloc(sizeof(pnode));
head3=(pnode *)malloc(sizeof(pnode));
head1->link=head1;
head2->link=head2;
head3->link=head3;
printf("enter the number of terms in polynomial equation A :");
scanf("%d",&head1->coeff);
head1=readpoly(head1);
printf("\nenter the number of terms in polynomial equation B :");
scanf("%d",&head2->coeff);
head2=readpoly(head2);
head3=polyadd(head1,head2,head3);
printf("the polynomial equation A =");
display(head1);
printf("\n the polynomial equation B =");
display(head2);
printf("\n the resulatant polynomial C=");
display(head3);

}

You might also like