완전 이진 트리 형태최대, 최소 값을 빠르게 찾아내는데 유용한 자료구조
![]()
O(log n))에 이루어짐O(N)이 소요됨정렬을 위한 연산이 있어 오버헤드가 발생할 수 있음| 연산 | 시간복잡도 |
|---|---|
| 삽입 | O(log N) |
| 삭제 | O(log N) |
Heap의 높이(height)log2 N을 넘지 않기 때문왼쪽 자식 노드 인덱스 = 부모 노드 인덱스 × 2 오른쪽 자식 노드 인덱스 = 부모 노드 인덱스 × 2 + 1부모 노드 인덱스 = 자식 노드 인덱스 / 2우선순위가 높은 데이터를 빠르게 알 수 있다.최대 힙과 최소 힙을 구현 가능import java.util.PriorityQueue;
// 낮은 숫자가 우선 순위인 int 형 우선순위 큐 선언 (Default)
PriorityQueue<Integer> pqLowest = new PriorityQueue<>();
// 높은 숫자가 우선 순위인 int 형 우선순위 큐 선언
PriorityQueue<Integer> pqHighest = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(1); // pq에 1 추가
pq.offer(2); // pq에 2 추가
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.poll(); // 헤드 값 반환 및 제거, pq가 비어있을 경우 Null 반환
pq.remove(); // 헤드 제거, pq가 비어있을 경우 예외 발생
pq.clear(); // pq에 저장되어 있는 모든 값 삭제
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.peek(); // 헤드 값 반환, pq가 비어있을 경우 Null 반환
pq.element(); // 헤드 값 반환, pq가 비어있을 경우 예외 발생
https://hoehen-flug.tistory.com/32
https://innovation123.tistory.com/111
https://velog.velcdn.com/images/suk13574/post/45ecd7ec-1a95-45c8-8aab-10e6fd3abc54/image.jpg
https://st-lab.tistory.com/205