Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,916 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,735 people online right now. Registration is fast and FREE... Join Now!




binary search trees

 
Reply to this topicStart new topic

binary search trees

vijjuchinnu
13 Mar, 2008 - 07:52 PM
Post #1

New D.I.C Head
*

Joined: 12 Mar, 2008
Posts: 0

I dont know why it is not running....................please check it out

CODE

#define YES 1
#define NO 0
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
class tree
{
    private:
struct leaf
{
     int data;
     leaf *l;
     leaf *r;
};
struct leaf *p;

    public:


void findparent(int n,int &found,leaf* &parent);
void findfordel(int n,int &found,leaf *&parent,leaf* &x);
void add(int n);

void del(int n);
};







void tree::findparent(int n,int &found,leaf *&parent)
{
    leaf *q;
    found=NO;
     parent=NULL;

    if(p==NULL)
return;

    q=p;
    while(q!=NULL)
    {
if(q->data==n)
{
     found=YES;
     return;
}
if(q->data>n)
{
     parent=q;
     q=q->l;
}
else
{
     parent=q;
     q=q->r;
}
    }
}

void tree::add(int n)
{
    int found;
    leaf *t,*parent;
    findparent(n,found,parent);
    if(found==YES)
cout<< "\nSuch a Node Exists"<<endl;
    else
    {
t=new leaf;
t->data=n;
t->l=NULL;
t->r=NULL;

if(parent==NULL)
     p=t;
else
     parent->data > n ? parent->l=t : parent->r=t;
    }
}




void tree::findfordel(int n,int &found,leaf *&parent,leaf *&x)
{
    leaf *q;
    found=0;
    parent=NULL;
    if(p==NULL)
return;

    q=p;
    while(q!=NULL)
    {
if(q->data==n)
{
     found=1;
     x=q;
     return;
}
if(q->data>n)
{
     parent=q;
     q=q->l;
}
else
{
     parent=q;
     q=q->r;
}
    }
}




void tree::del(int num)
{
    leaf *parent,*x,*xsucc;
    int found;

    // If EMPTY TREE
    if(p==NULL)
    {
cout<<"\nTree is Empty";
return;
    }
    parent=x=NULL;
    findfordel(num,found,parent,x);
    if(found==0)
    {
cout<<"\nNode to be deleted NOT FOUND";
return;
    }

    // If the node to be deleted has 2 leaves
    if(x->l != NULL && x->r != NULL)
    {
parent=x;
xsucc=x->r;

while(xsucc->l != NULL)
{
     parent=xsucc;
     xsucc=xsucc->l;
}
x->data=xsucc->data;
x=xsucc;
    }

    // if the node to be deleted has no child
    if(x->l == NULL && x->r == NULL)
    {
if(parent->r == x)
     parent->r=NULL;
else
     parent->l=NULL;

delete x;
return;
    }

    // if node has only right leaf
    if(x->l == NULL && x->r != NULL )
    {
if(parent->l == x)
     parent->l=x->r;
else
     parent->r=x->r;

delete x;
return;
    }

    // if node to be deleted has only left child
    if(x->l != NULL && x->r == NULL)
    {
if(parent->l == x)
     parent->l=x->l;
else
     parent->r=x->l;

delete x;
return;
    }
}





int main()
{
    tree t;
    int i;
    srand((unsigned)time(0));
    int random[65536];
    for(int i=0; i<10; i++)
    {
        
        random[i] = (rand()%10)+1;
    //  n=random[i];
    }
for(i =0; i<10; i++)
{
t.add(random[i]);
}
for(i=0;i<10;i++)
{
  
    t.del(random[i]);
}  
  
            return 0;
}          


User is offlineProfile CardPM
+Quote Post

bhandari
RE: Binary Search Trees
15 Mar, 2008 - 10:11 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
can you please tell what error the compiler is flashing??
User is offlineProfile CardPM
+Quote Post

slayer&wave
RE: Binary Search Trees
16 Mar, 2008 - 08:14 AM
Post #3

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 1

pleazzzzzzzzzzzz i need small program in c++

1- read array
2- making sorting with any method
3- making search using binart search
sending to my mail
slayer_859@hotmail.com

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:07PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month