| | |
Binary Tree help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 1
Reputation:
Solved Threads: 0
I am trying to create a binary tree. Ultimately I would like a user to be able to insert how deep he or she wants the tree to be and then have my program be able to create the tree at that depth. Root = depth 1 and so on. Here is the code I have.. it doesn't seem to do the right thing. I have set my depth level at a given number for now and have decided to implement the "asking for depth level" later on as it is not as important to me as actually getting the tree to build correctly:
#include <cstdlib>
#include <iostream>
typedef struct node
{
int data;
node *lc;
node *rc;
}node;
int deep = 3;
void traverse(node *current);
using namespace std;
int main(int argc, char *argv[])
{
node *here;
traverse(here);
system("PAUSE");
return EXIT_SUCCESS;
}
void traverse(node *current)
{
deep--;
if(deep != 0)
{
current = new node;
current->data = rand();
}
else
{
deep++;
return;
}
traverse(current->lc);
traverse(current->rc);
deep++;
}
Can anyone tell me what is wrong with this? I seem to run into a road block when the "traverse" function gets called again and it looks like it is trying to create a new node overtop of one that exists... maybe i'm just reading the code wrong.. i don't know. Obviously, you can see i'm doing this recursively. Any help would be much appreciated. Thank You!
#include <cstdlib>
#include <iostream>
typedef struct node
{
int data;
node *lc;
node *rc;
}node;
int deep = 3;
void traverse(node *current);
using namespace std;
int main(int argc, char *argv[])
{
node *here;
traverse(here);
system("PAUSE");
return EXIT_SUCCESS;
}
void traverse(node *current)
{
deep--;
if(deep != 0)
{
current = new node;
current->data = rand();
}
else
{
deep++;
return;
}
traverse(current->lc);
traverse(current->rc);
deep++;
}
Can anyone tell me what is wrong with this? I seem to run into a road block when the "traverse" function gets called again and it looks like it is trying to create a new node overtop of one that exists... maybe i'm just reading the code wrong.. i don't know. Obviously, you can see i'm doing this recursively. Any help would be much appreciated. Thank You!
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
Any particulary reason you're writing procedural code in C++? A better approach would be to create a tree class that handles everything.
Anyways, one major problem you have is that you pass a single pointer to traverse. The pointer in traverse is local to the function, so setting
Only modifies the pointer in the function.
Have you been reading a tutorial on binary trees? If you have, I think you should re-read it, and possibly find one that is specifically object oriented.
Anyways, one major problem you have is that you pass a single pointer to traverse. The pointer in traverse is local to the function, so setting
current = new node;
Only modifies the pointer in the function.
Have you been reading a tutorial on binary trees? If you have, I think you should re-read it, and possibly find one that is specifically object oriented.
Last edited by GloriousEremite; Oct 2nd, 2006 at 12:52 am.
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
out of this stony rubbish?"
For a full blown implementation of Binary trees in OO C++ see here:
http://library.thinkquest.org/C00561...inarytrees.htm
A good way of approaching the problem will be to jot down all the details (like variables, class design, usage etc) which though may seem as a waste of time to you will definately help you in the long run. IF this kind of documentation is made, the coding just becomes mechanical.
http://library.thinkquest.org/C00561...inarytrees.htm
A good way of approaching the problem will be to jot down all the details (like variables, class design, usage etc) which though may seem as a waste of time to you will definately help you in the long run. IF this kind of documentation is made, the coding just becomes mechanical.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Similar Threads
- 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: CGI script gathering browser info
- Next Thread: Problems with C++ program
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






