1 Replies - 273 Views - Last Post: 12 October 2012 - 10:00 PM Rate Topic: -----

#1 senthilguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-October 12

Is this Correct Binary Search Tree?

Posted 12 October 2012 - 08:30 PM

Is this Correct Binary Search Tree?

#include<iostream.h>
#include<stdio.h>
struct node
{
int info;
struct node *lchild,*rchild;
}*temp,*ptr,*root,*t;
class tree
{
node *r;
public:void create();
int insert(node *);
void in(node *);
void pre(node *);
void post(node *);
}item;
main()
{
int i=0;
do
{
cout<<"\n[1] Create Tree\n[2] Inorder\n[3] Preorder\n[4] Postorder\n[5] Exit\n";
cin>>i;
switch(i)
{
case 1:
item.create();
break;
case 2:
cout<<"Inorder \t: ";
item.in(root);
cout<<"\n";
break;
case 3:
cout<<"Preorder \t: ";
item.pre(root);
cout<<"\n";
break;
case 4:
cout<<"Postorder \t: ";
item.post(root);
cout<<"\n";
break;
case 5:
break;
default:
cout<<"Wrong Option";
}
}while(i!=5);
}
void tree :: create()
{
node *temp=new node;
temp->lchild=NULL;
temp->rchild=NULL;
cout<<"Enter the data : ";
cin>>temp->info;
root=temp;
insert(temp);
}
int tree :: insert(node *link)
{
char ch,e;
cout<<"Left Child for "<<link->info<<":\t Y for yes: ";
cin>>ch;
if(ch=='y')
{
node *temp=new node;
temp->lchild=NULL;
temp->rchild=NULL;
cout<<"Data \t: ";
cin>>temp->info;
link->lchild=temp;
insert(link->lchild);
}
ch='n';
cout<<"Right Child for "<<link->info<<":\t Y for yes : ";
cin>>ch;
if(ch=='y')
{
node *temp=new node;
temp->lchild=NULL;
temp->rchild=NULL;
cout<<"Data \t: ";
cin>>temp->info;
link->rchild=temp;
insert(link->rchild);
}
}
void tree :: in(node *rt)
{
if(rt!=NULL)
{
in(rt->lchild);
cout<<rt->info<<" ";
in(rt->rchild);
}
}
void tree::pre(node *rt)
{
if(rt!=NULL)
{
cout<<rt->info<<" ";
pre(rt->lchild);
pre(rt->rchild);
}
}
void tree::post(node *rt)
{
if(rt!=NULL)
{
post(rt->lchild);
post(rt->rchild);
cout<<rt->info<<" ";
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Is this Correct Binary Search Tree?

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Is this Correct Binary Search Tree?

Posted 12 October 2012 - 10:00 PM

Your code formatting is terrifying
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1