|
-
April 29th, 2009, 09:55 AM
#1
binary trees
I got one code with binary trees.
http://pastebin.com/f307cbe21
Is here:
Code:
void treeList(TreeNode *node) {
// Print the items in the tree in postorder, one item
// to a line. Since the tree is a sort tree, the output
// will be in increasing order.
if ( node != NULL ) {
treeList((*node).left); // Print items in left subtree.
cout << " " << node->item << endl; // Print item in the node.
treeList(node->right); // Print items in the right subtree.
}
} // end treeList()
pointer pointing at pointer (*node).left or node->left ???
Thanks in advance.
-
April 29th, 2009, 10:06 AM
#2
Re: binary trees
 Originally Posted by StGuru
(*node).left or node->left
Those two expressions are equivalent.
-
April 29th, 2009, 10:17 AM
#3
Re: binary trees
Yes I know. But is it pointer node pointing at pointer left of the struct TreeNode?
-
April 29th, 2009, 10:22 AM
#4
Re: binary trees
If I'm parsing your question right, no, it doesn't modify what "node" points to.
-
April 29th, 2009, 11:18 AM
#5
Re: binary trees
If left is not pointer, then what is it?
Thanks.
-
April 29th, 2009, 01:35 PM
#6
Re: binary trees
For first time I see pointer pointing to pointer :-). I understand the binary trees theoretically, but I don't understand even single line of the code.
Thanks in advance.
-
April 29th, 2009, 02:16 PM
#7
Re: binary trees
I'm not really sure what your question is anymore.
-
April 29th, 2009, 02:22 PM
#8
Re: binary trees
Anyway, there are questions. I really don't understand the code. Can somebody explain?
-
April 29th, 2009, 02:28 PM
#9
Re: binary trees
It's simply an inorder traversal of the entire tree....nothing particularly complicated there. You can read it as
Code:
IF (at a valid node)
Output all values in my left child
Output my value
Output all values in my right child
END IF
Where the first and third outputs are recursive calls, which I know will output their subtrees' values because that's what I do to my own value in the second output.
-
April 29th, 2009, 02:49 PM
#10
Re: binary trees
I don't understand the output. How does it come to node->item? There is treeList((*node).left); Which is pointer that points to another pointer "left". I actually don't understand how the pointers *left and *right store the items.
-
April 29th, 2009, 03:04 PM
#11
Re: binary trees
http://en.wikipedia.org/wiki/Binary_tree
http://en.wikipedia.org/wiki/File:Binary_tree.svg
In that image, the root node passed to treeList() initially would be the one with node->item == 2. Then node->left->item == 7, node->right->item == 5, node->left->right->item == 6, etc.
-
April 30th, 2009, 02:24 AM
#12
Re: binary trees
Thank you. Now I understand. When does it stop? Is it when the pointer is null or there isn't any more nodes in the tree?
Is it same like pointer pointing at pointer?
Code:
#include <iostream>
using namespace std;
int main()
{
int a;
int * b;
int ** c;
a = 5;
b = &a;
c = &b;
cout<<a<<" "<<*b<<" "<<**c<<endl;
system("PAUSE");
}
-
April 30th, 2009, 08:32 AM
#13
Re: binary trees
Typically such a tree would be built with the new operator, not with &. A NULL child pointer indicates no child node on that side.
Tags for this Thread
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
|