views:

40

answers:

2

I'm writing an expression tree.

The Node class has instances of itself as members left, right and parent.

Thanks to James McNellis in this post, I declared them as pointers.

   class Node
   {
     public:
        char *cargo; 
        int depth; 
        Node *parent;
        Node *left; 
        Node *right;
    //constructors/destructor:
        Node(void); 
        Node(int a_depth, Node *pparent = __nullptr); 
        ~Node();
    //method:
        void traverse_tree(Node n)
    };

Now I try to traverse the tree and print it out (to file "out").

Recursively calling 'traverse_tree(left);' and 'traverse_tree(right);'

causes the error message "cannot convert parameter 1 from 'Node *' to 'Node'".

Traverse_tree is called initially with the root node as the arguement.

I think the declaration of the parameter "(Node n)" is confusing the compiler and it doesn't know

whether to call a constructor or not.

How do I pass "left" and "right" to the "traverse_tree" method?

void Node::traverse_tree(Node n)
    //utility to view the tree
{
    if (((left) ==  __nullptr)||((right) ==  __nullptr))
    {
        return;
    }
    traverse_tree(right);
    out<<'  '<<n.cargo<<"\n";
    traverse_tree(left);
    return;
};
+2  A: 

I think you should be calling traverse_tree with a node pointer, not a node. You generally use pointers for this sort of operation. This would result in something like:

void Node::traverse_tree (Node *n) {
    if ((left == __nullptr) || (right == __nullptr))
        return;
    traverse_tree (right);
    out << "  " << n.cargo << "\n";
    traverse_tree (left);
    return;
};

and you would call it with:

root.traverse_tree (&root);

You may want to re-engineer your code at some point to make it more C++-like:

void Node::traverse_tree(void) {
    if ((left == __nullptr) || (right == __nullptr))
        return;
    right->traverse_tree();
    out << "  " << cargo << "\n";
    left->traverse_tree();
    return;
};

In other words, traverse using the methods of the sub-nodes themselves rather than passing the pointers (which is really the "old-school" non-object-oriented way of doing things).

paxdiablo
The scales fell from my eyes! Of course. I'm keeping the behaviour with the data.' In the fourth line from the end I removed the n. from n.cargo, and I added another "out<<' '<<n.cargo<<"\n"; " before the first return so that the leaf nodes would be displayed. It works except it returns 8224 before every cargo, like this 82241.0 8224* 82249.0 8224- 82241.0 .... I'm calling it with no arguements: root.traverse_tree(); Thanks for the lesson!
Peter Stewart
8224 problem solved. Single quotes changed to double quotes in " out<<" "<<cargo<<"\n";"
Peter Stewart
@Peter, the 8224 problem is because `' '` is a multi-byte character constant which turns into an integer. The two spaces (each hex 20 or decimal 32) give you the constant 32 * 256 + 32 = 8192 + 32 = 8224.
paxdiablo
+3  A: 

Dereference your pointers:

traverse_tree(*right);

You might also want to change your traverse_tree method to accept a reference:

void traverse_tree(Node &n)
Mark Byers
This works. Thanks, I'll wait a while before accepting an answer.
Peter Stewart
If I de-reference the pointer, why would the method expect a reference?
Peter Stewart
although, obviously it does, as this solved the problem. Thanks
Peter Stewart