简体   繁体   English

将二叉树的所有路径打印为0和1的字符串

[英]Printing all paths of Binary Tree as a s String of 0s and 1s

I am trying to print all the paths of a binary tree. 我正在尝试打印二叉树的所有路径。 If the method goes left, it should append 0 to the return value. 如果该方法左移,则应在返回值后附加0。 If the method goes right it should append 1 to the return value. 如果方法正确,则应在返回值后附加1。 The final product should look something like this: 最终产品应如下所示:

A
B 0
C 00
D 01
E 1

Unfortunately my code is only printing out zeros. 不幸的是,我的代码仅打印出零。 I would assume that my method is not going right, but I am unable to determine why. 我以为我的方法不正确,但是我无法确定原因。 Any help or suggestions would be greatly appreciated. 任何帮助或建议,将不胜感激。 Thanks! 谢谢!

private static String getAllPaths(final BinaryNodeInterface<Character> root)
{   
     String returnVal = "";
     returnVal = privateGetAllPaths(root, returnVal);
     return returnVal;

}
private static String privateGetAllPaths(final BinaryNodeInterface<Character> root, String numbers){
    String returnVal = "";
    String tempVal;
     if (root == null)
         return null;
     if (root != null)
         returnVal +=  root.getData() + numbers + '\n';
    tempVal = privateGetAllPaths(root.getLeftChild(), numbers += 0);
        if(tempVal != null)
          {
           returnVal += tempVal +"0";
            return returnVal;
          }
         tempVal  =  privateGetAllPaths(root.getRightChild(), numbers +=1);
          if(tempVal != null)
           {
            return returnVal;
           }


    return returnVal;
}

You have way to many return statements in your code. 您可以在代码中使用许多return语句。 And the complete code is a single mess in every way (layout, style, etc.). 完整的代码在各个方面(布局,样式等)都是一团糟。

public static void listPaths(BinaryNodeInterface<Character> node , StringBuilder builder , String path){
     builder.append('\n');
     builder.append(node.getData());
     builder.append(" " + path);

     if(node.getLeftChild() != null)
          listPaths(node.getLeftChild() , builder , path + "0");

     if(node.getRightChild() != null)
          listPaths(node.getRightChild() , builder , path + "1");
}

public String listPaths(BinaryNodeInterface<Character> node){
     StringBuilder builder = new StringBuilder();
     listPaths(node , builder , "");
     builder.deleteChar(0);//delete first char (useless '\n')
     return builder.toString();
}

Should work like a charm (not tested). 应该像魅力一样工作(未经测试)。

As fabian said you used too many return -s. 至于法比安说,你用了太多的回报 -s。 If your code follows the left link then you return and skip processing a right subtree of the current node. 如果您的代码遵循左链接,那么您将返回并跳过对当前节点的右子树的处理。

Additionaly you use += where should be + , thus when you finally go to the right child, the numbers will have the "01" suffix instead of "1". 另外,您使用+=应该在+ ,因此,当您最终找到合适的孩子时, numbers将带有“ 01”后缀而不是“ 1”。

Test also if numbers + 0 does what you expect (I'm not sure if it is equivalent to numbers + '0' )... 还测试numbers + 0是否符合您的期望(我不确定它是否等于numbers + '0' )...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM