二进制搜索树:递归toString

时间:2013-12-10 08:51:04

标签: java recursion binary-tree

它只打印出一个项目。 假设以升序打印树的内容

public String toString()
{
    return toString (_root);
}
private String toString(BSTnode root)
{
    if (root == null)
        return "";
    toString(root._left);
    toString(root._right);
    return root._data.toString();
}

4 个答案:

答案 0 :(得分:3)

你想如何展示它们?

例如,您需要附加字符串。

private String toString(BSTnode root)
{
    StringBuilder builder = new StringBuilder();
    if (root == null)
        return "";
    builder.append(toString(root._left));
    builder.append(toString(root._right));
    return builder.append(root._data.toString()).toString();
}

或者只是对字符串使用连接。

private String toString(BSTnode root)
{
    String result = "";
    if (root == null)
        return "";
    result += toString(root._left);
    result += toString(root._right);
    result += root._data.toString()
    return result;
}

答案 1 :(得分:0)

//Helper

public String toString(){
return "<" +toString(root) + ">";
}

//recursively printing out the nodes

public static String toString(Node r){
if(r==null)
return "";
else
return toString(r.left) + " " +r.value + " " +toString(r.right);
}

答案 2 :(得分:0)

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }

    // Helper

    public String toString() {
        return "<" + toString(this) + ">";
    }

    // recursively printing out the nodes

    public static String toString(TreeNode r) {
        if (r == null)
            return "";
        else
            return r.val + " " + toString(r.left) + " " + toString(r.right);
    }

}

答案 3 :(得分:0)

C:\dev\project>gradlew flywayMigrate -Dflyway.password=*****
> Configure project :
Running gradle version: 4.10.3
> Task :Core:flywayMigrate FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':Core:flywayMigrate'.
> Error occurred while executing flywayMigrate
  Unable to obtain connection from database (jdbc:mysql://localhost:3306/sr?serverTimezone=UTC) for user 'userid': Access denied for user 'userid'@'localhost' (using password: YES)
  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  SQL State  : 28000
  Error Code : 1045
  Message    : Access denied for user 'userid'@'localhost' (using password: YES)
  Access denied for user 'userid'@'localhost' (using password: YES)