943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3857
  • C++ RSS
Oct 2nd, 2006
0

Binary Tree help

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zith7400 is offline Offline
1 posts
since Oct 2006
Oct 2nd, 2006
0

Re: Binary Tree help

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

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.
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Oct 2nd, 2006
0

Re: Binary Tree help

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

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: CGI script gathering browser info
Next Thread in C++ Forum Timeline: Problems with C++ program





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


Follow us on Twitter


© 2011 DaniWeb® LLC