This is a program..to create and search for Binary Tree..
An error is occoured for class tree ..as " Too many types in declaration".
I would be glad if anyone can tell me to solve this problem.
Thanks for all the support.
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *right;
node *left;
}
class tree
{
private:
node *start,*cur,*temp;
public:
tree()
{
start=NULL;
}
void create(int x);
void search(int x);
};
void main()
{
tree obj;
cout<<"\n1: Create new Binary tree.\n";
cout<<"2: Search values from Binary tree.\n";
cout<<"Enter you Choice:";
int s,opt,val;
cin>>opt;
if(opt==1)
{
cout<<"\n\nEnter 10 values.\n";
for(int i=0;i<10;i++)
{
cin>>val;
obj.create(val);
}
}
else
{
cout<<"\n\nEnter value to search?";
cin>>s;
obj.search(s);
}
}
void tree::search(int x)
{
if(start==NULL)
{ cout<<"Tree is empty.."; getch(); return;
}
else
{
cur=start;
while(cur!=NULL)
{
if(cur->data==x)
{
cout<<"\nData found.."; getch(); return;
}
if(cur->data<x)
cur=cur->right;
else
cur=cur->left;
}
if(cur==NULL)
{
cout<<"\nValue not found";
getch();
}
}
}
void tree::create(int x)
{
if(start==NULL)
{
start=new node;
start->data=x;
start->right=NULL;
start->left=NULL;
}
else
{
node *parent;
cur=start;
while(cur!=NULL)
{
if(cur->data==x)
{
cout<<"\nValue already exist..";
return;
}
if(cur->data<x)
{ parent=cur; cur=cur->right;
}
else
{ parent=cur; cur=cur->left;
}
}
temp=new node;
temp->data=x;
temp->right=NULL;
temp->left=NULL;
if(parent==NULL)
parent=temp;
else if(parent->data>x)
parent->left=temp;
else
parent->right=temp;
}
}