PriorityQueue 源码解析

简介

PriorityQueue是基于小顶堆实现的优先队列,它的头部总是指向当前队列中最小的元素。关于小顶堆的知识可以参考严蔚敏的《数据结构》。

    // 初始的数组长度
    private static final int DEFAULT_INITIAL_CAPACITY = 11;  

     /**
     * 优先队列实质就是小顶堆。
     * 下标为i的节点,其两个孩子节点下标为2*i+1, 2*i+2,并且  父节点小于等于孩子节点
     */
    transient Object[] queue; // non-private to simplify nested class access

    /**
     * The number of elements in the priority queue.
     */
    private int size = 0;

    /**
     * 比较器,如果没有,则使用元素的自然排序
     */
    private final Comparator<? super E> comparator;
    
    public PriorityQueue(int initialCapacity,
                         Comparator<? super E> comparator) {
        // Note: This restriction of at least one is not actually needed,
        // but continues for 1.5 compatibility
        if (initialCapacity < 1)
            throw new IllegalArgumentException();
        this.queue = new Object[initialCapacity];
        this.comparator = comparator;
    }

add()方法

将指定的元素插入到此优先级队列中。 需要向上调整满足小顶堆的性质

策略:元素添加到数组的尾部,并向上调整。

    public boolean add(E e) {
        return offer(e);
    }
    
    public boolean offer(E e) {
        if (e == null)   // 不能插入 null
            throw new NullPointerException();
        modCount++;
        int i = size;
        if (i >= queue.length)
            grow(i + 1);
        size = i + 1;
        if (i == 0)  // 首次插入
            queue[0] = e;
        else
            siftUp(i, e);
        return true;
    }
    
    /**
     * 插入x至位置k。
     * 为了保持小顶堆的性质,需要进行向上调整
     */
    private void siftUp(int k, E x) {
        if (comparator != null)
            siftUpUsingComparator(k, x);  //有比较器
        else
            siftUpComparable(k, x);  // 无比较器
    }
    
    // 向上调整的算法
    private void siftUpComparable(int k, E x) {
        Comparable<? super E> key = (Comparable<? super E>) x;
        while (k > 0) {
            int parent = (k - 1) >>> 1;
            Object e = queue[parent];
            if (key.compareTo((E) e) >= 0)
                break;
            queue[k] = e;
            k = parent;
        }
        queue[k] = key;
    }
    
    // 向上调整的算法
    private void siftUpUsingComparator(int k, E x) {
        while (k > 0) {
            int parent = (k - 1) >>> 1;
            Object e = queue[parent];
            if (comparator.compare(x, (E) e) >= 0)  // x比父节点大
                break;
            queue[k] = e;
            k = parent;
        }
        queue[k] = x;
    }

poll()方法

检索并删除此队列的头,如果此队列为空,则返回 null 。

策略:将队尾元素赋值到队头,并向下调整

    public E poll() {
        if (size == 0)
            return null;
        int s = --size;
        modCount++;
        E result = (E) queue[0]; // 队头
        E x = (E) queue[s];  // 队尾
        queue[s] = null;
        if (s != 0)
            siftDown(0, x);
        return result;
    }
    
    //向下调整
    private void siftDown(int k, E x) {
        if (comparator != null)
            siftDownUsingComparator(k, x);
        else
            siftDownComparable(k, x);
    }
    
    private void siftDownComparable(int k, E x) {
        Comparable<? super E> key = (Comparable<? super E>)x;
        int half = size >>> 1;        // loop while a non-leaf
        while (k < half) {   // 只有前一半有孩子节点,故 k < half
            int child = (k << 1) + 1; // assume left child is least
            Object c = queue[child];
            int right = child + 1;
            if (right < size &&
                ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
                c = queue[child = right];
            if (key.compareTo((E) c) <= 0)
                break;
            queue[k] = c;
            k = child;
        }
        queue[k] = key;
    }

    private void siftDownUsingComparator(int k, E x) {
        int half = size >>> 1;
        while (k < half) {   
            int child = (k << 1) + 1;
            Object c = queue[child];
            int right = child + 1;
            if (right < size &&
                comparator.compare((E) c, (E) queue[right]) > 0)
                c = queue[child = right];
            if (comparator.compare(x, (E) c) <= 0)
                break;
            queue[k] = c;
            k = child;
        }
        queue[k] = x;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值