๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Java] PriorityQueue

์ถœ์ฒ˜

ChatGPT

์ž๋ฐ” 8 ์†Œ์Šค์ฝ”๋“œ


PriorityQueue๋Š” ์ž๋ฐ”์˜ ํ(queue) ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜๋กœ, ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์š”์†Œ๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” ํ์ด๋‹ค. PriorityQueue๋Š” ํž™(Heap) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๋ฉฐ, ์‚ฝ์ž…๋œ ์š”์†Œ๋“ค์„ ํŠน์ • ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜์—ฌ, ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๊ฐ€ ๋จผ์ € ์ฒ˜๋ฆฌ๋˜๋„๋ก ํ•œ๋‹ค.

 

1. PriorityQueue์˜ ์ฃผ์š” ํŠน์ง•

์šฐ์„ ์ˆœ์œ„ ๊ธฐ๋ฐ˜

PriorityQueue๋Š” ์ผ๋ฐ˜์ ์ธ ํ์™€ ๋‹ฌ๋ฆฌ, ์š”์†Œ๋“ค์ด ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ •๋ ฌ๋œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋ฉฐ, Comparator๋ฅผ ์ œ๊ณตํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ •์˜ ์šฐ์„ ์ˆœ์œ„๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํž™(Heap)  ์ž๋ฃŒ๊ตฌ์กฐ

๋‚ด๋ถ€์ ์œผ๋กœ ์ด์ง„ ํž™(Binary Heap) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด ์š”์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•œ๋‹ค. ํž™์€ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ์˜ ํ•œ ์ข…๋ฃŒ๋กœ, ํŠธ๋ฆฌ์—์„œ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ž์‹ ๋…ธ๋“œ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค. ์ด๋ฅผ ํ†ตํ•ด O(log n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

์ •๋ ฌ ์ˆœ์„œ

PrioriotyQueue๋Š” ํ์˜ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ์š”์†Œ๋“ค์ด ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ์ •๋ ฌ๋˜์ง€๋งŒ, toString()์ด๋‚˜ ๋ฐ˜๋ณต์ž๋ฅผ ํ†ตํ•ด ์ˆœํšŒํ•  ๋•Œ๋Š” ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค๋งŒ, ํ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋Š” ํ•ญ์ƒ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์š”์†Œ์ด๋‹ค.

null ์š”์†Œ ํ—ˆ์šฉ ์•ˆ ํ•จ

PriorityQueue๋Š” null ์š”์†Œ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Š” ์šฐ์„ ์ˆœ์œ„ ๋น„๊ต์‹œ null ๊ฐ’์ด ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

2. PriorityQueue์˜ ๊ธฐ๋ณธ ๋™์ž‘

์‚ฝ์ž… ( offer ๋˜๋Š” add() ๋ฉ”์†Œ๋“œ )

ํ์— ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ, ํ•ด๋‹น ์š”์†Œ๋Š” ํž™์„ ์ด์šฉํ•ด ์ ์ ˆํ•œ ์œ„์น˜์— ๋ฐฐ์น˜๋˜์–ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ์œ ์ง€ํ•œ๋‹ค. ์‚ฝ์ž…๋œ ์š”์†Œ๋Š” ํž™์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ๋น„๊ต๋˜์–ด ์ ์ ˆํ•œ ์œ„์น˜๋กœ ์˜ฌ๋ผ๊ฐ€๊ฑฐ๋‚˜ ๋‚ด๋ ค๊ฐ„๋‹ค.

์ œ๊ฑฐ ( poll ๋ฉ”์†Œ๋“œ )

ํ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ, ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  ์ œ๊ฑฐ๋œ๋‹ค. ํž™์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์œ„์น˜๋กœ ์ด๋™ํ•œ ํ›„, ๋‹ค์‹œ ํž™์˜ ํŠน์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ํž™ ๋‹ค์šด(Heap Down) ์ž‘์—…์ด ์ง„ํ–‰๋œ๋‹ค.

์กฐํšŒ ( peek ๋ฉ”์†Œ๋“œ )

ํ์˜ ๋งต ์•ž ์š”์†Œ(์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์š”์†Œ)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋˜, ํ์—์„œ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค.

 

3. PriorityQueue ๋ฉ”์†Œ๋“œ

1. ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ

boolean offer(E e)

ํ์— ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•˜๋ฉฐ, ์„ฑ๊ณต์ ์œผ๋กœ ์‚ฝ์ž…๋˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜๋‚˜๋‹ค.

E poll

ํ์—์„œ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ๊ฐ€ ๋น„์–ด ์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

2. ์กฐํšŒ

E peek()

ํ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ(์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์š”์†Œ)๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, ํ์—์„œ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ํ๊ฒŒ ๋น„์–ด ์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

3. ๊ธฐํƒ€ ๋ฉ”์†Œ๋“œ

int size()

ํ์— ์žˆ๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

boolean isEmpty()

ํ๊ฐ€ ๋น„์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

 

4. PriorityQueue์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„

PriorityQueue๋Š” ํž™(Heap) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„๋˜๋ฉด, ๋‚ด๋ถ€์ ์œผ๋กœ ๋™์  ๋ฐฐ์—ด(Object[])์„ ์‚ฌ์šฉํ•ด ์š”์†Œ๋“ค์„ ์ €์žฅํ•œ๋‹ค. ํž™์€ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ์ตœ์†Œ ํž™(min-heap) ๋˜๋Š” ์ตœ๋Œ€ ํž™(max-heap)์œผ๋กœ ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ๋‹ค. PriorityQueue๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ตœ์†Œ ํž™์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. 

1. ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ํž™ ๊ตฌํ˜„

PriorityQueue๋Š” ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค. ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๋ถ€๋ชจ์™€ ์ž์‹๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค : (i-1) /2
  • ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค : 2 * i + 1
  • ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค : 2 * i + 1

2. ์‚ฝ์ž… ์—ฐ์‚ฐ

PriorityQueue์— ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ๋Š” ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰์— ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•œ ํ›„, ํž™ ์—…(Heap Up) ์ž‘์—…์„ ํ†ตํ•ด ์ ์ ˆํ•œ ์œ„์น˜๋กœ ์ด๋™์‹œํ‚จ๋‹ค. ์ด๋Š ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ๋น„๊ตํ•ด ์ ์ ˆํ•œ ์œ„์น˜์— ์žˆ์„ ๋•Œ๊ฐ€์ง€ ๋ฐ˜๋ณต๋œ๋‹ค.

 

์ž๋ฐ” 8 PriorityQueue ์†Œ์Šค์ฝ”๋“œ

/**
 * Inserts the specified element into this priority queue.
 * 
 * Returns:
 * true (as specified by Collection.add)
 * Throws: 
 * ClassCastException – if the specified element cannot be 
 * compared with elements currently in this priority queue 
 * according to the priority queue's ordering
 * Throws: 
 * NullPointerException – if the specified element is null
 */
public boolean add(E e) {
        return offer(e);
}

/**
 * Inserts the specified element into this priority queue.
 *
 * Returns:
 * true (as specified by Queue.offer)
 * Throws:
 * ClassCastException – if the specified element cannot be 
 * compared with elements currently in this priority queue 
 * according to the priority queue's ordering
 * Throws:
 * NullPointerException – if the specified element is null
 */
public boolean offer(E e) {
    if (e == null)
        throw new NullPointerException();
    modCount++;
    int i = size;
    if (i >= queue.length)
        grow(i + 1);
    size = i + 1;
    if (i == 0)
        queue[0] = e;
    else
        siftUp(i, e);
    return true;
}



/**
 * Inserts item x at position k, maintaining heap invariant by 
 *  promoting x up the tree until it is greater than or equal to 
 *  its parent, or is the root. 
 *
 *  To simplify and speed up coercions and comparisons. the 
 *  Comparable and Comparator versions are separated into different 
 *  methods that are otherwise identical. (Similarly for siftDown.)
 *
 * Params:
 *  k – the position to fill
 *  x – the item to insert
 */
private void siftUp(int k, E x) {
    if (comparator != null)
        siftUpUsingComparator(k, x);
    else
        siftUpComparable(k, x);
}

@SuppressWarnings("unchecked")
private void siftUpUsingComparator(int k, E x) {
    while (k > 0) {
        int parent = (k - 1) >>> 1;
        Object e = queue[parent];
        if (comparator.compare(x, (E) e) >= 0)
            break;
        queue[k] = e;
        k = parent;
    }
    queue[k] = x;
}

@SuppressWarnings("unchecked")
private void siftUpComparable(int k, E x) {
    Comparable<? super E> key = (Comparable<? super E>) x;
    while (k > 0) {
        int parent = (k - 1) >>> 1;
        Object e = queue[parent];
        if (key.compareTo((E) e) >= 0)
            break;
        queue[k] = e;
        k = parent;
    }
    queue[k] = key;
}

3. ์‚ญ์ œ ์—ฐ์‚ฐ

๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ ์š”์†Œ(๋ฃจํŠธ ๋…ธ๋“œ)๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋Š” ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฃจํŠธ ์œ„์น˜๋กœ ์ด๋™์‹œํ‚จ ํ›„, ํž™ ๋‹ค์šด(Heap Down) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด ํž™์˜ ํŠน์„ฑ์„ ์œ ์ง€ํ•œ๋‹ค.

@SuppressWarnings("unchecked")
public E poll() {
    if (size == 0)
        return null;
    int s = --size;
    modCount++;
    E result = (E) queue[0];
    E x = (E) queue[s];
    queue[s] = null;
    if (s != 0)
        siftDown(0, x);
    return result;
}

/**
 * Inserts item x at position k, maintaining heap invariant by 
 * demoting x down the tree repeatedly until it is less than or 
 * equal to its children or is a leaf.
 *
 * Params:
 * k – the position to fill
 * Params:
 *  x – the item to insert
 */
private void siftDown(int k, E x) {
    if (comparator != null)
        siftDownUsingComparator(k, x);
    else
        siftDownComparable(k, x);
}

@SuppressWarnings("unchecked")
private void siftDownUsingComparator(int k, E x) {
    int half = size >>> 1;
    while (k < half) {
        int child = (k << 1) + 1;
        Object 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;
        k = child;
    }
    queue[k] = x;
}

@SuppressWarnings("unchecked")
private void siftDownComparable(int k, E x) {
    Comparable<? super E> key = (Comparable<? super E>)x;
    int half = size >>> 1;        // loop while a non-leaf
    while (k < half) {
        int child = (k << 1) + 1; // assume left child is least
        Object c = queue[child];
        int right = child + 1;
        if (right < size &&
            ((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
            c = queue[child = right];
        if (key.compareTo((E) c) <= 0)
            break;
        queue[k] = c;
        k = child;
    }
    queue[k] = key;
}

4. ํ™•์žฅ ๋ฐ ๋™์  ๋ฐฐ์—ด ๊ด€๋ฆฌ

PriorityQueue๋Š” ๋‚ด๋ถ€ ๋ฐฐ์—ด์ด ๊ฐ€๋“ ์ฐจ๋ฉด ์ž๋™์œผ๋กœ ๋™์  ๋ฐฐ์—ด ํ™•์žฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ฐฐ์—ด ํฌ๊ธฐ๋Š” ๊ธฐ์กด ํฌ๊ธฐ์˜ 1.5๋ฐฐ๋กœ ์ฆ๊ฐ€ํ•˜๋ฉฐ, ์ด๋Š” Arrays.copyOf() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ณต์‚ฌ๋œ๋‹ค.

/**
 * Increases the capacity of the array.
 *
 * Params:
 * minCapacity – the desired minimum capacity
 */
private void grow(int minCapacity) {
    int oldCapacity = queue.length;
    // Double size if small; else grow by 50%
    int newCapacity = oldCapacity + ((oldCapacity < 64) ?
                                     (oldCapacity + 2) :
                                     (oldCapacity >> 1));
    // overflow-conscious code
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    queue = Arrays.copyOf(queue, newCapacity);
}

5. PriorityQueue์˜ ์˜ˆ์‹œ

import java.util.PriorityQueue;

public class PriorityQueueExample {
    public static void main(String[] args) {
        // ๊ธฐ๋ณธ์ ์œผ๋กœ Integer ํƒ€์ž…์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋จ (์ตœ์†Œ ํž™)
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        pq.offer(10);
        pq.offer(20);
        pq.offer(15);

        // ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์š”์†Œ(10)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ œ๊ฑฐ
        System.out.println("Poll: " + pq.poll()); // ์ถœ๋ ฅ: 10

        // ํ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ(15)๋ฅผ ์กฐํšŒํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€ ์•Š์Œ
        System.out.println("Peek: " + pq.peek()); // ์ถœ๋ ฅ: 15

        // ํ์˜ ๋‚จ์€ ์š”์†Œ๋“ค
        System.out.println("Remaining elements: " + pq); // ์ถœ๋ ฅ: [15, 20]
    }
}

 

6. Comparator๋ฅผ ์‚ฌ์šฉํ•œ PriorityQueue

PriorityQueue๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž์—ฐ ์ˆœ์„œ(์˜ค๋ฆ„์ฐจ์ˆœ)๋กœ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•˜์ง€๋งŒ, Comparator๋ฅผ ์‚ฌ์šฉํ•ด ์‚ฌ์šฉ์ž ์ •์˜ ์ •๋ ฌ ๊ธฐ์ค€์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” PriorityQueue๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

import java.util.PriorityQueue;
import java.util.Comparator;

public class CustomPriorityQueueExample {
    public static void main(String[] args) {
        // ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ์œ„ํ•œ Comparator๋ฅผ ์‚ฌ์šฉํ•œ PriorityQueue
        PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());

        pq.offer(10);
        pq.offer(20);
        pq.offer(15);

        // ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์š”์†Œ(20)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ œ๊ฑฐ
        System.out.println("Poll: " + pq.poll()); // ์ถœ๋ ฅ: 20

        // ํ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ(15)๋ฅผ ์กฐํšŒํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€ ์•Š์Œ
        System.out.println("Peek: " + pq.peek()); // ์ถœ๋ ฅ: 15

        // ํ์˜ ๋‚จ์€ ์š”์†Œ๋“ค
        System.out.println("Remaining elements: " + pq); // ์ถœ๋ ฅ: [15, 10]
    }
}

 

7. PriorityQueue์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„

์‚ฝ์ž… offer() : O(log n)

์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ํž™์— ์‚ฝ์ž…๋  ๋•Œ, ํž™์˜ ํŠน์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ํž™ ์—… (Heap Up) ์ž‘์—…์ด ์ˆ˜ํ–‰๋œ๋‹ค.

์‚ญ์ œ poll() : O(log n)

๋ฃจํŠธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ ํ›„, ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ ๋ฃจํŠธ๋กœ ์ด๋™ํ•˜๊ณ  ํž™ ๋‹ค์šด (Heap Down) ์ž‘์—…์ด ์ˆ˜ํ–‰๋œ๋‹ค.

์กฐํšŒ peek() : O(1)

์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ž‘์—…์€ ํž™์—์„œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ์ด๋ฃจ์–ด์ง„๋‹ค.

 

8. PriorityQueue์˜ ๋‹จ์ 

์ •๋ ฌ๋œ ์ˆœ์„œ๋กœ์˜ ๋ฐ˜๋ณต์ž ์ œ๊ณต ์•ˆ ํ•จ

PriorityQueue๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ์ง€๋งŒ, ํ์˜ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ๋Š” ์ •๋ ฌ๋œ ์ˆœ์„œ๋กœ ์ œ๊ณต๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ํ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์š”์†Œ๋ฅผ ์–ป์œผ๋ ค๋ฉด, poll() ๋ฉ”์†Œ๋“œ๋ฅผ ๋ฐ˜๋ณต ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

null ํ—ˆ์šฉ ์•ˆ ํ•จ

PriorityQueue๋Š” null ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. null์„ ์‚ฝ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋ฉด NullPointerException์ด ๋ฐœ์ƒํ•œ๋‹ค.