二叉树的递归遍历不在返回语句处终止

发布于 2024-12-09 18:50:07 字数 1719 浏览 1 评论 0原文

我创建了一个用摩尔斯电码填充二叉树的类。向左移动表示 DOT,向右移动表示 DASH。一切都很顺利,直到我编写一个编码方法将字母字符转换为莫尔斯电码字符串。该方法应该递归地对树进行预序遍历(沿途创建摩尔斯电码字符串),直到找到目标字符,然后返回该字符串。

但是,由于某种原因,我的递归不会在我的基本情况下终止。它只是继续运行整个遍历。我附上了下面方法的代码。为什么if语句中的return语句没有触发并结束方法?

抱歉,如果这有歧义,但我不想为我的整个项目发布 300 行代码,因为比我聪明的人会立即注意到问题。

感谢您的帮助

    //wrapper class
    //@parameter character is the character to be encoded
    //@return return the morse code as a string corresponding to the character

    public String encode(char character){

        return encode(morseTree, character, "");


    }


    //@Parameters tree is the binary tree is the tree to be searched, 
    //element is the target character trying to be foudn, s is the string being used to build the morse code
    //@return returns the morse code that corresponds to the element being checked

    public String encode(BinaryTree<Character> tree, char target, String s){


        if(tree.getData() == target){  //if the data at the current tree is equal to the target element
            //return the string that is holding the morse code pattern for this current traversal
            return s;
        }else{
            if(tree.getLeftSubtree() != null){
                    //Traverse the left side, add a DOT to the end of a string to change the morse code
                    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
                    encode(tree.getRightSubtree(), target, s + DASH);
            }
        }

        //The code should never get this far!
        return s;
    }

I have created a class that populates a binary tree with morse code. Where traversing to the left signifies a DOT and traversing to the right signifies a DASH. Everything was going great until I am writing an encode method to convert a alpha character into a morse code string. The method should recursively do a preorder traverse of the tree(creating a string of the morse code along the way) until it finds a target character and then returns that string.

However, for some reason my recursion won't terminate on my base case. It just keeps running the entire traverse. I attached my code for the method below. Why does the return statement at in the if statement not trigger and end the method?

Sorry if this is ambiguous, but I didn't want to post 300 lines of code for my entire project when someone smarter than I would notice the problem right off.

Thanks for any help

    //wrapper class
    //@parameter character is the character to be encoded
    //@return return the morse code as a string corresponding to the character

    public String encode(char character){

        return encode(morseTree, character, "");


    }


    //@Parameters tree is the binary tree is the tree to be searched, 
    //element is the target character trying to be foudn, s is the string being used to build the morse code
    //@return returns the morse code that corresponds to the element being checked

    public String encode(BinaryTree<Character> tree, char target, String s){


        if(tree.getData() == target){  //if the data at the current tree is equal to the target element
            //return the string that is holding the morse code pattern for this current traversal
            return s;
        }else{
            if(tree.getLeftSubtree() != null){
                    //Traverse the left side, add a DOT to the end of a string to change the morse code
                    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
                    encode(tree.getRightSubtree(), target, s + DASH);
            }
        }

        //The code should never get this far!
        return s;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2024-12-16 18:50:07

您在 else 块中的调用不会返回 - 它们可能应该像这样:

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);
}

但是,如果左子树和右子树都为空,您希望发生什么?如果它们都是非空,你想返回什么?

请注意,仅仅因为您的基本调用已经返回,所以仅返回该单个调用 - 而不是堆栈中的所有其他调用。递归不会用新调用替换堆栈帧 - 它只是添加另一个堆栈帧1。从新的堆栈帧返回只会让您回到原来的位置。


1 是的,我知道尾递归。不过,我们不要混淆事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文