Binary Tree help

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

Join Date: Oct 2006
Posts: 1
Reputation: zith7400 is an unknown quantity at this point 
Solved Threads: 0
zith7400 zith7400 is offline Offline
Newbie Poster

Binary Tree help

 
0
  #1
Oct 2nd, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Binary Tree help

 
0
  #2
Oct 2nd, 2006
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.
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 473
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Binary Tree help

 
0
  #3
Oct 2nd, 2006
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.
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
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC