|
-
December 5th, 2002, 11:43 PM
#1
Binary Tree Printing
Hi,
I need to print a binary tree
1. in the order of its level ( root, left & right child, children of the left & right nodes, its children & so on )
1, 2, 3, 4, 5, 6,7,8 .... where 2 & 3 are the children of 1, 4 & 5 are the children of 2, 6 & 7 are the children of 3 & so on...
2. print them in the following manner
1
2
3
-
December 5th, 2002, 11:58 PM
#2
Does the tree contain OBJECTS or Integers?
-
December 6th, 2002, 12:45 PM
#3
The tree contains simple integers
-
December 6th, 2002, 05:10 PM
#4
You have a strange binary tree. Normally it is left, middle then right but you want it printed as middle, left right. Oh well - we all have our reasons. Assuming the structure is
Code:
struct bintree
{
struct bintree* left;
struct bintree* right;
int val;
};
Printing it is just
Code:
void printtree (struct bintree* node)
{
cout << node->val << endl;
if (node->left != 0) printree (node->left);
if (node->right != 0) printree (node->right);
}
or if you are likely to start with a null node then
void printtree (struct bintree* node)
{
if (node != 0)
{
cout << node->val << endl;
printtree (node->left);
printtree (node->right);
}
}
The latter looks neater but nests more deeply at runtime. If you wanted it as left, value, right, just move the printree of the left before the cout.
Last edited by cup; December 6th, 2002 at 05:12 PM.
Succinct is verbose for terse
-
December 6th, 2002, 05:40 PM
#5
Are you trying to print them like
I was curious since you posted twice, and maybe weren't familiar as to how to get the tabbing to show up...
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
December 7th, 2002, 01:45 AM
#6
Originally posted by galathaea
I was curious since you posted twice
Sorry, no that was me. While writing the post he accidentally posted the half-done post and TheCPUWizard replied to that. I then merged the full question with this thread. But by deleting the first (uncomplete) post, I would delete the whole thread. Ok, I'm not trying to explain my life here, just that it's a technical glitch
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|