Thread: Binary Trees
View Single Post
Old Dec 2nd, 2004, 5:25 PM   #1
neufunk
Newbie
 
Join Date: Dec 2004
Posts: 1
Rep Power: 0 neufunk is on a distinguished road
Hello lads,

Let me start off by saying, no I'm not here to beg code off of you guys to do an assignment. I'm here to BUY code! JUST KIDDING

Anyway, this one is quite in depth...the object is to create a full binary tree of integers. The user is prompted for a height integer, and then the program is supposed to generate a full binary tree SUCH THAT doing a preorder traversal would output it in order (1 to whatever the final node is).

I've got most of the side things done, like computing the total number of nodes given the height, as well as a function to output the binary tree in order as specified...I just need to figure out an algorithm to put all the figures into the binary tree. For example, given 4, it would be something like this:

1
2 9
3 6 10 13
4 5 7 8 11 12 14 15

How am I going to do this? Any ideas? Does this look like a good idea?

struct node{
int value;
int level;
node* left;
node* right;
};

Then I would have to have some sort of recursion, right, storing the values of level to ensure that the tree stayed only as deep as specified by the height value. Also, I could assumedly use a for loop since I know exactly how many values will be inserted, something like

for (i=1;i<=totalnodes;i++)
insert(i);

Where insert() is going to somehow stick it into the tree!

I'm so muddled here. If anyone can put me on the path to clear thinking again, I'd much appreciate it. Gracias!
neufunk is offline   Reply With Quote