Java Review - PriorityQueue源码解读
文章目錄
- 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)整。
如上,擴(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)整。
-
先記錄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è)為止。
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()即可.
總結(jié)
以上是生活随笔為你收集整理的Java Review - PriorityQueue源码解读的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java Review - Queue和
- 下一篇: Java Review - HashMa