简体   繁体   English

如何使用 to String 方法打印出我的二叉树

[英]How can i print out my binary tree with a to String method

I have a question for my binary tree code.我对我的二叉树代码有疑问。 First of all I've got 3 variables:首先,我有 3 个变量:

  • int node for the number数字的int node

  • boolean lhs = null for the left tree boolean lhs = null左树

  • boolean rhs = null for the right tree boolean rhs = null右树

I want to print out my binary tree but my code doesn't work correctly:我想打印出我的二叉树,但我的代码不能正常工作:

public String toString () {
    if (lhs == null) {
    } else if (rhs == null) {
        return "Baum ist leer";
    }
    if (lhs != null) {
    }
    else if (rhs != null){
        return rhs.toString();
    }
}

This is my code, but the only output I get in my console is:这是我的代码,但我在控制台中得到的唯一输出是:

Baum ist leer鲍姆·莱尔

...and I don't know why, as my tree isn't empty. ...我不知道为什么,因为我的树不是空的。 I can't find my mistake.我找不到我的错误。

This is the code for inserting into my tree:这是插入我的树的代码:

public void add(int insert) {   
    if (node > insert) {
        if (lhs == null) {
            lhs = new Tree(insert);
        } else {
            lhs.add(insert);
        }
    } else if (node < insert) {
        if (rhs == null) {
            rhs = new Tree(insert);
        } else {
            rhs.add(insert);
        }
    }
}

Your first condition isn't right correctly.您的第一个条件不正确。 What this code menans is:这段代码的意思是:

if (lhs == null) {
    //Code here will run if lhs equals to null, regardless the other variables values.
} else if (rhs == null) {
    //Code here will run if lhs isn't null, but rhs equals null.
    return "Baum ist leer";
}

According to your return, I think you try make your else if run if both of them are null, which this code does:根据你的回报,我认为如果它们都为空,你可以尝试让你的else if运行,这段代码就是这样做的:

if(lhs == null){
    if(rhs == null){
        //Code here will run only if both lhs and rhs are null.
        return "Baum ist leer";
    }
}

Or, more clean code:或者,更干净的代码:

if(lhs == null && rhs == null){
    //Code here will run only if both lhs and rhs are null.
    return "Baum ist leer";
}

If I misunderstood your purpose, live comment with it below.如果我误解了您的目的,请在下面进行实时评论。

The reason you only get "Baum ist leer" as return value, is because your function does not ever return anything else.您仅将“Baum ist leer”作为返回值的原因是因为您的函数不会返回任何其他内容。 The only time it returns a literal string is here:它唯一一次返回文字字符串是在这里:

return "Baum ist leer";

The only other time it returns something, it is:它返回的唯一其他时间是:

return rhs.toString();

..which is a recursive call that can only result in either another recursive call or some return that does not make a recursive call. ..这是一个递归调用,只能导致另一个递归调用或一些不进行递归调用的return And as you only have one such return that does not make a recursive call, there is no way you would get any other string than "Baum ist leer".而且由于您只有一个不进行递归调用的此类return ,因此除了“Baum ist leer”之外,您无法获得任何其他字符串。

Secondly, the function should guarantee to always return a string, so there should be no empty if blocks.其次,函数应该保证总是返回一个字符串,所以不应该有空的if块。

Finally, there is no code in your toString method that reads the data of a node, so it is impossible that this function would return a string with node data.最后,您的toString方法中没有读取节点数据的代码,因此该函数不可能返回带有节点数据的字符串。

A side note: it is confusing that you have named the data of a tree node node .附注:您将树节点node的数据命名为令人困惑。 It is most common to refer to the object as the node, and to its data as either data or value , or something the like.最常见的是将对象称为节点,将其数据称为datavalue或类似的东西。 But naming it node is confusing.但是将其命名为node令人困惑。

Here is an implementation of toString that returns a multiline string, where each line represents one node, and the indentation of the node's number is an indication where it is in the tree structure.这是一个toString的实现,它返回一个多行字符串,其中每一行代表一个节点,节点编号的缩进指示它在树结构中的位置。 The root will have no indentation and so appears at the far left side, while the deepest leaves of the tree will appear at the far right.根没有压痕,因此出现在最左侧,而树最深的叶子将出现在最右侧。

    // Helper
    private String indentedString(Tree tree, String indent) {
        if (tree == null) {
            return "";
        }
        return indentedString(tree.rhs, indent + "  ") 
             + indent + tree.node + "\n"
             + indentedString(tree.lhs, indent + "  ");
    }
    
    public String toString () {
        return indentedString(this, "");
    }

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

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