Java 实现最大堆

最大堆的特点是父元素比子元素大,并且是一棵完全二叉树。

堆接口:

/**
 * Create by zxb on 2017/9/10
 */
public interface IHeap<T extends Comparable<T>> {

    void display();

    void initOriginList(List<T> orginList);

    void makeHeap(int first, int last);

    void popHeap(int first, int last);

    void pushHeap(int first, int last);

    List<T> getHeap();
}
堆实现,这里采用的是动态数组来做存储

/**
 * Create by zxb on 2017/9/10
 */
public class HeapImpl<T extends Comparable<T>> implements IHeap<T> {

    private List<T> heap;

    private List<T> orginList;

    public void initOriginList(List<T> orginList) {
        this.orginList = orginList;
    }

    public List<T> getHeap() {
        return heap;
    }

    public HeapImpl() {
        this.heap = new ArrayList<>();
    }

    /**
     * 插入对应上浮
     *
     * @param start
     */
    protected void adjustUp(int start) {
        int currentIndex = start;
        int parentIndex = (currentIndex - 1) / 2;
        T tmp = heap.get(currentIndex);
        while (currentIndex > 0) {
            int cmp = heap.get(parentIndex).compareTo(tmp);
            if (cmp >= 0) {
                break;
            } else {
                heap.set(currentIndex, heap.get(parentIndex));
                currentIndex = parentIndex;
                parentIndex = (parentIndex - 1) / 2;
            }
        }
        heap.set(currentIndex, tmp);
    }

    public void insert(T data) {
        int size = heap.size();
        heap.add(data);    // 将"数组"插在表尾
        adjustUp(size);        // 向上调整堆
    }

    public void remove(int index) {
        int size = heap.size();
        heap.set(index, heap.get(size - 1));
        heap.remove(size - 1);
        adjustDown(index);
    }

    /**
     * 删除对应下沉
     *
     * @param index
     */
    private void adjustDown(int index) {
        int currentIndex = index;
        int leftChildIndex = index * 2 + 1;
        int rightChildIndex = index * 2 + 2;
        T tmp = heap.get(currentIndex);
        int size = heap.size();
        while (leftChildIndex < size) {
            T left = null;
            T right = null;
            if (leftChildIndex < size) {
                left = heap.get(leftChildIndex);
            }
            if (rightChildIndex < size) {
                right = heap.get(rightChildIndex);
            }
            int maxIndex = right == null ? leftChildIndex : (left.compareTo(right) >= 0 ? leftChildIndex : rightChildIndex);
            T max = heap.get(maxIndex);
            if(tmp.compareTo(max)>=0){
                break;
            }else{
                heap.set(currentIndex, max);
                heap.set(maxIndex, tmp);
                leftChildIndex = maxIndex * 2 +1;
                rightChildIndex = maxIndex +1;
            }
        }
    }

    public void makeHeap(int first, int last) {
        for (int i = first; i < last; i++) {
            insert(orginList.get(i));
        }
    }

    public void popHeap(int first, int last) {
        remove(first);
    }

    public void pushHeap(int first, int last) {
        adjustUp(last - 1);
    }

    public void display() {
        System.out.println(heap);
    }

    public static void main(String[] args) {
        IHeap<Integer> heap = new HeapImpl<>();
        heap.initOriginList(Arrays.asList(10, 20, 30, 5, 15));
        System.out.println("初始构建堆:");
        heap.makeHeap(0, 5);
        heap.display();
        System.out.println("弹出堆顶:");
        heap.popHeap(0, 5);
        heap.display();
        System.out.println("弹出堆顶:");
        heap.popHeap(0, 4);
        heap.display();
        System.out.println("插入堆尾:");
        heap.getHeap().add(90);
        heap.display();
        System.out.println("重新排列:");
        heap.pushHeap(0, 4);
        heap.display();
    }
}
运行结果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值