当前位置:网站首页>PriorityQueue类的使用及底层原理
PriorityQueue类的使用及底层原理
2022-08-04 19:51:00 【像你这样的】
目录
Java中PriorityQueue通过二叉小顶堆实现,可以用一棵完全二叉树表示
上面数组对应的完全二叉树是
从上面二叉树这个图中, 我们可以发现规律 :
如果一个节点对应的数组下标是index, 则我们可以计算出他的左节点的数组下标, 右节点的数组下标, 父节点的数组下标
左节点的数组下标 : index * 2 + 1
右节点的数组下标 : index * 2 + 2
父节点的数组下标 : (index - 1) / 2
因此我们可以简单的获取值的左右节点和父节点的值, 用于值与值间的对比, 替换
add()方法的底层原理 :
add()方法中调用的就是offer()方法, 直接看offer()方法
poll()方法的底层原理 :
边栏推荐
- lds链接的 顺序问题
- Ant Group's time series database CeresDB is officially open source
- c sqlite ... ...
- Polygon zkEVM 基本概念
- 面试官:JVM运行时数据区包含哪几部分?作用是啥?
- 哈佛架构 VS 冯·诺依曼架构
- Yuanguo chain game system development
- 数据安全解决方案的发展
- Openharmony code framework (2) the person that
- Orthodontic MIA micro-implant anchorage technology China 10th anniversary exchange meeting was held in Shenyang
猜你喜欢
随机推荐
如何让远在的老板看到你!----------来自财富中国网
IIC驱动OLED
致-.-- -..- -
How to use the Chrome DevTools performance tab
《支付宝体验设计精髓》一书,跟测试相关性知识记录
如何推动乡村振兴的落地
电脑一键重装系统后连不上远程了?教你设置的方法
vscode离线安装插件方法
备忘录模式
Infrared image filtering
If it is test axi dma catch a few words here
对比几类主流的跨端技术方案
Switch node version and switch npm source tool
华为交换机:STP测试实验
To -.-- -..- -
C#弹出询问对话框
前3名突然变了,揭秘 7 月编程语言最新排行榜
The difference between Client Side Cache and Server Side Cache
How to manually download and install SAP Fiori tools - Extension Pack for Visual Studio Code
Go study notes (Part 1) Configuring the Go development environment