A Tricky Recursive Situation in Binary Trees

I'm trying to obtain a value through recursive calls, the problem I am encountering is that the first call has the same function which causes it to overwrite the variable I am trying to obtain.

Initially:

 ``123456789101112131415161718192021`` `````` //some variables I'm using stringCount = 0; value = '>'; char dLetter; //the function I initially use Traverse(root, value); //the container for a node typedef struct treeNode //container for a node { char letter; //letter of node treeNode* leftChild; //points to next node treeNode* rightChild; //points to previous node }; treeNode* root; //stuff you should know root->letter = a; (root->rightChild)->letter = p; ``````

Here's my code:

 ``1234567891011121314151617`` ``````void BinaryTree::Traverse(treeNode* tree, string value) //similar to Retrieve { while(stringCount < value.size()) { if(value[stringCount] == '*') { //special case when string translates into root node stringCount++; } else if(value[stringCount] == '<') { stringCount++; Traverse(tree->leftChild, value); } else { //assumes (value[stringCount] == '>') stringCount++; Traverse(tree->rightChild, value); } } dLetter = tree->letter; }``````

I want this to return dLetter = tree->letter of the last node traversed to, which would make dLetter = 'p', but once reaching the base case, it reads the previous Traverse() function as well making dLetter then equal that node's letter, which is 'a' (the root node).
Last edited on
I just figured this out after brainstorming a little bit more. So I simply stuck the
 dLetter = tree->letter
inside each conditional. Here is one example:

 ``12345`` ``````else if(value[stringCount] == '<') { stringCount++; dLetter = (tree->leftChild)->letter; Traverse(tree->leftChild, value); }``````
Last edited on
Topic archived. No new replies allowed.