0
원하는 결과를 설명하기가 어렵지만, 여기서 왼쪽으로 90도 기울이면 일반 트리의 출력과 같습니다.콘솔에 허프만 트리를 표시하려면 어떻게해야합니까?
printTree(tree, "\t", 0);
private static void printTree(HTree tree, String space, int height) {
assert tree != null;
if (tree instanceof HLeaf) {
HLeaf leaf = (HLeaf)tree;
for (int k = 0; k < height; k++)
System.out.print(space);
System.out.println("(" + leaf.value + ")");
}
else if (tree instanceof HNode) {
HNode node = (HNode)tree;
// traverse left
printTree(node.left, space, height++);
// traverse right
printTree(node.right, space, height++);
}
}
출력은 다음과 같습니다 당신이 볼 수 있듯이
(e)
(s)
(o)
(w)
(l)
(n)
(t)
(h)
(k)
()
(a)
, 즉 아주 잘 허프만 트리를 닮은하지 않습니다.