【每日一题】加强堆

题目

设计一个数据结构,1. 在插入数据时可以保证有序,2.在插入相同的数据时,更新该集合中的数据,并且依然保持有序 3. 需要提供删除集合中的指定数据

要求:所有方法的时间复杂度为O(n*logN)

解析

按照题目的要求的函数功能,可以很好实现,但是如果要求时间复杂度为O(N*logN),那么比较适合的数据结构是堆,但是系统提供的堆(PriorityQueue)不能更新堆中的节点,只能添加,所以需要设计一个加强堆

代码实现

public class HeapGreater<T> {

    private List<T> heap; // 堆数组
    private Map<T,  Integer> indexMap; // 堆的反向索引表
    private int heapSize; // 堆大小
    private Comparator<? super T> comparator; // 比较器

    public HeapGreater(Comparator<T> c) {
        heap = new ArrayList<>();
        indexMap = new HashMap<>();
        heapSize = 0;
        comparator = c;
    }

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

    public int size() {
        return heapSize;
    }

    public boolean contains(T obj) {
        // 如果用堆需要遍历,但是使用反向索引表,可以直接判断 O(1)
        return indexMap.containsKey(obj);
    }

    public T peek() {
        return heap.get(0);
    }

    public void push(T obj) {
        heap.add(obj);
        indexMap.put(obj, heapSize);
        // 调整顺序之后,将heapSize++, 用于插入下一个位置
        // 从heapSize位置开始向堆中插入数据
        heapInsert(heapSize++);
    }

    public T pop() {
        T t = heap.get(0);
        swap(0, heapSize - 1);
        indexMap.remove(t);
        heap.remove(--heapSize);
        // 从0开始向下调整堆
        heapify(0);
        return t;
    }

    public void remove(T obj) {
        T replace = heap.get(heapSize - 1);
        Integer i = indexMap.get(obj);
        indexMap.remove(obj);
        heap.remove(--heapSize);
        if(obj != replace) {
            heap.set(i, replace);
            indexMap.put(replace, i);
            resign(replace);
        }
    }

    private void resign(T replace) {
        // 向上比较进行调整replace的位置
        heapInsert(indexMap.get(replace));
        // 向下比较进行调整replace的位置
        heapify(indexMap.get(replace));
    }

    public List<T> getAllElements() {
        List<T> list = new ArrayList<>(heap);
        return list;
    }

    private void heapify(int index) {
        int left = index * 2 + 1;
        while (left < heapSize) {
            int best = left + 1 < heapSize && comparator.compare(heap.get(left + 1), heap.get(left)) < 0 ? (left + 1) : left;
            best = comparator.compare(heap.get(best), heap.get(index)) < 0 ? best : index;
            if(best == index) {
                break;
            }
            swap(best, index);
            index = best;
            left = index * 2 + 1;
        }
    }

    private void heapInsert(int index) {
        while (comparator.compare(heap.get(index), heap.get((index - 1) / 2)) < 0) {
            swap(index, (index - 1) / 2);
            index = (index - 1) / 2;
        }
    }

    private void swap(int i, int j) {
        T t1 = heap.get(i);
        T t2 = heap.get(j);
        Integer i1 = indexMap.get(t1);
        Integer i2 = indexMap.get(t2);
        heap.set(i2, t1);
        heap.set(i1, t2);
        indexMap.put(t1, i2);
        indexMap.put(t2, i1);
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值