Algorithm/PROGRAMMERS

[힙(Heap)] 우선순위 큐(PriorityQueue)

imsseong 2020. 11. 10. 10:23

1. 선언

1
2
3
4
5
6
7
import java.util.PriorityQueue; //import
 
//int형 priorityQueue 선언 (최소힙)
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
 
//int형 priorityQueue 선언 (최대힙)
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());
cs

2. 값 추가

1
priorityQueue.offer(3);
cs

3. 값 삭제

1
priorityQueue.poll(); // priorityQueue에 첫번째 값을 반환하고 제거 비어있다면 null
cs

4. 값 출력

1
priorityQueue.peek(); //Priority Queue에서 우선순위가 가장 높은 값 출력
cs