日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java Review - PriorityQueue源码解读

發(fā)布時(shí)間:2025/3/21 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Review - PriorityQueue源码解读 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • Pre
  • PriorityQueue 概述
  • PriorityQueue 繼承關(guān)系
  • PriorityQueue通過(guò)用數(shù)組表示的小頂堆實(shí)現(xiàn)
  • 時(shí)間復(fù)雜度
  • 構(gòu)造函數(shù)
  • 方法
    • add()和offer()
    • element()和peek()
    • remove()和poll()
    • remove(Object o)


Pre

Java Review - ArrayList 源碼解讀

Java Review - LinkedList源碼解讀

Java Review - Queue和Stack 源碼解讀


PriorityQueue 概述

Java Review - Queue和Stack 源碼解讀以Java ArrayDeque為例講解了Stack和Queue,還有一種特殊的隊(duì)列叫做PriorityQueue,即優(yōu)先隊(duì)列。

優(yōu)先隊(duì)列的作用是能保證每次取出的元素都是隊(duì)列中權(quán)值最小的

那大小關(guān)系如何評(píng)判呢? 元素大小的評(píng)判可以通過(guò)元素本身的自然順序(natural ordering),也可以通過(guò)構(gòu)造時(shí)傳入的比較器


PriorityQueue 繼承關(guān)系


PriorityQueue通過(guò)用數(shù)組表示的小頂堆實(shí)現(xiàn)

  • PriorityQueue實(shí)現(xiàn)了Queue接口,不允許放入null元素

  • 其通過(guò)堆實(shí)現(xiàn),具體說(shuō)是通過(guò)完全二叉樹(shù)(complete binary tree)實(shí)現(xiàn)的小頂堆(任意一個(gè)非葉子節(jié)點(diǎn)的權(quán)值,都不大于其左右子節(jié)點(diǎn)的權(quán)值),也就意味著可以通過(guò)數(shù)組來(lái)作為PriorityQueue的底層實(shí)現(xiàn)

上圖中我們給每個(gè)元素按照層序遍歷的方式進(jìn)行了編號(hào), 會(huì)發(fā)現(xiàn)父節(jié)點(diǎn)和子節(jié)點(diǎn)的編號(hào)是有聯(lián)系的,更確切的說(shuō)父子節(jié)點(diǎn)的編號(hào)之間有如下關(guān)系:

leftNo = parentNo*2+1 rightNo = parentNo*2+2 parentNo = (nodeNo-1)/2

通過(guò)上述三個(gè)公式,可以輕易計(jì)算出某個(gè)節(jié)點(diǎn)的父節(jié)點(diǎn)以及子節(jié)點(diǎn)的下標(biāo)。這也就是為什么可以直接用數(shù)組來(lái)存儲(chǔ)堆的原因。


時(shí)間復(fù)雜度

  • peek()和element操作是常數(shù)時(shí)間,

  • add(), offer(), 無(wú)參數(shù)的remove()以及poll()方法的時(shí)間復(fù)雜度都是log(N)


構(gòu)造函數(shù)

private static final int DEFAULT_INITIAL_CAPACITY = 11;/*** The comparator, or null if priority queue uses elements'* natural ordering.*/private final Comparator<? super E> comparator;/*** Creates a {@code PriorityQueue} with the default initial* capacity (11) that orders its elements according to their* {@linkplain Comparable natural ordering}.*/public PriorityQueue() {this(DEFAULT_INITIAL_CAPACITY, null);}/*** Creates a {@code PriorityQueue} with the specified initial* capacity that orders its elements according to their* {@linkplain Comparable natural ordering}.** @param initialCapacity the initial capacity for this priority queue* @throws IllegalArgumentException if {@code initialCapacity} is less* than 1*/public PriorityQueue(int initialCapacity) {this(initialCapacity, null);}/*** Creates a {@code PriorityQueue} with the default initial capacity and* whose elements are ordered according to the specified comparator.** @param comparator the comparator that will be used to order this* priority queue. If {@code null}, the {@linkplain Comparable* natural ordering} of the elements will be used.* @since 1.8*/public PriorityQueue(Comparator<? super E> comparator) {this(DEFAULT_INITIAL_CAPACITY, comparator);}/*** Creates a {@code PriorityQueue} with the specified initial capacity* that orders its elements according to the specified comparator.** @param initialCapacity the initial capacity for this priority queue* @param comparator the comparator that will be used to order this* priority queue. If {@code null}, the {@linkplain Comparable* natural ordering} of the elements will be used.* @throws IllegalArgumentException if {@code initialCapacity} is* less than 1*/public PriorityQueue(int initialCapacity,Comparator<? super E> comparator) {// Note: This restriction of at least one is not actually needed,// but continues for 1.5 compatibilityif (initialCapacity < 1)throw new IllegalArgumentException();this.queue = new Object[initialCapacity];this.comparator = comparator;}/*** Creates a {@code PriorityQueue} containing the elements in the* specified collection. If the specified collection is an instance of* a {@link SortedSet} or is another {@code PriorityQueue}, this* priority queue will be ordered according to the same ordering.* Otherwise, this priority queue will be ordered according to the* {@linkplain Comparable natural ordering} of its elements.** @param c the collection whose elements are to be placed* into this priority queue* @throws ClassCastException if elements of the specified collection* cannot be compared to one another according to the priority* queue's ordering* @throws NullPointerException if the specified collection or any* of its elements are null*/@SuppressWarnings("unchecked")public PriorityQueue(Collection<? extends E> c) {if (c instanceof SortedSet<?>) {SortedSet<? extends E> ss = (SortedSet<? extends E>) c;this.comparator = (Comparator<? super E>) ss.comparator();initElementsFromCollection(ss);}else if (c instanceof PriorityQueue<?>) {PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;this.comparator = (Comparator<? super E>) pq.comparator();initFromPriorityQueue(pq);}else {this.comparator = null;initFromCollection(c);}}/*** Creates a {@code PriorityQueue} containing the elements in the* specified priority queue. This priority queue will be* ordered according to the same ordering as the given priority* queue.** @param c the priority queue whose elements are to be placed* into this priority queue* @throws ClassCastException if elements of {@code c} cannot be* compared to one another according to {@code c}'s* ordering* @throws NullPointerException if the specified priority queue or any* of its elements are null*/@SuppressWarnings("unchecked")public PriorityQueue(PriorityQueue<? extends E> c) {this.comparator = (Comparator<? super E>) c.comparator();initFromPriorityQueue(c);}/*** Creates a {@code PriorityQueue} containing the elements in the* specified sorted set. This priority queue will be ordered* according to the same ordering as the given sorted set.** @param c the sorted set whose elements are to be placed* into this priority queue* @throws ClassCastException if elements of the specified sorted* set cannot be compared to one another according to the* sorted set's ordering* @throws NullPointerException if the specified sorted set or any* of its elements are null*/@SuppressWarnings("unchecked")public PriorityQueue(SortedSet<? extends E> c) {this.comparator = (Comparator<? super E>) c.comparator();initElementsFromCollection(c);}

方法

add()和offer()

  • add(E e)和offer(E e)的語(yǔ)義相同,都是向優(yōu)先隊(duì)列中插入元素,只是Queue接口規(guī)定二者對(duì)插入失敗時(shí)的處理不同,前者在插入失敗時(shí)拋出異常,后者則會(huì)返回false。

  • 對(duì)于PriorityQueue這兩個(gè)方法其實(shí)沒(méi)什么差別。


新加入的元素可能會(huì)破壞小頂堆的性質(zhì),因此需要進(jìn)行必要的調(diào)整。

//offer(E e) public boolean offer(E e) {if (e == null)//不允許放入null元素throw new NullPointerException();modCount++;int i = size;if (i >= queue.length)grow(i + 1);//自動(dòng)擴(kuò)容size = i + 1;if (i == 0)//隊(duì)列原來(lái)為空,這是插入的第一個(gè)元素queue[0] = e;elsesiftUp(i, e);//調(diào)整return true; }

如上,擴(kuò)容函數(shù)grow()類(lèi)似于ArrayList里的grow()函數(shù),就是再申請(qǐng)一個(gè)更大的數(shù)組,并將原數(shù)組的元素復(fù)制過(guò)去 。

需要注意的是siftUp(int k, E x)方法,該方法用于插入元素x并維持堆的特性。

//siftUp() private void siftUp(int k, E x) {while (k > 0) {int parent = (k - 1) >>> 1;//parentNo = (nodeNo-1)/2Object e = queue[parent];if (comparator.compare(x, (E) e) >= 0)//調(diào)用比較器的比較方法break;queue[k] = e;k = parent;}queue[k] = x; }

新加入的元素x可能會(huì)破壞小頂堆的性質(zhì),因此需要進(jìn)行調(diào)整。調(diào)整的過(guò)程為 : 從k指定的位置開(kāi)始,將x逐層與當(dāng)前點(diǎn)的parent進(jìn)行比較并交換,直到滿(mǎn)足x >= queue[parent]為止。注意這里的比較可以是元素的自然順序,也可以是依靠比較器的順序。


element()和peek()

//peek() public E peek() {if (size == 0)return null;return (E) queue[0];//0下標(biāo)處的那個(gè)元素就是最小的那個(gè) }
  • element()和peek()的語(yǔ)義完全相同,都是獲取但不刪除隊(duì)首元素,也就是隊(duì)列中權(quán)值最小的那個(gè)元素,二者唯一的區(qū)別是當(dāng)方法失敗時(shí)前者拋出異常,后者返回null。

  • 根據(jù)小頂堆的性質(zhì),堆頂那個(gè)元素就是全局最小的那個(gè);

  • 由于堆用數(shù)組表示,根據(jù)下標(biāo)關(guān)系,0下標(biāo)處的那個(gè)元素既是堆頂元素。所以直接返回?cái)?shù)組0下標(biāo)處的那個(gè)元素即可


remove()和poll()

  • remove()和poll()方法的語(yǔ)義也完全相同,都是獲取并刪除隊(duì)首元素,區(qū)別是當(dāng)方法失敗時(shí)前者拋出異常,后者返回null。

  • 由于刪除操作會(huì)改變隊(duì)列的結(jié)構(gòu),為維護(hù)小頂堆的性質(zhì),需要進(jìn)行必要的調(diào)整。

public E poll() {if (size == 0)return null;int s = --size;modCount++;E result = (E) queue[0];//0下標(biāo)處的那個(gè)元素就是最小的那個(gè)E x = (E) queue[s];queue[s] = null;if (s != 0)siftDown(0, x);//調(diào)整return result; }
  • 先記錄0下標(biāo)處的元素,并用最后一個(gè)元素替換0下標(biāo)位置的元素,之后調(diào)用siftDown()方法對(duì)堆進(jìn)行調(diào)整,最后返回原來(lái)0下標(biāo)處的那個(gè)元素(也就是最小的那個(gè)元素)。

  • 重點(diǎn)是siftDown(int k, E x)方法,該方法的作用是從k指定的位置開(kāi)始,將x逐層向下與當(dāng)前點(diǎn)的左右孩子中較小的那個(gè)交換,直到x小于或等于左右孩子中的任何一個(gè)為止

//siftDown() private void siftDown(int k, E x) {int half = size >>> 1;while (k < half) {//首先找到左右孩子中較小的那個(gè),記錄到c里,并用child記錄其下標(biāo)int child = (k << 1) + 1;//leftNo = parentNo*2+1Object c = queue[child];int right = child + 1;if (right < size &&comparator.compare((E) c, (E) queue[right]) > 0)c = queue[child = right];if (comparator.compare(x, (E) c) <= 0)break;queue[k] = c;//然后用c取代原來(lái)的值k = child;}queue[k] = x; }


remove(Object o)

  • remove(Object o)方法用于刪除隊(duì)列中跟o相等的某一個(gè)元素(如果有多個(gè)相等,只刪除一個(gè)),該方法不是Queue接口內(nèi)的方法,而是Collection接口的方法。由于刪除操作會(huì)改變隊(duì)列結(jié)構(gòu),所以要進(jìn)行調(diào)整;

  • 又由于刪除元素的位置可能是任意的,所以調(diào)整過(guò)程比其它函數(shù)稍加繁瑣。具體來(lái)說(shuō),remove(Object o)可以分為2種情況: 1. 刪除的是最后一個(gè)元素。直接刪除即可,不需要調(diào)整。2. 刪除的不是最后一個(gè)元素,從刪除點(diǎn)開(kāi)始以最后一個(gè)元素為參照調(diào)用一次siftDown()即可.

//remove(Object o) public boolean remove(Object o) {//通過(guò)遍歷數(shù)組的方式找到第一個(gè)滿(mǎn)足o.equals(queue[i])元素的下標(biāo)int i = indexOf(o);if (i == -1)return false;int s = --size;if (s == i) //情況1queue[i] = null;else {E moved = (E) queue[s];queue[s] = null;siftDown(i, moved);//情況2......}return true; }

總結(jié)

以上是生活随笔為你收集整理的Java Review - PriorityQueue源码解读的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品h | 成年人黄色录像 | 一级在线 | 日本成片网 | 成人av一级 | 欧美成人另类 | 久久久久成人精品免费播放动漫 | 欧美成人日韩 | 亚洲v欧美v另类v综合v日韩v | www..com黄色| 美国成人av | 国产特级视频 | 狠狠操操 | 奇米影音 | 青青草综合在线 | 日本老妇高潮乱hd | 日本一区二区三区免费观看 | 91噜噜噜 | 另类天堂 | 中国女人一级片 | 成人看片免费 | 久久久久无码国产精品一区 | 亚洲国产精品无码久久 | 在线免费观看一区二区 | 国产一区二区网站 | 歪歪视频在线观看 | 欧美精品在线一区 | 久久伊人亚洲 | 日本乱子伦 | 精品一区久久久 | 久久综合桃花网 | 波多野结衣一区二区三区中文字幕 | 夜夜高潮夜夜爽 | 欧美作爱视频 | 国产精品丝袜黑色高跟鞋的设计特点 | 黄色片在线免费 | 精品国产鲁一鲁一区二区三区 | www.五月激情 | 插入综合网 | 猛1被调教成公厕尿便失禁网站 | 亚洲色图视频网站 | 亚洲一区人妻 | 国产啪视频 | 国产精品第7页 | 亚洲麻豆视频 | 五月婷婷婷 | 国产美女视频一区二区 | 亚洲欧美成人综合 | 一区不卡av | 久久久性色精品国产免费观看 | 不卡av在线免费观看 | 亚洲资源在线 | 18禁肉肉无遮挡无码网站 | 色综合天天综合网国产成人网 | 五月激情久久 | 成年人视频在线 | 亚洲成人a v | 国产精品乱码一区 | 中文字幕不卡av | 白嫩情侣偷拍呻吟刺激 | 欧美毛片免费看 | 嫩草网站 | 另类专区欧美 | 操她视频在线观看 | 4438亚洲最大 | 波多av | 中文字幕日韩精品亚洲一区小树林 | 欧美用舌头去添高潮 | 国产精品sm调教免费专区 | 蜜桃一区二区 | 青青草福利 | 小色哥网站| 乱色视频 | 免费成人毛片 | 中文字幕在线观 | 亚洲国产精品女人 | 黄色av观看 | 91射区| xxx在线播放 | 极品女神无套呻吟啪啪 | 久久泄欲网 | 可以直接看的毛片 | 亚洲无码乱码精品国产 | 亚洲www啪成人一区二区麻豆 | 日韩大片免费观看 | 日韩美女视频网站 | 少妇专区 | 色欲狠狠躁天天躁无码中文字幕 | 日本黄色xxx | 国产中文字幕三区 | 台湾三级伦理片 | 日本特黄特黄刺激大片 | 久草最新| 国产精品无码久久久久高潮 | 可以免费看av | 成人福利一区二区三区 | 91看片在线观看 | 成人av软件| 亚洲一区二区 |