Recursive Traverse of Binary Tree Not Terminating At Return Statement?

Your calls in the else block don't return - they probably should, like this: if (tree.getLeftSubtree()! = null) { // Traverse the left side, add a DOT to the end of a string to // change the morse code return encode(tree.getLeftSubtree(), target, s + DOT); } if (tree.getRightSubtree()! = null) { // Traverse the left side, add a DOT to the end of a string to // change the morse code return encode(tree.getRightSubtree(), target, s + DASH); } However, what do you want to happen if both the left and right subtrees are null?

And if they're both non-null what do you want to return? Note that just because your base call already returned, that only returns for that single call not all the other calls in the stack. Recursing doesn't replace the stack frame with the new call - it just adds another stack frame 1 Returning from that new stack frame just gets you back to where you were 1 Yes, I know about tail recursion.

Let's not confuse things though.

Your calls in the else block don't return - they probably should, like this: if (tree.getLeftSubtree()! = null) { // Traverse the left side, add a DOT to the end of a string to // change the morse code return encode(tree.getLeftSubtree(), target, s + DOT); } if (tree.getRightSubtree()! = null) { // Traverse the left side, add a DOT to the end of a string to // change the morse code return encode(tree.getRightSubtree(), target, s + DASH); } However, what do you want to happen if both the left and right subtrees are null?

And if they're both non-null, what do you want to return? Note that just because your base call already returned, that only returns for that single call - not all the other calls in the stack. Recursing doesn't replace the stack frame with the new call - it just adds another stack frame1.

Returning from that new stack frame just gets you back to where you were. 1 Yes, I know about tail recursion. Let's not confuse things though.

What I want it to return is the morse code for the target character. I'm building a string that represents that code with s and DOT or DASH on each step of the recursion. Though I thought the recursion would stop when I did the return.

I guess not. – Penn Oct 15 at 19:30 Ok, it does seem that I need I need the return statement in the else blocks, but I did something else wrong somewhere in there because it is not getting out of my left side traversal. I need to trace my code a bit to see where I went wrong, though your tip about adding the return statement seems to be pointing me in the right direction.

– Penn Oct 15 at 19:37 @Penn: It stops when you return from the first method call - the return statement when you're already 10 stack frames down doesn't pop the stack all the way up. – Jon Skeet Oct 15 at 19:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions