views:

145

answers:

5
+2  A: 

Well, for starters, you're using post-increment/decrement, you probably meant ++depth/--depth for the right assignment and the else return;

Also, why pass a pointer as a reference variable?

cthom06
I tried the precrement as you suggested and it's getting there, but not quite right. Why would I use precrement instead of post? I thought that only mattered in order of operations?I'm using the reference to a pointer because that's how it seems to work. We were given very brief introductions to pointers, then dropped in an ocean of Trees. It was very much a guess and check to see when the compiler stopped throwing errors at me.
xyzjace
+3  A: 

You don' t need

else //leaf
    return depth--;

You also don't want to increment the depth variable, just pass depth+1 to the next interation.

Also there's no need to return a value.

Try this:

void assignDepth(Tree T, int depth)
{
    if(T!=NULL)
    {
        assignDepth(T->left, depth+1);
        T->depth = depth;
        assignDepth(T->right, depth+1);
    }
}
Andrew Cooper
I don't understand why not, but everyone has said to remove it, so I did. Recursion breaks my mind. Thanks :)
xyzjace
It helps to think through what's happening. In the first call depth = 0 (or whatever) and T is the root of the tree. Assuming T is not NULL the function calls itself on the left sub-tree with depth+1, assigns depth to the current (root) node, then calls itself on the right sub-tree, again with depth+1. It's that middle step that assigns the value of the depth parameter to the node, so there's no need to return the value to anywhere.
Andrew Cooper
That makes sense. I get a little confused with value returning in recursion. But I think I get it now. Much appreciated :)
xyzjace
+2  A: 
int assignDepth(Tree &T, int depth)

You have defined Tree as a pointer to a treeNode. You don't need to pass it by reference. You can modify the node that's pointed to anyway.

{
    if(T!=NULL)
    {
        depth = assignDepth(T->left, depth++);

The postfix ++ ensures that you're passing the original depth down. That's not what you want. Increment depth before this, and forget about returning it as a function result.

    T->depth = depth;

This is OK.

        depth = assignDepth(T->right, depth++);

Similar as for the previous recursive call, except that here you shouldn't modify depth at all because it has already been incremented.

    }
  else //leaf
        return depth--;

You don't need to return any depth information (or is that an unstated requirement?).

}

Cheers & hth.,

Alf P. Steinbach
I read all the comments to make sure I don't miss anything. Thanks :)
xyzjace
+1  A: 
  1. Once you've reached a leaf node, you don't care about its depth any more, so the return value appears to accomplish nothing.

  2. In two statements:

    depth = assignDepth(T->left, depth++);
    // and
    depth = assignDepth(T->right, depth++);
    

You have undefined behavior from modifying depth twice without an intervening sequence point (although it seems like there should be, there is not a sequence point between the right and left sides of an assignment).

Jerry Coffin
I just realized why the return value is redundant as I was typing this. Man do I feel stupid. Thanks :)!Not sure what sequence points are, sorry.
xyzjace
@user475234: I debated over even mentioning it, since fixing the code otherwise will remove this problem as well. With a few exception, C basically says you can only modify a particular variable once in a given statement, and in this case you're doing it twice (once in the increment, once in the assignment).
Jerry Coffin
+1  A: 
  1. Why are you returning when the node is NULL. As per your specification you don't need to return any depth
  2. In other case you just need to increment the depth and send to the function call. The following is my version of the code

    void assignDepth(Tree &T,int depth) { if(T == NULL) return; else { T->depth = depth; if(T->left != NULL) assignDepth(T->left,depth+1); if(T->right != NULL) assignDepth(T->right,depth+1); } }

Ravi Gummadi
It's starting to make a little more sense. I realize now that when the recursion is finished it doesn't need to return the depth, because as it pulls the function call off the stack, depth is the same value it was before you called the function. Thanks :D!
xyzjace