2012-03-16 2 views
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) 

, 즉 아주 잘 허프만 트리를 닮은하지 않습니다.

답변

3

예를 들어 this을 살펴볼 수 있습니다. 하프 맨 트리는 다음과 같은 방식으로 인쇄됩니다.

Display of Huffman coding tree 



    +---f: 0.3960, 0 (step 6) 
    | 
    [email protected]: 1.0000, 
    | 
    |  
    |   
    |    
    |   +---a: 0.0495, 1000 (step 2) 
    |   | 
    |  [email protected]: 0.1089, 100 (step 3) 
    |  | | 
    |  | |  
    |  | | +---#: 0.0099, 10010 (step 1) 
    |  | | | 
    |  | [email protected]: 0.0594, 1001 (step 2) 
    |  |  | 
    |  |  +---b: 0.0495, 10011 (step 1) 
    |  |   
    |  |  
    |  | 
    | [email protected]: 0.2574, 10 (step 5) 
    | | | 
    | | +---c: 0.1485, 101 (step 3) 
    | |  
    | | 
    [email protected]: 0.6040, 1 (step 6) 
     | 
     |  
     | +---d: 0.1683, 110 (step 4) 
     | | 
     [email protected]: 0.3465, 11 (step 5) 
      | 
      +---e: 0.1782, 111 (step 4) 
관련 문제