它有一个包含此代码的节点类
class Node {
String value;
Node left;
Node right;
Node(String value) {
this.value = value;
this.left = null;
this.right = null;
}
}
树结构已经根据节点进行了
Node node = new Node();
此处node表示树的根。
我需要绘制或打印没有Gui,Jframe的树。只是在输出面板中显示。像这样的例子
format of the tree
应该根据节点的左右分支绘制这种树状结构。
在电话sketch(node);
上,树将打印出来
你们可以帮我创建一个打印树的草图类。
public static void sketch(Node root) {
}
非常感谢您的帮助。
答案 0 :(得分:0)
发表有用的答案;) 您链接的图片无法显示,所以我试一试,不知道您想要存档的内容!
使用为每个"子节点"调用的递归函数。 如果必须使树居中,可以让函数返回节点的最大级别,并在上面的所有行前面添加空格。
希望这会给你一些想法...... 没试过!
public static void sketch(Node node){
printNode(node,0);
}
public static int printNode(Node node, int level){
String line = "";
String lineLeft = "";
String lineRight = "";
for (int i = 0; i++; i<level){
line = line + " ";
lineLeft = lineLeft + " ";
lineRight = lineRight + " ";
}
line = line + node.getValue();
System.out.println(line);
System.out.println(lineLeft);
if(node.getLeft() != null){
printNode(node.getLeft(), level+1);
}
.... proceed here
}