最大堆

data[1]开始存,data[0]空着不用。也可以把data[0]当成size来用。

public class MaxHeap<T extends Comparable<? super T>> {
    protected T[] data;
    protected int size;
    protected int capacity;

    public MaxHeap(int capacity) {
        data = (T[]) new Comparable[capacity + 1];
        this.capacity = capacity;
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void insert(T item) {
        data[size + 1] = item;
        size++;
        shiftUp(size);
    }

    /**
     * @return 弹出最大根(弹出意味着删除,与seekMax对比)
     */
    public T popMax() {
        T ret = data[1];
        swap(1, size);
        size--;
        shiftDown(1);
        return ret;
    }

    /**
     * @return 查看最大根(只看不删,与popMax对比)
     */
    public T seekMax() {
        return data[1];
    }

    public void swap(int i, int j) {
        if (i != j) {
            T temp = data[i];
            data[i] = data[j];
            data[j] = temp;
        }
    }

    public void shiftUp(int k) {
        while (k > 1 && data[k / 2].compareTo(data[k]) < 0) {
            swap(k, k / 2);
            k /= 2;
        }
    }

    /**
     * @param a 一个节点的下角标
     * @param b 另一个结点的下角标
     * @return 两者谁的data值最大,就返回谁的下角标
     */
    private int max(int a, int b) {
        if (data[a].compareTo(data[b]) >= 0) {
            return a;
        } else {
            return b;
        }
    }

    /**
     * @param a 第一个节点的下角标
     * @param b 第二个结点的下角标
     * @param c 第三个结点的下角标
     * @return 三者谁的data值最大,就返回谁的下角标
     */
    private int max(int a, int b, int c) {
        int newFather = max(a, b);
        newFather = max(newFather, c);
        return newFather;
    }

    /**
     * @param father 父节点father和左右两个孩子进行比较,三者中最大的当father
     */
    public void shiftDown(int father) {
        while (true) {
            int left = 2 * father;//左孩子
            int right = 2 * father + 1;//右孩子
            int newFather;

            if (left > size) {//如果这个father结点即没有左孩子,又没有右孩子。
                break;
            } else if (right > size) {//如果这个father结点没有右孩子,只有左孩子,则父、左两个结点中找出最大的。
                newFather = max(father, left);
            } else {//如果这个father结点有右孩子,则父、左、右三个结点比较,找出最大的
                newFather = max(father, left, right);
            }

            if (father == newFather) {//如果父亲自己就是最大的,说明堆已经满足条件,不用继续shiftDown了
                return;
            } else {//如果其中一个孩子比父亲大,那么说明堆还不是大根堆,需要继续shiftDown
                swap(father, newFather);//值进行交换
                father = newFather;//newfather是(2*father)或者是(2*father+1),也就是继续shiftDown(newFather);
            }
        }
    }

    public static void main(String[] args) {
        //创建大根堆
        MaxHeap<Integer> maxHeap = new MaxHeap<Integer>(100);
        //向堆里存
        for (int i = 0; i < 100; i++) {
            maxHeap.insert((int) (Math.random() * 100));
        }

        //创建数组
        Integer[] arr = new Integer[100];
        //从堆里取,放进数组里
        for (int i = 0; i < 100; i++) {
            arr[i] = maxHeap.popMax();
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

最大堆:shiftDown()函数与上面不一样

public class MaxHeap<T extends Comparable<? super T>> {
    protected T[] data;
    protected int size;
    protected int capacity;

    public MaxHeap(int capacity) {
        data = (T[]) new Comparable[capacity + 1];
        this.capacity = capacity;
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void insert(T item) {
        data[size + 1] = item;
        size++;
        shiftUp(size);
    }

    /**
     * @return 弹出最大根(弹出意味着删除, 与seekMax对比)
     */
    public T popMax() {
        T ret = data[1];
        swap(1, size);
        size--;
        shiftDown(1);
        return ret;
    }

    /**
     * @return 查看最大根(只看不删, 与popMax对比)
     */
    public T seekMax() {
        return data[1];
    }

    public void swap(int i, int j) {
        if (i != j) {
            T temp = data[i];
            data[i] = data[j];
            data[j] = temp;
        }
    }

    public void shiftUp(int k) {
        while (k > 1 && data[k / 2].compareTo(data[k]) < 0) {
            swap(k, k / 2);
            k /= 2;
        }
    }

    public void shiftDown(int father) {
        while (2 * father <= size) {
            int newFather = 2 * father;
            if (newFather + 1 <= size && data[newFather + 1].compareTo(data[newFather]) > 0) {//data[j] data[j+1]两者取大的那个
                newFather = newFather + 1;
            }
            if (data[father].compareTo(data[newFather]) >= 0) {
                break;
            } else {
                swap(father, newFather);//值进行交换
                father = newFather;//newFather是(2*father)或者是(2*father+1),也就是继续shiftDown(newFather);
            }
        }
    }

    public static void main(String[] args) {
        //创建大根堆
        MaxHeap<Integer> maxHeap = new MaxHeap<Integer>(100);
        //向堆里存
        for (int i = 0; i < 100; i++) {
            maxHeap.insert((int) (Math.random() * 100));
        }

        //创建数组
        Integer[] arr = new Integer[100];
        //从堆里取,放进数组里
        for (int i = 0; i < 100; i++) {
            arr[i] = maxHeap.popMax();
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}