/**
 * Company
 * Copyright (C) 2014-2017 All Rights Reserved.
 */
package com.cwenao.trying.tree;

/**
 * @author cwenao
 * @version $Id MaxTopHeapByArrays.java, v 0.1 2017-10-31 09:21 cwenao Exp $$
 */
public class MaxTopHeapByArrays<T extends Comparable> {

    private T[] heaps;

    private int n;




    /**
     * 下沉交换树上位置
     * @param k
     */
    private void sink(int k) {

        while (2 * k < n) {
            int j = 2 * k;
            if (j < 0 && less(j, j + 1)) {
                j++;
            }
            if (less(k, j)) {
                break;
            }
            exch(k, j);
            k = j;
        }
    }

    /**
     * 上浮
     * @param k
     */
    private void swam(int k) {
        while (k > 1 && less(k / 2, k)) {
            exch(k / 2, k);
            k = k / 2;
        }
    }

    private boolean less(int i, int j) {
        return heaps[i].compareTo(heaps[j]) < 0;
    }

    private void exch(int i, int j) {
        T tmp = heaps[i];
        heaps[i] = heaps[j];
        heaps[j] = tmp;
    }

}

@@ -298,17 +298,588 @@
 int n;%0A%0A
-%0A
+    private MaxTopHeapByArrays(int len) %7B%0A        this.n = len;%0A        heaps = (T%5B%5D) new Object%5Bthis.n + 1%5D;%0A    %7D%0A%0A    /**%0A     * %E6%8F%92%E5%85%A5%0A     * @param key%0A     */%0A    public void insert(T key) %7B%0A        if (n %3E= heaps.length - 1) %7B%0A            resize(n);%0A        %7D%0A        heaps%5B++n%5D = key;%0A        swam(n);%0A    %7D%0A%0A    /**%0A     * %E6%89%A9%E5%B1%95%E6%A0%91%0A     * @param initCapacity%0A     */%0A    private void resize(int initCapacity) %7B%0A        T%5B%5D tmp = (T%5B%5D) new Object%5B2 * initCapacity%5D;%0A%0A        for (int i = 0; i %3C= n; i++) %7B%0A            tmp%5Bi%5D = heaps%5Bi%5D;%0A        %7D%0A        heaps = tmp;%0A    %7D
 %0A%0A    /*

add the method of insert and resize