试图建立霍夫曼树

时间:2019-03-15 19:02:50

标签: java

我正在使用霍夫曼算法,并且试图使其看起来像:this tree,但是在构建它后遇到了问题。我提取了两个最低频率的节点,直到在堆中有一个,但是我无法访问堆(根)中其余节点的第2级上的子节点。

public void HuffmanTree(String path) throws Exception {
    uniqueLeafsIntoHeap(path);
    while (heap.size() > 1) {
        Node tempNode1 = heap.remove();
        Node tempNode2 = heap.remove();
        Node tempNodeParent = new Node('\0', tempNode1.getOccurs() + tempNode2.getOccurs(), tempNode1, tempNode2);
        System.out.println(tempNodeParent.toString());
        heap.insert(tempNodeParent);}
    System.out.println(heap.getRoot().getOccurs());
    System.out.println(heap.getRoot().getRight().getLeft());
    heap.print();
    codeWords();
}

在我尝试访问的节点的第10行上,出现的次数等于25,控制台显示以下内容: screenshot

堆类:

public class Heap {
    private boolean debug;
    private Node[] Heap;
    private int size;
    private int maxsize;
private static final int FRONT = 1;

public Heap(int maxsize) {
    debug = true;
    this.maxsize = maxsize;
    this.size = 0;
    Heap = new Node[this.maxsize + 1];
    Heap[0] = new Node('\0', Integer.MIN_VALUE);
}
public Node getRoot() 
{
    return Heap[1];
}
public int size() {
    return size;
}

private int parent(int pos) {
    return pos / 2;
}

private int leftChild(int pos) {
    return (2 * pos);
}

private int rightChild(int pos) {
    return (2 * pos) + 1;
}

private boolean isLeaf(int pos) {
    if (pos >= (size / 2) && pos <= size) {
        return true;
    }
    return false;
}

private void swap(int fpos, int spos) {
    Node tmp = new Node(Heap[fpos].getSymbol(), Heap[fpos].getOccurs());
    Heap[fpos].setOccurs(Heap[spos].getOccurs());
    Heap[fpos].setSymbol(Heap[spos].getSymbol());
    Heap[spos].setOccurs(tmp.getOccurs());
    Heap[spos].setSymbol(tmp.getSymbol());
}

private void minHeapify(int pos) {
    int smallest = pos;
    if (leftChild(pos) <= size && Heap[pos].getOccurs() > Heap[leftChild(pos)].getOccurs()) {
        smallest = leftChild(pos);
    } else
        smallest = pos;
    if (rightChild(pos) <= size && Heap[smallest].getOccurs() > Heap[rightChild(pos)].getOccurs()) {
        smallest = rightChild(pos);
    }
    if (smallest != pos) {
        swap(pos, smallest);
        minHeapify(smallest);
    }
}

public void insert(Node element) {
    Heap[++size] = element;
    int current = size;

    while (Heap[current].getOccurs() < Heap[parent(current)].getOccurs()) {
        swap(current, parent(current));
        current = parent(current);
    }
}

public void print() {
    if (debug) {
        for (int i = 1; i <= size; i++) {
            System.out.print(Heap[i].getOccurs() + " - ");
        }
        System.out.println();
    }
    if (!debug) {
        for (int i = 1; i <= size / 2; i++) {
            System.out.print(" PARENT : " + Heap[i].getOccurs() + " LEFT CHILD : " + Heap[2 * i].getOccurs()
                    + " RIGHT CHILD :" + Heap[2 * i + 1].getOccurs());
            System.out.println();
        }
    }
}

public void minHeap() {
    for (int pos = (size / 2); pos >= 1; pos--) {
        minHeapify(pos);
    }
}

public Node remove() {
    /*
     * Node popped = Heap[FRONT] ; Heap[FRONT] = Heap[size--]; minHeapify(FRONT);
     */
    int temp = size--;
    Node popped = new Node(Heap[FRONT].getSymbol(), Heap[FRONT].getOccurs());
    Heap[FRONT].setOccurs(Heap[temp].getOccurs());
    Heap[FRONT].setSymbol(Heap[temp].getSymbol());
    minHeapify(FRONT);

    return popped;
}

}

0 个答案:

没有答案