![]() |
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 :P 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! |
>doing a preorder traversal would output it in order
Preorder traversal or did you mean inorder traversal? Because most projects assume a binary search tree, and if a preorder traversal gave the values in order then it would violate the strict ordering of a binary search tree. As described, you want this tree: :
1:
8:
#include <stdio.h>Creating the type of tree that you want is not as obvious, but the solution will be similar in complexity. Keep that in mind when you try to work out your own implementation. :) |
| All times are GMT -5. The time now is 3:11 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC