堆的应用-优先级队列(队列的实现)

堆得概念:

  1. 堆逻辑上是一棵完全二叉树
  2. 堆物理上是保存在数组中
  3. 满足任意结点的值都大于其子树中结点的值,叫做大堆,或者大根堆,或者最大堆
  4. 反之,则是小堆,或者小根堆,或者最小堆
  5. 堆的基本作用是,快速找集合中的最值
    在这里插入图片描述优先级队列概念:
    在很多应用中,我们通常需要按照优先级情况对待处理对象进行处理,比如首先处理优先级最高的对象,然后处理次高的对象。最简单的一个例子就是,在手机上玩游戏的时候,如果有来电,那么系统应该优先处理打进来的电话。
    在这种情况下,我们的数据结构应该提供两个最基本的操作,一个是返回最高优先级对象,一个是添加新的对象。
    这种数据结构就是优先级队列(Priority Queue)。
    下面是关于队列的一些操作。
public class MyPriorityQueue {
    private int[] array = new int[100]; // 暂时不考虑扩容
    private int size = 0;   // [0, size) 表示有效元素区间.

    public void offer(int x) {
        // 1. 先把 x 放到数组元素的末尾.
        array[size] = x;
        size++;
        // 2. 把 x 进行向上调整即可
        // 第一个参数表示用来承载堆的数组
        // 第二个参数表示数组中有效元素的个数.
        // 第三个参数表示从哪个位置进行向上调整.
        shiftUp(array, size, size - 1);
    }

    private void shiftUp(int[] array, int size, int index) {
        int child = index;
        int parent = (child - 1) / 2;
        // 如果 child 为 0, 说明 child 已经是根节点了. 根节点就没有父节点.
        // 调整到这里已经就到顶了.
        while (child > 0) {
            // 比较当前 child 和 parent 之间的大小关系, 看看是否符合大堆.
            if (array[parent] < array[child]) {
                // 交换父子元素的内容
                int tmp = array[parent];
                array[parent] = array[child];
                array[child] = tmp;
            } else {
                break;
            }
            child = parent;
            parent = (child - 1) / 2;
        }
    }

    public Integer poll() {
        if (size <= 0) {
            return null;
        }
        int ret = array[0];

        // 如何删除队首元素(根节点) 呢?
        // 把未知问题转换成已知问题.
        // 1. 把最后一个元素的值填入到 0 号元素上.
        array[0] = array[size - 1];
        // 2. 删除最后一个元素
        size--;
        // 3. 从 0 下标开始进行向下调整
        shiftDown(array, size, 0);
        return ret;
    }

    private void shiftDown(int[] array, int size, int index) {
        int parent = index;
        int child = 2 * parent + 1;
        while (child < size) {
            if (child + 1 < size && array[child + 1] > array[child]) {
                child = child + 1;
            }
            if (array[child] > array[parent]) {
                int tmp = array[child];
                array[child] = array[parent];
                array[parent] = tmp;
            } else {
                break;
            }
            parent = child;
            child = 2 * parent + 1;
        }
    }

    public Integer peek() {
        if (size == 0) {
            return null;
        }
        return array[0];
    }

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

    public static void main(String[] args) {

        MyPriorityQueue queue = new MyPriorityQueue();
        queue.offer(9);
        queue.offer(5);
        queue.offer(2);
        queue.offer(7);
        queue.offer(3);
        queue.offer(6);
        queue.offer(8);
        while (!queue.isEmpty()) {
            Integer cur = queue.poll();
            System.out.println(cur);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45755718/article/details/106107754