Вы находитесь на странице: 1из 7

* program 14: binary search tree.

*/

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

class bin_tree
{
public:
int info;
bin_tree *leftson;
bin_tree *rightson;
void add_nodes();
void traverse_tree();
void pretrav(bin_tree *);
void intrav(bin_tree *);
void posttrav(bin_tree *);
void display();
}*root;

int main()
{
int choice;
bin_tree t;
root=null;
while(1)
{
cout<<"\n---------------- menu -------------\n"
<<"1.add node.\n2.traverse.\n3.display.\n4.exit.\n"
<<"-----------------------------------\n"
<<"\nenter your choice : ";
cin>>choice;
switch(choice)
{
case 1: t.add_nodes();
break;

case 2: t.traverse_tree();
break;

case 3: t.display();
break;

case 4: exit(0);
}
}
return(0);
}

void bin_tree::add_nodes()
{
int val;
bin_tree *temp,*p,*q;
while(1)
{
cout<<"\nenter the info value [-99 to stop]: ";
cin>>val;
if(val==-99)
return;
temp=new bin_tree;
temp->info=val;
if(root==null)
{
temp->leftson=null;
temp->rightson=null;
root=temp;
}
else
{
p=q=root;
while((val!=p->info)&&(q!=null))
{
p=q;
if(val<p->info)
q=p->leftson;
else
q=p->rightson;
}
if(val==p->info)
cout<<"\n"<<val<<" node already exists...\n";
else if(val<p->info)
{
p->leftson=temp;
temp->leftson=temp->rightson=null;
}
else
{
p->rightson=temp;
temp->leftson=temp->rightson=null;
}
}
}
}

void bin_tree::traverse_tree()
{
bin_tree *in,*pre,*post;
if(root==null)
{
cout<<"\ntree does not exists..\n";
return;
}
in=pre=post=root;
cout<<"\npreorder traversal. : ";
pretrav(pre);
cout<<"\n";
cout<<"\ninorder traversal. : ";
intrav(in);
cout<<"\n";
cout<<"\npostorder traversal. : ";
posttrav(post);
cout<<"\n";
}

void bin_tree::pretrav(bin_tree *tree)


{
if(tree!=null)
{
cout<<tree->info<<" ";
pretrav(tree->leftson);
pretrav(tree->rightson);
}
}

void bin_tree::intrav(bin_tree *tree)


{
if(tree!=null)
{
intrav(tree->leftson);
cout<<tree->info<<" ";
intrav(tree->rightson);
}
}

void bin_tree::posttrav(bin_tree *tree)


{
if(tree!=null)
{
posttrav(tree->leftson);
posttrav(tree->rightson);
cout<<tree->info<<" ";
}
}

void bin_tree::display()
{
if(root==null)
{
cout<<"\ntree does not exists..\n";
return;
}
cout<<"\nthe tree nodes are : ";
intrav(root);
cout<<"\n";
}

/* ---------- output ----------

---------------- menu -------------


1.add node.
2.traverse.
3.display.
4.exit.
-----------------------------------

enter your choice : 1

enter the info value [-99 to stop]: 10

enter the info value [-99 to stop]: 5

enter the info value [-99 to stop]: 3


enter the info value [-99 to stop]: 6

enter the info value [-99 to stop]: 12

enter the info value [-99 to stop]: 11

enter the info value [-99 to stop]: -99

---------------- menu -------------


1.add node.
2.traverse.
3.display.
4.exit.
-----------------------------------

enter your choice : 3

the tree nodes are : 3 5 6 10 11 12

---------------- menu -------------


1.add node.
2.traverse.
3.display.
4.exit.
-----------------------------------

enter your choice : 2

preorder traversal. : 10 5 3 6 12 11

inorder traversal. : 3 5 6 10 11 12

postorder traversal. : 3 6 5 11 12 10

---------------- menu -------------


1.add node.
2.traverse.
3.display.
4.exit.
-----------------------------------

enter your choice : 4

*/
plain text attachment [ scan and save to computer | save to yahoo! briefcase ]

/* program 3: program to create complex class and overloaded add


function */

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

class complex
{
int real,imag;
public :
void read();
friend complex add(int,complex);
friend complex add(complex,complex);
friend ostream& operator <<(ostream&,complex);
//void display();
};

void complex ::read ()


{
cout<<"enter real part : ";
cin>>real;
cout<<"\nenter imag. part : ";
cin>>imag;
cout<<endl;
}

complex add(int a,complex s)


{
complex temp;
temp.real=a+s.real;
temp.imag=s.imag;
return temp;
}

complex add(complex s1,complex s2)


{
complex temp;
temp.real=s1.real+s2.real;
temp.imag=s1.imag+s2.imag;
return temp;
}

/*void complex::display()
{
if(imag>0)
cout<<real<<"+i"<<imag;
else
cout<<real<<"-i"<<abs(imag);
}*/
ostream& operator <<(ostream &os,complex s)
{
os<<s.real;
s.imag<0?os<<"-i":os<<"+i";
os<<abs(s.imag);
return os;
}

int main()
{
complex s1,s2,s3;
int a,ch;
while(1)
{
cout<<"1. add integer to a complex no.\n"
<<"2. add two complex numbers.\n"
<<"3. exit.\n"
<<"---------------------------------\n"
<<"enter your chice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nenter a complex no...\n";
s1.read();
cout<<"\nenter a integer : ";
cin>>a;
s3=add(a,s1);
cout<<"\nsum of "<<a<<" and "<<s1
<<" is : "<<s3<<"\n\n";

break;

case 2:
cout<<"\n\nenter two cmplex numbers :\n";
s1.read();
s2.read();
s3=add(s1,s2);
cout<<"\n\nsum of "<<s1<<" and "
<<s2<<" is : "<<s3<<"\n\n";
break;

case 3: exit(0);
}
}
return 0;
}

/* ---------- output ----------

1. add integer to a complex no.


2. add two complex numbers.
3. exit.
---------------------------------
enter your chice : 1

enter a complex no...

enter real part : 10

enter imag. part : 5

enter a integer : 20

sum of 20 and 10+i5 is : 30+i5

1. add integer to a complex no.


2. add two complex numbers.
3. exit.
---------------------------------
enter your choice : 2
enter two cmplex numbers :

enter real part : 10

enter imag. part : 5

enter real part : 20

enter imag. part : 5

sum of 10+i5 and 20+i5 is : 30+i10

1. add integer to a complex no.


2. add two complex numbers.
3. exit.
---------------------------------
enter your choice : 3

Вам также может понравиться