Recursion headaches

Status
Not open for further replies.

morpheus1981

Wise, Aged Ars Veteran
104
I'm working with a binary tree, and I'm kinda stuck in recursion hell. Here's the algorithm in C++:

Code:
template <class T>
node<T>::inOrderTraversal(int stopspot, node<T>* current, node<T>* returnval, int nodecount)
{
  if (current->value){
     if (current->leftchild){ 
            current = current->leftchild;
     }
     nodecount++;
     if (nodecount == stopspot) { returnval = current; }
     if (current->rightchild){
            current = current->rightchild;
     }
  }
}

Now, here's what happens. I can completely traverse the tree with an inorder Traversal. No problems. However, the pointer returnval should point to the nth node(which is stopspot). But it doesn't. Once it traverses completely the left tree of the root, nodecount has a value, such as 3 or 7, something like that. After it is done with the root's lefttree, then it processes the root, and at the moment, nodecount is set to 1. It's like I never even traversed the left subtree of the root. Any suggestions to keep this from happening. since it's a traversal algorithm...I don't think I can use any return values successfully for each function, so I have to keep passing nodecount as a function paramter so that each time the function is called, I can retain the value. But the value is not actually retained. Please help me, I'm so confused by this phenomena.
 

morpheus1981

Wise, Aged Ars Veteran
104
Well, the algorithm for an inorder traversal is:

if the current node exists
--traverse the left child, if it exists
--process the current node
--traverse the right child, if it exists

What I want to do, given an integer value n, return a pointer to the nth value. To do this, I used an inorder traversal. So, when it gets to the nth value, <stopspot>, it *should* assign the returnval pointer to the current node pointed at by current, finish processing the tree, and then it'll be done. returnval should point to the nth node. But that doesn't happen.


UPDATD CODE -- sorry, forgot to put the code in that actually makes it recusive
Code:
template <class T>
node<T>::inOrderTraversal(int stopspot, node<T>* current, node<T>* returnval, int nodecount)
{  
   if (current->value)
   {     
       if (current->leftchild)
       {             
           inOrderTraversal(stopspot, current->leftchild, returnval,
                   nodecount); 
       }     
       nodecount++;     
       if (nodecount == stopspot) { returnval = current; }     
       if (current->rightchild)
       {            
           inOrderTraversal(stopspot, current->rightchild, returnval,
                   nodecount); 
       }  
   }
}

stopspot -- integer, the number of the node I need to return
current -- a pointer to the node to start the traversal at
returnval -- a pointer that should point to the nth node
nodecount -- a counter, to keep track of what node we currently are on

I am solving this problem recursively, because that is the only way I know how to. I need to find the nth node in a tree, and if anyone can point me to a non-recursive inorder traversal with pointers, I'd really love to see it. I couldn't find any. So I'm stuck with recursion.

My algorithm above works, but what happens is that it always assigns returnval to the last node, not the nth node. Lets say I have a binary tree as follows:

for discussion sake, let
current = root node
stopspot = 3
nodecount = 0 //initially
returnval = NULL
Code:
                       L
                    /      \
                  B         S
                  \          \
                   G         X
                     \
                       I
after processing B nodcount = 1
after processing G nodecount = 2
after processing I nodecount = 3, and returnval points to I
after processing L nodecount = 1 <--this is the problem
after processing S nodecount = 2
after processing X nodecouent = 3, and returnval points to X...
...then the algorithm terminates and I'm stuck with the wrong answer.

My question...why is nodecount reset to 1 when it returns to the root node??????

as defined above, stopspot = 3. Following an inorder traversal, the traversal list is: B G I L S X. So 'I' should be pointed to by returnval. And that works...until I return to node L to process it and the right subtree. nodecount was three when I was at node I, but when I am at node L again(because I'm done traversing the left subtree of L), node count is zero! I can't figure out why nodecount is reset. I am just curious why the nodecount value is reset....I'm still learning C++ and data structures, and so I don't know why nodecount is pass back when the function is done. :confused:
 

Handor

Ars Tribunus Militum
2,513
I think the issue is your understanding of how things are passed and manipulated in C++. Let's do a little pseudocode.

Code:
fun1(int myVar)
{
    myVar++;
}

fun2(object myVar)
{
    myVar->field = 1;
}

fun3(object myVar)
{
    myVar = newObject;
    myVar->field = 1;
}

int fun4(int myVar)
{
    return ++myVar;
}

main()
{
    int intVar = 0;
    object objVar = 0; // assumed pointer type

    fun1(intVar);
    fun4(intVar); // Neither of these will change intVar at all
                  // The reason is that these things are copied
                  // (not cross-referenced) to the target function
                  // so you have two copies of the value, one in
                  // each function; unless you pass it back, they
                  // will never meet each other.

    intVar = fun4(intVar); // This one is passing the value back and
                           // copying it into the local memory block.
                           // Now you'll have access to it.

    fun2(objVar); // what's getting passed to the function here is a
                  // pointer type, so only the value of the pointer
                  // is getting copied. When you use object->variable
                  // notation, you're going to the address which is
                  // at the variable object and working with the
                  // value there. So even though objVar (local to
                  // main()) and myVar (local to fun2()) refer to
                  // different places, they CONTAIN the same thing,
                  // and so they get to the same place.

    fun3(objVar); // This will not change objVar. Since you are
                  // merely copying the value to myVar in fun3(), and
                  // then you change myVar in fun3(), the two
                  // variables cease to have anything to do with one
                  // another, and so when you change the field on
                  // myVar, the original value gets maintained.
}

Essentially, you're running into this problem: when you recurse into a new level in your hierarchy, you are copying the value of nodecount into the node on the next level; but since it isn't getting passed back, that value disappears.
 
Status
Not open for further replies.