前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >优先队列

优先队列

作者头像
shysh95
发布2020-05-17 20:10:16
2710
发布2020-05-17 20:10:16
举报
文章被收录于专栏:shysh95shysh95

队列是一种先进先出的结构,队列末尾插入,队列开头出队。但是优先队列是什么呢?优先队列打破了队列的特性,有两种优先队列:

  • 最大优先队列:无论入队顺序如何,出队时都是最大元素出队
  • 最小优先队列:无论入队顺序如何,出队时都是最小元素出队

最大优先队列可以使用最大堆进行实现,每一次入队操作都是堆的插入操作,每一次出队都是删除堆顶节点。

代码语言:txt
复制
    public class PriorityQueue {


        private int[] array;


        private int size;


        public PriorityQueue(int size) {

            array = new int[size];

        }


        public void enqueue(int value) {

            if (size >= array.length) {

                resize();

            }

            array[size++] = value;

            upAdjust(array);

        }


        public int dequeue() throws Exception {

            if (size < 0) {

                throw new Exception("queue is empty!");

            }

            int head = array[0];

            array[0] = array[--size];

            downAdjust();

            return head;

        }


        private void upAdjust(int[] array) {

            int childIndex = size - 1;

            int parentIndex = (childIndex - 1) / 2;

            int temp = array[childIndex];

            while (childIndex > 0 && array[parentIndex] < array[childIndex]) {

                array[childIndex] = array[parentIndex];

                childIndex = parentIndex;

                parentIndex = (childIndex - 1) / 2;

            }

            array[childIndex] = temp;

        }


        private void downAdjust() {

            int parenIndex = 0;

            int childIndex = 2 * parenIndex + 1;

            int temp = array[parenIndex];

            while (parenIndex < size) {

                if (childIndex + 1 < size && array[childIndex + 1] > array[childIndex]) {

                    childIndex ++;

                }

                if (temp >= array[childIndex]) {

                    break;

                }

                array[parenIndex] = array[childIndex];

                parenIndex = childIndex;

                childIndex = 2 * parenIndex + 1;

            }

            array[parenIndex] = temp;

        }


        private void resize() {

            int newSize = this.size * 2;

            this.array = Arrays.copyOf(this.array, newSize);

        }


        @Override

        public String toString() {

            return Arrays.toString(array);

        }


        public static void main(String[] args) throws Exception {

            PriorityQueue priorityQueue = new PriorityQueue(32);

            priorityQueue.enqueue(3);

            priorityQueue.enqueue(5);

            priorityQueue.enqueue(10);

            priorityQueue.enqueue(2);

            priorityQueue.enqueue(7);

            System.out.println(priorityQueue.toString());

            System.out.println("出队:" + priorityQueue.dequeue());

            System.out.println("出队:" + priorityQueue.dequeue());

        }

    }
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员修炼笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档