我正在用Java实现一个红色/黑色的树,并验证我的树是否正确,并使调试更容易,我只需复制/粘贴一个打印出树到标准输出的方法。
对于输入序列:29, 42, 23, 47, 11, 4
该方法将打印出来:
有点想象力,这实际上是一棵红/黑树,只是没有节点之间的边缘 42是黑色根,右黑色子47和左红色子23(红色节点被<和>包围)等。
对于较小的树木来说这很好,但对于较大的树木会变得有点复杂 现在,根位于左侧,树向右扩展。 我想知道是否有任何现成的方法可以通过首先打印根,然后向下扩展树来打印出这样的树?
像这样:或者,如果没有这样的方法可以随时使用,我怎么能改变当前的方法,使其像第二张图像一样打印?
这是目前的方法:
private static void printHelper(Node n, int indent) {
if (n.getRight() != null) {
printHelper(n.getRight(), indent + INDENT_STEP);
}
for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
if (n.getColor() == BLACK) {
System.out.println(n.getValue());
} else {
System.out.println("<" + n.getValue() + ">");
}
if (n.getLeft() != null) {
printHelper(n.getLeft(), indent + INDENT_STEP);
}
}
以树的根作为节点调用,0作为缩进调用(INDENT_STEP
为4)。
编辑:现在我想到了这不是红/黑树的特定问题。我因此从标题中删除红色/黑色,然后用二叉树替换它。
答案 0 :(得分:1)
也许您可以考虑使用不同的方法绘制更复杂的树。一个很好的工具是dot language,它是Graphviz软件的一部分。
这是一个如何在python中写入一个红黑树的例子: http://code.activestate.com/recipes/576817-red-black-tree/
答案 1 :(得分:1)
该死,我非常接近按预期工作!有谁知道为什么这仍未达到要求的结果?
package tree;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Tree {
private final static int BLACK = 1;
private final static int RED = 2;
private Tree left = null;
private Tree right = null;
private int color = BLACK;
private String value = "";
Tree(final Tree left, final Tree right, final int color, final String value) {
this.left = left;
this.right = right;
this.color = color;
this.value = value;
}
Tree getLeft() {
return left;
}
Tree getRight() {
return right;
}
int getColor() {
return color;
}
String getValue() {
return value;
}
public static void main(String[] args) {
Tree leaf1 = new Tree(null, null, RED, "20");
Tree leaf2 = new Tree(null, null, BLACK, "30");
Tree leaf3 = new Tree(null, null, RED, "2");
Tree leaf4 = new Tree(null, null, RED, "100");
Tree leaf5 = new Tree(null, null, BLACK, "5");
Tree middle1 = new Tree(leaf1, leaf2, RED, "40");
Tree middle2 = new Tree(middle1, leaf3, BLACK, "200");
Tree middle3 = new Tree(leaf4, leaf5, RED, "3");
Tree root = new Tree(middle2, middle3, RED, "50");
printTree(root);
}
static void printTree(final Tree t) {
final Map<Tree, Integer> widths = new HashMap<Tree, Integer>();
final Map<Tree, Integer> offsets = new HashMap<Tree, Integer>();
setWidths(widths, t);
setOffsets(offsets, widths, t, widths.get(t)/2);
final List<Tree> root = new ArrayList<Tree>();
root.add(t);
printTree(offsets, root);
}
static int setWidths(final Map<Tree, Integer> widths, final Tree t) {
if(widths.containsKey(t))
return widths.get(t);
int width = (t.getColor() == BLACK) ? t.getValue().length()
: t.getValue().length() + 2;
final Tree left = t.getLeft();
final Tree right = t.getRight();
if(left != null)
width += setWidths(widths, left);
if(right != null)
width += setWidths(widths, right);
widths.put(t, width);
return width;
}
static void setOffsets(final Map<Tree, Integer> offsets, final Map<Tree, Integer> widths,
final Tree t, final int offset) {
offsets.put(t, offset);
System.out.println("Parent offset for node " + t.getValue() + ", offset " + offset);
final Tree left = t.getLeft();
final Tree right = t.getRight();
if(left != null)
setOffsets(offsets, widths, left, offset - widths.get(left)/2);
if(right != null)
setOffsets(offsets, widths, right, offset + widths.get(right)/2);
}
static void printTree(final Map<Tree, Integer> offsets, final List<Tree> trees) {
if(trees.isEmpty())
return;
final List<Tree> children = new ArrayList<Tree>();
int linePos = 0;
for(int i = 0; i < trees.size(); ++i) {
final Tree t = trees.get(i);
int offset = offsets.get(t);
final char[] lead = new char[Math.max(offset - linePos, 0)];
Arrays.fill(lead, ' ');
System.out.print(new String(lead));
linePos += Math.max(offset, 0);
if(t.getColor() == RED) {
System.out.print(t.getValue());
linePos += t.getValue().length();
} else {
System.out.print("<" + t.getValue() + ">");
linePos += t.getValue().length() + 2;
}
if(t.getLeft() != null)
children.add(t.getLeft());
if(t.getRight() != null)
children.add(t.getRight());
}
System.out.println("");
printTree(offsets, children);
}
}