Community Sites
Create your own community website and start earning today !
It's Free !
 
Communities Members BookmarksPolls Fresher Jobs Strange Photos Academic Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Binary search tree using c++


Posted Date: 19 May 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology

Posted By: sharu       Member Level: Gold
Rating:     Points: 1



//BINARY SEARCH TREE

#include
#include
#include

struct node
{
int data;
struct node *left,*right;
};
node *start;

class tree
{
public:
tree();
void create(node *start,int a);
void preorder(node *current);
void inorder(node *current);
void postorder(node *current);
void delet(node *);
};

tree::tree()
{
start=NULL;
}
void tree::create(node *root,int a)
{
node *nd;
if(root ==NULL)
{
nd=new node;
nd->data=a;
nd->left=NULL;
nd->right=NULL;
start=nd;
}
else if(adata)
{
if(root->left==NULL)
{
nd=new node;
nd->data=a;
nd->left=NULL;
nd->right=NULL;
root->left=nd;
}
else
create(root->left,a);
}
else if(a>root->data)
{
if(root->right==NULL)
{
nd=new node;
nd->data=a;
nd->left=NULL;
nd->right=NULL;
root->right=nd;
}
else
create(root->right,a);
}
}

void tree::inorder(node *current)
{
if(current!=NULL)
{
inorder(current->left);
cout<data<<"\t";
inorder(current->right);
}
}

void tree::preorder(node *current)
{
if(current!=NULL)
{
cout<data<<"\t";
preorder(current->left);
preorder(current->right);
}
}

void tree::postorder(node *current)
{
if(current!=NULL)
{
postorder(current->left);
postorder(current->right);
cout<data<<"\t";
}
}
void tree::delet(node *start)
{
node *ptr,*t,*rp,*q,*s;
ptr=start;
q=NULL;int x;
cout<<"enter the element to be deleted";
cin>>x;
while(ptr!=NULL)
{
if(xdata)
{
q=ptr;
ptr=ptr->left;
}
else
if(x>ptr->data)
{
q=ptr;
ptr=ptr->right;
}
else break;
}

if(ptr==NULL)
{
cout<<"there is no element to delete";
return;
}
if(ptr->left==NULL)
rp=ptr->right;
else
{
t=ptr;
rp=ptr->right;
while(s!=NULL)
{
t=rp;
rp=s;
s=rp->left;
}
if(t!=ptr)
{
t->left=rp->right;
rp->right=ptr->right;
}
rp->left=ptr->left;
}
if(q==NULL)
start=rp;
else
if(ptr==q->left)
q->left=rp;
else
q->right=rp;
if(ptr->right==NULL)

q->left=ptr->left;
return;
}

void main()
{
tree t;
int i,x,z,n,a,s;char c;
clrscr();
cout<<"Enter -999 for stop creation\n";
cout<<"\nEnter the elements";
cin>>a;
while(a!=-999)
{
t.create(start,a);
cin>>a;
}
cout<<"the root is"<data;
while(1)
{
cout<<"\n\t1.\tpreorder\n2.\tinorder\n3.\tpostorder\n4.\tdeletion\n5.\tinsertion\n6.\texit";
cout<<"\nenter your choice";
cin>>x;
switch(x)
{
case 1 :t.preorder(start); break;
case 2 :t.inorder(start);break;
case 3 :t. postorder(start);break;
case 4 :t.delet(start);
break;
case 5 :cout<<"\nenter elmt to be inserted" ;
cin>>a;
t.create(start,a);
break;
case 6:exit(0);
}
}
getch();
}




OUTPUT

Enter -999 for stop creation
Enter the elements
55
11
22
8
33
6
7
-999
the root is55
1. preorder
2. inorder
3. postorder
4. deletion
5. insertion
6. exit
Enter your choice 1
55 11 8 6 7 22 33
1. preorder
2. inorder
3. postorder
4. deletion
5. insertion
6. exit
Enter your choice 4
Enter the element to be deleted 22
1. preorder
2. inorder
3. postorder
4. deletion
5. insertion
6. exit

enter your choice1
55 11 8 6 7 33
1. preorder
2. inorder
3. postorder
4. deletion
5. insertion
6. exit
Enter your choice5
Enter elmt to be inserted 2
enter your choice6




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Show ur name on top of Internet Explorer
Previous Resource: calculator programme in c
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
Related Resources



Watch TV Channels
  • Watch Asianet TV online
  • Kairali TV in Internet
  • Surya TV online
  • Amritha TV Channel

  • Contact Us    Privacy Policy    Terms Of Use   

    SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.