| | |
Binary tree coding
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
>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.
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.
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
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)
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); } } }
Last edited by Narue; Dec 16th, 2008 at 3:16 pm. Reason: added code tags
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
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.
in main fun u can call this fun in iteration...
if u know the c code u can easily make the code for c++
Try This Code
and Try to Under stand It ..........
its BST SAmple
and Try to Under stand It ..........
its BST SAmple
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by ninja_gs; Dec 16th, 2008 at 1:25 pm.
" Known is A Drop - Unknown is An Ocean "
>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?
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.
Narue
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..
•
•
•
•
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?
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 "
>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.
>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.
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
•
•
•
•
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..
•
•
•
•
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.
![]() |
Similar Threads
- Coding a Complete Binary Tree (C)
- complete binary tree using an array help (C++)
- binary tree traversal .. (C++)
- Binary Tree Traversal (C)
- Question about binary tree & heaps (Computer Science)
- binary tree class (C++)
- C++ complete binary tree using an array. Unexpected end file (C++)
- coding a complete binary tree with Dev-C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: I need feed back on how to correct my pseudo code....Can someone help me please?
- Next Thread: ask about function
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






