954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary tree coding

please tell me how i can code a BST in c++ ?
i know that each node is like that of a linked list where one 'box' stores the item.
another two boxes stores the address/pointers to the two siblings. but how can i transform this into code?
like this?

struct Node
{
int item;
Node* left_child;
Node* right_child;
};

but this is just one node- how can i get a tree????

and i also need to know the c++ code of how to traverse this tree and delete/insert nodes etc.

please help me... thanks

nitro
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

>but this is just one node
No, it's just the definition of a node. You don't actually have any yet.

>please tell me how i can code a BST in c++ ?
This is in C, but you look like you're at the point where it doesn't matter, because you have no idea how to start.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

please tell me how i can code a BST in c++ ? i know that each node is like that of a linked list where one 'box' stores the item. another two boxes stores the address/pointers to the two siblings. but how can i transform this into code? like this?

struct Node { int item; Node* left_child; Node* right_child; };

but this is just one node- how can i get a tree????

and i also need to know the c++ code of how to traverse this tree and delete/insert nodes etc.

please help me... thanks


solution:
u can use this structure and the following functions

struct node
{
int iData;
struct node* pLeft;
struct node* pRight;
}node;


node* getNode(int iData)
{
node* pNewNode = (node *)malloc(sizeof(node));
pNewNode->iData = iData;
pNewNode->m_pLeft = NULL;
pNewNode->m_pRight = NULL;
return pNewNode;
}
void insert(node* pParent, int iData)
{
if (pParent == NULL)
{
pParent = getNode(iData);
if (pRoot == NULL)
{
pRoot = pParent;
}
}
else
{
if (iData < pParant->iData)
{
insert(pParent->pLeft, iData);
}
else
{
insert(pParent->pRight, iData);
}
} }
manishady
Newbie Poster
2 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

>but this is just one node No, it's just the definition of a node. You don't actually have any yet.

>please tell me how i can code a BST in c++ ? This is in C, but you look like you're at the point where it doesn't matter, because you have no idea how to start.

this is not one node the fun has been called recursively
in main fun u can call this fun in iteration...
if u know the c code u can easily make the code for c++

manishady
Newbie Poster
2 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Try This Code
and Try to Under stand It ..........

its BST SAmple

#include<iostream>
#include<cstdio>
using namespace std;
float find(float c[50][50],int r[50][50],int,int);
int main(void)
{
   int i,n,k,l;
  float w[50][50];
  int r[50][50];
  float p[50],q[50];
  int j,min;
  float c[50][50];
  cout<<endl<<endl;
  cout<<"enter the no.of identifiers"<<endl;
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    cout<<"Enter The Identifier "<<i<<endl;
    cin>>p[i];
  }
  //cout<<"q"<<endl;
  //cin>>q;
  for(i=0;i<=n;i++)
  {
    cout<<"Enter The Qualifier "<<i<<endl;
    cin>>q[i];
  }
  for(i=0;i<=n-1;i++)
  {
    w[i][i]=q[i];
    r[i][i]=0;
    c[i][i]=0.0;
    //optimal trees with one node.
    w[i][i+1]=q[i]+q[i+1]+p[i+1];
    r[i][i+1]=i+1;
    c[i][i+1]=q[i]+q[i+1]+p[i+1];
  }
  w[n][n]=q[n];
  r[n][n]=0;
  c[n][n]=0.0;
  for(int m=2;m<=n;m++)
   for(i=0;i<=n-m;i++)
    {
      j=i+m;
      w[i][j]=w[i][j-1]+p[j]+q[j];
      k=find(c,r,i,j);
      c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
      r[i][j]=k;
    }

    cout<<"Weight Is\t"<<w[0][n]<<endl;
    cout<<"Cost Is\t"<<c[0][n]<<endl;
    cout<<"Root Is\t"<<r[0][n]<<endl<<endl;
    cout<<"Matrix format"<<endl;
    for(i=0;i<=n;i++)
    {
       cout<<"\n";
       for(j=i;j<=n;j++)
       {
	  cout<<"\nWeight="<<w[i][j];
	  cout<<"\tCost="<<c[i][j];
	  cout<<"\tRoot="<<r[i][j];
       }
  }
  int b;
cout<<"\nRoot is "<<r[0][n];
b=r[0][n];
i=n;
cout<<"\nLeft child is "<<r[0][i-2];
cout<<"\nRight child of "<<b<<" is "<<r[i-2][n-1];
cout<<"\nThis is again a root";
i=1;
cout<<"\nRight sub child is "<<r[n-1][n];
cout<<"\nLeft sub child is "<<r[i-2][n-2];
    cin.get();
}
float find(float c[50][50],int r[50][50],int i,int j)
{
   int min;
   float l;
   min=1000;
   for(int m=r[i][j-1];m<=r[i+1][j];m++)
     if(c[i][m-1]+c[m][j]<min)
     {
       min=c[i][m-1]+c[m][j];
       l=m;
     }
     cin.get();
     cin.get();
     cin.get();
     return 0;
}
ninja_gs
Light Poster
45 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

>u can use this structure and the following functions
If you're an average (or in your case, below average) programmer, you should compile and run your examples before posting them to avoid embarrassment. Your code is quite broken in many ways.

>this is not one node the fun has been called recursively
>in main fun u can call this fun in iteration...
Is this English? By the way, you quoted me and not the OP, was that intended?

>Try This Code
>and Try to Under stand It ..........
Holy crap! Were you trying to make the code impenetrable?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Narue
Is this English? By the way, you quoted me and not the OP, was that intended?

>Try This Code
>and Try to Under stand It ..........
Holy crap! Were you trying to make the code impenetrable?
Dont comment any one like this
If you R Good
Post a Program to meetnitro requirment
If Not
MIND YOUR OWN BIZnesSS

I M Too a C++ learner , I liked to help nitro....
And I Provide wat i Can , From My Knowlege of Programming....

Naure
HAVE
NO RIGHTS TO COMMENT
ON ME

JUST LOOK AT NAURE 's ATTITUDE

"I'm here to prove you wrong"

So All Ppl
Just think of it

s(he) is Not here To Motivate or Help
s(he) is Here to Make you comment and prove you are Wrong..

ninja_gs
Light Poster
45 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

>If you R Good
>Post a Program to meet nitro requirment
I wrote a complete tutorial covering all of the basics with plenty of code examples. This goes above and beyond the OP's requirement. I answered this question far more completely than you did last year when this thread was started.

p.s. Learn to spell my name correctly.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
MIND YOUR OWN BIZnesSS YOu CreePpy MonkeY Narue.

Is there really any reason to explode over this? Calm down, this is just a programming forum, not some political moral debate.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

JUST LOOK AT NAURE 's ATTITUDE

"I'm here to prove you wrong"

So All Ppl Just think of it

s(he) is Not here To Motivate or Help s(he) is Here to Make you comment and prove you are Wrong..

Here:And what is my sort? you will ask. I am one of those who are very willing to be refuted if I say anything which is not true, and very willing to refute any one else who says what is not true, and quite as ready to be refuted as to refute-I for I hold that this is the greater gain of the two, just as the gain is greater of being cured of a very great evil than of curing another.

By the way no intention to flame - just a thought I liked to share.

mrboolf
Junior Poster
183 posts since Jun 2008
Reputation Points: 134
Solved Threads: 18
 

since manishady gave you the code, i just give you some advise:
1) data structrue is a very important subject,
2) you should relize every model the books says.
3) may you success.

AHUazhu
Newbie Poster
17 posts since Dec 2008
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You