日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java Review - PriorityQueue源码解读

發布時間:2025/3/21 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Review - PriorityQueue源码解读 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • Pre
  • PriorityQueue 概述
  • PriorityQueue 繼承關系
  • PriorityQueue通過用數組表示的小頂堆實現
  • 時間復雜度
  • 構造函數
  • 方法
    • 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,還有一種特殊的隊列叫做PriorityQueue,即優先隊列。

優先隊列的作用是能保證每次取出的元素都是隊列中權值最小的

那大小關系如何評判呢? 元素大小的評判可以通過元素本身的自然順序(natural ordering),也可以通過構造時傳入的比較器


PriorityQueue 繼承關系


PriorityQueue通過用數組表示的小頂堆實現

  • PriorityQueue實現了Queue接口,不允許放入null元素

  • 其通過堆實現,具體說是通過完全二叉樹(complete binary tree)實現的小頂堆(任意一個非葉子節點的權值,都不大于其左右子節點的權值),也就意味著可以通過數組來作為PriorityQueue的底層實現

上圖中我們給每個元素按照層序遍歷的方式進行了編號, 會發現父節點和子節點的編號是有聯系的,更確切的說父子節點的編號之間有如下關系:

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

通過上述三個公式,可以輕易計算出某個節點的父節點以及子節點的下標。這也就是為什么可以直接用數組來存儲堆的原因。


時間復雜度

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

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


構造函數

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)的語義相同,都是向優先隊列中插入元素,只是Queue接口規定二者對插入失敗時的處理不同,前者在插入失敗時拋出異常,后者則會返回false。

  • 對于PriorityQueue這兩個方法其實沒什么差別。


新加入的元素可能會破壞小頂堆的性質,因此需要進行必要的調整。

//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);//自動擴容size = i + 1;if (i == 0)//隊列原來為空,這是插入的第一個元素queue[0] = e;elsesiftUp(i, e);//調整return true; }

如上,擴容函數grow()類似于ArrayList里的grow()函數,就是再申請一個更大的數組,并將原數組的元素復制過去 。

需要注意的是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)//調用比較器的比較方法break;queue[k] = e;k = parent;}queue[k] = x; }

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


element()和peek()

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

  • 根據小頂堆的性質,堆頂那個元素就是全局最小的那個;

  • 由于堆用數組表示,根據下標關系,0下標處的那個元素既是堆頂元素。所以直接返回數組0下標處的那個元素即可


remove()和poll()

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

  • 由于刪除操作會改變隊列的結構,為維護小頂堆的性質,需要進行必要的調整。

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

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

//siftDown() private void siftDown(int k, E x) {int half = size >>> 1;while (k < half) {//首先找到左右孩子中較小的那個,記錄到c里,并用child記錄其下標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取代原來的值k = child;}queue[k] = x; }


remove(Object o)

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

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

//remove(Object o) public boolean remove(Object o) {//通過遍歷數組的方式找到第一個滿足o.equals(queue[i])元素的下標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; }

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。