CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: binary trees

  1. #1
    Join Date
    Mar 2009
    Posts
    82

    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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: binary trees

    Quote Originally Posted by StGuru View Post
    (*node).left or node->left
    Those two expressions are equivalent.

  3. #3
    Join Date
    Mar 2009
    Posts
    82

    Re: binary trees

    Yes I know. But is it pointer node pointing at pointer left of the struct TreeNode?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: binary trees

    If I'm parsing your question right, no, it doesn't modify what "node" points to.

  5. #5
    Join Date
    Mar 2009
    Posts
    82

    Re: binary trees

    If left is not pointer, then what is it?

    Thanks.

  6. #6
    Join Date
    Mar 2009
    Posts
    82

    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: binary trees

    I'm not really sure what your question is anymore.

  8. #8
    Join Date
    Mar 2009
    Posts
    82

    Re: binary trees

    Anyway, there are questions. I really don't understand the code. Can somebody explain?

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  10. #10
    Join Date
    Mar 2009
    Posts
    82

    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.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  12. #12
    Join Date
    Mar 2009
    Posts
    82

    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");
    }

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured