堆排序实现优先级队列


 * Created on 2022/9/1.
 *
 * @author lan
 */
@SuppressWarnings("unchecked")
public class PriorityQueue<T extends Comparable<T>> {

    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(Arrays.asList(5, 7, 9, 1, 3, 4, 6, 2, 0));
        System.out.println(queue);
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }

    private Object[] queue;
    private int len;

    public PriorityQueue() {
    }

    public PriorityQueue(Collection<T> collection) {
        for (T t : collection) {
            if (t == null) {
                throw new NullPointerException();
            }
        }
        queue = collection.toArray(new Object[]{});
        len = queue.length;
        heapify();
    }

    public void addAll(Collection<T> collection) {
        if (collection != null && !collection.isEmpty()) {
            ensureCapacity(collection.size());
            for (T t : collection) {
                if (t == null) {
                    throw new NullPointerException();
                }
                doAdd(t);
            }
        }
    }

    private void heapify() {
        for (int i = len / 2 - 1; i >= 0; i--) {
            siftDown(i);
        }
    }


    /**
     * 根据变动的位置,自下而上检查堆
     */
    private void siftUp(int i) {
        int parent = (i - 1) / 2;
        if (parent >= 0) {
            if (bigger(i, parent)) {
                swap(i, parent);
                siftUp(parent);
            }
        }
    }

    /**
     * 自上而下检查堆
     */
    private void siftDown(int i) {
        int left = i * 2 + 1;
        int right = i * 2 + 2;
        int largest = i;
        if (left < len && bigger(left, largest)) {
            largest = left;
        }
        if (right < len && bigger(right, largest)) {
            largest = right;
        }
        if (largest != i) {
            swap(largest, i);
            siftDown(largest);
        }
    }

    private boolean bigger(int i, int j) {
        T ti = (T) queue[i];
        T tj = (T) queue[j];
        return ti.compareTo(tj) > 0;
    }

    private void doAdd(T e) {
        queue[len++] = e;
        siftUp(len - 1);
    }

    private void swap(int i, int j) {
        Object tmp = queue[i];
        queue[i] = queue[j];
        queue[j] = tmp;
    }

    public void add(T e) {
        if (e == null) {
            throw new NullPointerException();
        }
        ensureCapacity(1);
        doAdd(e);
    }

    public T poll() {
        if (isEmpty()) {
            return null;
        }
        T res = (T) queue[0];
        queue[0] = queue[len - 1];
        len--;
        siftDown(0);
        return res;
    }

    public T peek() {
        return isEmpty() ? null : (T) queue[0];
    }

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

    private void ensureCapacity(int required) {
        if (queue == null) {
            queue = new Object[required * 2];
        } else {
            if (len + required > queue.length) {
                Object[] _queue = new Object[queue.length * 2];
                System.arraycopy(queue, 0, _queue, 0, len);
                queue = _queue;
            }
        }
    }

    @Override
    public String toString() {
        return "PriorityQueue{" +
                "queue=" + Arrays.toString(queue) +
                ", len=" + len +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lanicc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值