Академический Документы
Профессиональный Документы
Культура Документы
*/
#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::display()
{
if(root==null)
{
cout<<"\ntree does not exists..\n";
return;
}
cout<<"\nthe tree nodes are : ";
intrav(root);
cout<<"\n";
}
preorder traversal. : 10 5 3 6 12 11
inorder traversal. : 3 5 6 10 11 12
postorder traversal. : 3 6 5 11 12 10
*/
plain text attachment [ scan and save to computer | save to yahoo! briefcase ]
#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::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;
}
enter a integer : 20