@@ -0,0 +1,2081 @@
+/**%0A * priority heap%0A */%0Apublic class PriorityHeap %3CT extends Comparable%3E %7B%0A    private static int ARRAY_SIZE = 100;%0A    private T%5B%5D arrays;%0A    private int size;%0A%0A    public PriorityHeap() %7B%0A        this.arrays = (T%5B%5D)new Integer%5BARRAY_SIZE%5D;%0A        size = 0;%0A    %7D%0A%0A    public void push(T data) %7B%0A        if (size == ARRAY_SIZE) %7B%0A            return;%0A        %7D%0A%0A        flip(size++, data);%0A    %7D%0A%0A    public T poll() %7B%0A        if (isEmpty()) %7B%0A            return null;%0A        %7D else %7B%0A            T data = this.arrays%5B0%5D;%0A%0A            swap(0, this.size - 1);%0A            this.size--;%0A            sink(0);%0A%0A            return data;%0A        %7D%0A    %7D%0A%0A    public T peek() %7B%0A        if (isEmpty()) %7B%0A            return null;%0A        %7D else %7B%0A            return this.arrays%5B0%5D;%0A        %7D%0A    %7D%0A%0A    public int size() %7B%0A        return this.size;%0A    %7D%0A%0A    public boolean isEmpty() %7B%0A        return (this.size == 0);%0A    %7D%0A%0A    // ----------------- private methods%0A%0A    /**%0A     * flip %E5%85%83%E7%B4%A0%E7%9A%84%E4%B8%8A%E6%B5%AE%E6%93%8D%E4%BD%9C%0A     */%0A    private void flip(int i, T data) %7B%0A        while (i %3E 0) %7B%0A            int parent = i / 2;%0A            int compareResult = data.compareTo(this.arrays%5Bparent%5D);%0A            if (compareResult %3C 0) %7B%0A                this.arrays%5Bi%5D = this.arrays%5Bparent%5D;%0A            %7D else %7B%0A                break;%0A            %7D%0A%0A            i = parent;%0A        %7D%0A%0A        this.arrays%5Bi%5D = data;%0A    %7D%0A%0A    /**%0A     * sink %E5%85%83%E7%B4%A0%E7%9A%84%E4%B8%8B%E6%B2%89%E6%93%8D%E4%BD%9C%0A     */%0A    private void sink(int i) %7B%0A        while ((2 * i + 1) %3C= (this.size - 1)) %7B%0A            int child = 2 * i + 1;%0A            if ((child %3C (this.size - 1) && (this.arrays%5Bchild%5D.compareTo(this.arrays%5Bchild + 1%5D) %3E 0))) %7B%0A                child++;%0A            %7D%0A%0A            int compareResult = this.arrays%5Bi%5D.compareTo(this.arrays%5Bchild%5D);%0A            if (compareResult %3E 0) %7B%0A                swap(i, child);%0A            %7D else %7B%0A                break;%0A            %7D%0A%0A            i = child;%0A        %7D%0A    %7D%0A%0A    private void swap(int x, int y) %7B%0A        T data = this.arrays%5Bx%5D;%0A        this.arrays%5Bx%5D = this.arrays%5By%5D;%0A        this.arrays%5By%5D = data;%0A    %7D%0A%7D%0A

add PriorityHeap