PriorityQueue(优先级队列总结)

PriorityQueue(优先级队列总结)一,概念队列是一种先进先出(FIFO)的数据结构,但有些情况下,操作的数据可能带有优先级,一般出队列时,可能需要优先级高的元素先出队列 数据结构应该提供两个最基本的操作,一个是返回最高优先级对象,一个是添加新的对象。这种数据结构就是优先级队列(PriorityQueue)二,PriorityQueue的特性Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlo.

大家好,又见面了,我是你们的朋友全栈君。

一,概念

  • 队列是一种先进先出(FIFO)的数据结构,但有些情况下,操作的数据可能带有优先级,一般出队列时,可能需要优先级高的元素先出队列
  • 数据结构应该提供两个最基本的操作,一个是返回最高优先级对象,一个是添加新的对象。这种数据结构就是优先级队列(Priority Queue) 

二,PriorityQueue的特性

Java集合框架中提供了PriorityQueuePriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的

三,使用时的注意事项

1. 使用时必须导入PriorityQueue所在的包

import java.util.PriorityQueue;

2. PriorityQueue中放置的元素必须要能够比较大小,不能插入无法比较大小的对象,否则会抛出ClassCastException异常

3. 不能插入null对象,否则会抛出NullPointerException

4. 没有容量限制,可以插入任意多个元素,其内部可以自动扩容

5. 插入和删除元素的时间复杂度为\log_{2}N

6. PriorityQueue底层使用了堆数据结构

7. PriorityQueue默认情况下是小堆—即每次获取到的元素都是最小的元素

四,PriorityQueue的构造器(三种)

import java.util.PriorityQueue;

public class MyPriorityQueue {
    public static void method1(){
        //不添加初始容量  默认为11
        PriorityQueue<Integer> p1 = new PriorityQueue<>();

        //添加初始容量为100
        PriorityQueue<Integer> p2 = new PriorityQueue<>(100);

        p1.offer(1);
        p1.offer(2);
        p1.offer(3);
        p1.offer(4);
        p1.offer(5);

        //使用集合容器来进行构造

        PriorityQueue<Integer> p3 = new PriorityQueue<>(p1);

        System.out.println(p3.peek());
        System.out.println(p3.size());
    }

    public static void main(String[] args) {
        method1();
    }
}

PriorityQueue(优先级队列总结)

默认情况下,PriorityQueue队列是小堆,如果需要大堆需要用户提供比较器

    public static void method2(){
        PriorityQueue<Integer> p1 = new PriorityQueue<>();

        p1.offer(10);
        p1.offer(12);
        p1.offer(14);
        p1.offer(16);
        p1.offer(18);

        System.out.println(p1.peek());

        PriorityQueue<Integer> p2 = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        p2.offer(10);
        p2.offer(12);
        p2.offer(14);
        p2.offer(16);
        p2.offer(18);

        System.out.println(p2.peek());
    }

PriorityQueue(优先级队列总结)
五,插入/删除/获取优先级队列的元素以及使用

PriorityQueue(优先级队列总结)PriorityQueue(优先级队列总结)

 

    public static void method3(){
        PriorityQueue<Integer> p = new PriorityQueue<>();

        p.offer(1);
        p.offer(2);
        p.offer(3);
        p.offer(4);
        p.offer(5);

        System.out.print(p.size() + " ");
        System.out.println(p.peek());

        p.poll();
        p.poll();

        System.out.print(p.size() + " ");
        System.out.println(p.peek());

        p.poll();
        p.poll();
        p.poll();

        System.out.print(p.size() + " ");
        System.out.print(p.peek() + " ");
        System.out.println(p.poll() + " ");
        if (p.isEmpty()){
            System.out.println("p is empty!");
        }else{
            System.out.println("p is not empty!");
        }
    }

PriorityQueue(优先级队列总结)

六,堆

1.什么是堆

JDK1.8中的PriorityQueue底层使用了堆的数据结构,而堆实际就是在完全二叉树的基础之上进行了一些元素的调整。
堆的性质:

  • 堆中某个节点的值总是不大于或不小于其父节点的值;
  • 堆总是一棵完全二叉树

PriorityQueue(优先级队列总结) 

2.堆的存储方式

堆是一棵完全二叉树,因此可以层序的规则采用顺序的方式来高效存储,
注意:对于非完全二叉树,则不适合使用顺序方式进行存储,因为为了能够还原二叉树,空间中须要存储空节点,就会导致空间利用率比较低

将元素存储到数组中后,可以根据二叉树章节的性质5对树进行还原。假设i为节点在数组中的下标,则有:

  • 如果i为0,则i表示的节点为根节点,否则i节点的双亲节点为 (i – 1)/2
  • 如果2 * i + 1 小于节点个数,则节点i的左孩子下标为2 * i + 1,否则没有左孩子
  • 如果2 * i + 2 小于节点个数,则节点i的右孩子下标为2 * i + 2,否则没有右孩子

3.堆的创建

向下调整

    public void shiftDown(int[] array,int parent){
        int child = parent * 2 + 1;
        int size = array.length;

        while(child < size){
            if ((child + 1 < size) && (array[child + 1] < array[child])){
                child = child + 1;
            }

            if (array[parent] <= array[child]){
                break;
            }else{
                int temp = array[parent];
                array[parent] = array[child];
                array[child] = temp;
                parent = child;
                child = parent * 2 + 1;
            }
        }
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

PriorityQueue(优先级队列总结)

堆的创建

    //堆的创建
    public void createHeap(int[] array){
        int root = ((array.length - 2) >> 1);
        for (int i = root; i >= 0 ; i--) {
            shiftDown(array,root);
        }
    }

堆的插入与删除

向上调整 

    public void shiftUp(int child) {
        // 找到child的双亲
        int parent = (child - 1) / 2;
        while (child > 0) {
            // 如果双亲比孩子大,parent满足堆的性质,调整结束
            if (array[parent] > array[child]) {
                break;
            }
            else{// 将双亲与孩子节点进行交换
                int t = array[parent];
                array[parent] = array[child];
                array[child] = t;
            // 小的元素向下移动,可能到值子树不满足对的性质,因此需要继续向上调增
                child = parent;
                parent = (child - 1) / 1;
            }
        }
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133261.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月1日 下午4:20
下一篇 2022年5月1日 下午4:20


相关推荐

  • 什么是移动端开发【重点学习系列—干货十足–一万字详解】

    什么是移动端开发【重点学习系列—干货十足–一万字详解】引言这一篇文章主要对移动端开发相关的基础知识点,进行总结。从移动端开发的一些概念、专有名词、缩放、viewport移动端事件、适配问题以及一些工作中沟通经常会用到这些方面来说一下移动端1-移动端开发相关概念移动端特点移动端与PC端网页有所不同,有以下几个特点小屏幕触摸交互屏幕尺寸繁多屏幕大小​屏幕大小指屏幕的对角线的长度,单位一般是英寸。常见的手机屏幕大小3.5、4…

    2022年6月24日
    40
  • Pandas的apply用法

    Pandas的apply用法defhandle time t replaced str returnre sub d 4 d 2 d 2 lambdax x group 1 replaced str x group 3 t defgen new data replaced str path data logs xlsx data pd read excel path data time data

    2026年3月17日
    2
  • 使用 For 循环遍历 Python 字典的 3 种方法

    使用 For 循环遍历 Python 字典的 3 种方法在 Python 中 如何使用 for 循环遍历字典 今天我们将会演示三种方法 并学会遍历嵌套字典 在实战前 我们需要先创建一个模拟数据的字典 dict 1 Name Zara Age 7 Class First Address Beijing 方法 1 使用 For 循环 索引进行迭代在 Python 中遍历字典的最简单方法 是将其直接放入 for 循环中 Python 会自动将 dict 1 视为字典 并允许你迭代其 key 键 然后 我们就可以使用索引

    2026年3月26日
    2
  • python实例代码爬虫_python 网络爬虫实例代码

    python实例代码爬虫_python 网络爬虫实例代码本节内容:python网络爬虫代码。一共两个文件,一个是toolbox_insight.py,是一个工具文件另一个是test.py,是一个用到toolbox_insight.py中工具的测试文件代码示例:#filename:toolbox_insight.pyfromsgmllibimportsgmlparserimportthreadingimporttimeimporturl…

    2025年5月22日
    3
  • Matlab中while语句使用

    Matlab中while语句使用目录一 语法二 说明三 示例 1 重复执行语句 直到表达式为 False2 跳至下一循环迭代 3 在表达式为 false 之前退出循环详细信息表达式 while 语句是条件为 true 时重复执行的 while 循环 一 语法 whileexpress 提示 如果意外创建了一个无限循环 即永远不会自行结束的循环 请按下 Ctrl C 停止执行循环 如果条件表达式的计算结果是一个矩阵 则仅当该矩阵中的所

    2026年3月17日
    3
  • windows开启远程桌面命令

    windows开启远程桌面命令开启远程桌面命令 关闭远程桌面 设置远程桌面端口 默认开启 3389 检查默认端口状态非命令行打开方式 1 打开 控制面板 怎么开启远程桌面连接详细教程 2 点击 系统 怎么开启远程桌面连接详细教程 3 选择 远程设置 怎么开启远程桌面连接详细教程 4 点击 远程 勾选 允许远程协助连接这台计算机 和 允许远程连接到此计算机 点击 确定 怎么开启远程桌面连接详细教程 5 在任务栏搜索 远程桌面连接 并打开 怎么开启远程桌面连接详细教程 6 输入 ip 地址后点击 连接 即可

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号