943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3545
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 18th, 2007
1

Binary tree coding

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nitro is offline Offline
3 posts
since Oct 2007
Oct 18th, 2007
0

Re: Binary tree coding

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 16th, 2008
0

Re: Binary tree coding

Click to Expand / Collapse  Quote originally posted by nitro ...
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
C++ Syntax (Toggle Plain Text)
  1. struct node
  2. {
  3. int iData;
  4. struct node* pLeft;
  5. struct node* pRight;
  6. }node;
  7.  
  8.  
  9. node* getNode(int iData)
  10. {
  11. node* pNewNode = (node *)malloc(sizeof(node));
  12. pNewNode->iData = iData;
  13. pNewNode->m_pLeft = NULL;
  14. pNewNode->m_pRight = NULL;
  15. return pNewNode;
  16. }
  17. void insert(node* pParent, int iData)
  18. {
  19. if (pParent == NULL)
  20. {
  21. pParent = getNode(iData);
  22. if (pRoot == NULL)
  23. {
  24. pRoot = pParent;
  25. }
  26. }
  27. else
  28. {
  29. if (iData < pParant->iData)
  30. {
  31. insert(pParent->pLeft, iData);
  32. }
  33. else
  34. {
  35. insert(pParent->pRight, iData);
  36. }
  37. } }
Last edited by Narue; Dec 16th, 2008 at 3:16 pm. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manishady is offline Offline
2 posts
since Dec 2008
Dec 16th, 2008
0

Re: Binary tree coding

Click to Expand / Collapse  Quote originally posted by Narue ...
>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++
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manishady is offline Offline
2 posts
since Dec 2008
Dec 16th, 2008
0

Re: Binary tree coding

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

its BST SAmple
c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. float find(float c[50][50],int r[50][50],int,int);
  5. int main(void)
  6. {
  7. int i,n,k,l;
  8. float w[50][50];
  9. int r[50][50];
  10. float p[50],q[50];
  11. int j,min;
  12. float c[50][50];
  13. cout<<endl<<endl;
  14. cout<<"enter the no.of identifiers"<<endl;
  15. cin>>n;
  16. for(int i=1;i<=n;i++)
  17. {
  18. cout<<"Enter The Identifier "<<i<<endl;
  19. cin>>p[i];
  20. }
  21. //cout<<"q"<<endl;
  22. //cin>>q;
  23. for(i=0;i<=n;i++)
  24. {
  25. cout<<"Enter The Qualifier "<<i<<endl;
  26. cin>>q[i];
  27. }
  28. for(i=0;i<=n-1;i++)
  29. {
  30. w[i][i]=q[i];
  31. r[i][i]=0;
  32. c[i][i]=0.0;
  33. //optimal trees with one node.
  34. w[i][i+1]=q[i]+q[i+1]+p[i+1];
  35. r[i][i+1]=i+1;
  36. c[i][i+1]=q[i]+q[i+1]+p[i+1];
  37. }
  38. w[n][n]=q[n];
  39. r[n][n]=0;
  40. c[n][n]=0.0;
  41. for(int m=2;m<=n;m++)
  42. for(i=0;i<=n-m;i++)
  43. {
  44. j=i+m;
  45. w[i][j]=w[i][j-1]+p[j]+q[j];
  46. k=find(c,r,i,j);
  47. c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
  48. r[i][j]=k;
  49. }
  50.  
  51. cout<<"Weight Is\t"<<w[0][n]<<endl;
  52. cout<<"Cost Is\t"<<c[0][n]<<endl;
  53. cout<<"Root Is\t"<<r[0][n]<<endl<<endl;
  54. cout<<"Matrix format"<<endl;
  55. for(i=0;i<=n;i++)
  56. {
  57. cout<<"\n";
  58. for(j=i;j<=n;j++)
  59. {
  60. cout<<"\nWeight="<<w[i][j];
  61. cout<<"\tCost="<<c[i][j];
  62. cout<<"\tRoot="<<r[i][j];
  63. }
  64. }
  65. int b;
  66. cout<<"\nRoot is "<<r[0][n];
  67. b=r[0][n];
  68. i=n;
  69. cout<<"\nLeft child is "<<r[0][i-2];
  70. cout<<"\nRight child of "<<b<<" is "<<r[i-2][n-1];
  71. cout<<"\nThis is again a root";
  72. i=1;
  73. cout<<"\nRight sub child is "<<r[n-1][n];
  74. cout<<"\nLeft sub child is "<<r[i-2][n-2];
  75. cin.get();
  76. }
  77. float find(float c[50][50],int r[50][50],int i,int j)
  78. {
  79. int min;
  80. float l;
  81. min=1000;
  82. for(int m=r[i][j-1];m<=r[i+1][j];m++)
  83. if(c[i][m-1]+c[m][j]<min)
  84. {
  85. min=c[i][m-1]+c[m][j];
  86. l=m;
  87. }
  88. cin.get();
  89. cin.get();
  90. cin.get();
  91. return 0;
  92. }
Last edited by ninja_gs; Dec 16th, 2008 at 1:25 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
ninja_gs is offline Offline
44 posts
since Dec 2008
Dec 16th, 2008
1

Re: Binary tree coding

>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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 16th, 2008
0

Re: Binary tree coding

Narue
Quote ...
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 meet nitro 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..
Last edited by Narue; Dec 16th, 2008 at 4:12 pm. Reason: removed derogatory comments
Reputation Points: 10
Solved Threads: 0
Light Poster
ninja_gs is offline Offline
44 posts
since Dec 2008
Dec 16th, 2008
1

Re: Binary tree coding

>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.
Last edited by Narue; Dec 16th, 2008 at 4:13 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 16th, 2008
0

Re: Binary tree coding

Click to Expand / Collapse  Quote originally posted by ninja_gs ...
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.
Last edited by MosaicFuneral; Dec 16th, 2008 at 4:11 pm.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 16th, 2008
0

Re: Binary tree coding

Click to Expand / Collapse  Quote originally posted by ninja_gs ...
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:

Quote originally posted by Plato on Gorgias ...
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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I need feed back on how to correct my pseudo code....Can someone help me please?
Next Thread in C++ Forum Timeline: ask about function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC