Binary tree coding

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 3
Reputation: nitro is an unknown quantity at this point 
Solved Threads: 0
nitro's Avatar
nitro nitro is offline Offline
Newbie Poster

Binary tree coding

 
0
  #1
Oct 18th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary tree coding

 
0
  #2
Oct 18th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: manishady is an unknown quantity at this point 
Solved Threads: 0
manishady manishady is offline Offline
Newbie Poster

Re: Binary tree coding

 
0
  #3
Dec 16th, 2008
Originally Posted by nitro View 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



solution:
u can use this structure and the following functions
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: manishady is an unknown quantity at this point 
Solved Threads: 0
manishady manishady is offline Offline
Newbie Poster

Re: Binary tree coding

 
0
  #4
Dec 16th, 2008
Originally Posted by Narue View Post
>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++
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

Re: Binary tree coding

 
0
  #5
Dec 16th, 2008
Try This Code
and Try to Under stand It ..........

its BST SAmple
  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.
" Known is A Drop - Unknown is An Ocean "
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary tree coding

 
0
  #6
Dec 16th, 2008
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 44
Reputation: ninja_gs is an unknown quantity at this point 
Solved Threads: 0
ninja_gs's Avatar
ninja_gs ninja_gs is offline Offline
Light Poster

Re: Binary tree coding

 
0
  #7
Dec 16th, 2008
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 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
" Known is A Drop - Unknown is An Ocean "
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary tree coding

 
0
  #8
Dec 16th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Binary tree coding

 
0
  #9
Dec 16th, 2008
Originally Posted by ninja_gs View Post
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Binary tree coding

 
0
  #10
Dec 16th, 2008
Originally Posted by ninja_gs View Post
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:

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC