이진 트리를 이용한 자료구조로 우선순위가 높은 요소가 먼저 나가기 위해
요소가 삽입, 삭제 될때 즉시 정렬되는 특징이 있습니다.
여러 개의 값들 중 최대값과 최소값을 빠르게 찾아낼 수 있습니다.
주의!
우선순위 큐를 힙이라고 부르기도 하지만 정확히는 아닙니다.
배열을 우선순위에 따라 정렬한다면 이 또한 우선순위 큐입니다.
최대 힙(Max Heap)
과 그 반대인 최소 힙(Min Heap)
이 있다.위와같은 특징으로 인해 힘은 요소를 추가
,삭제
하는 로직이 핵심이라고 볼 수 있습니다.
O(logN)
의 시간복잡도를 가진다. push(value) {
this.heap.push(value);
let currIndex = this.heap.length - 1;
let parrentIndex = Math.floor(currIndex / 2);
while (parrentIndex !== 0 && this.heap[parrentIndex] < value) {
const temp = this.heap[parrentIndex];
this.heap[parrentIndex] = value;
this.heap[currIndex] = temp;
currIndex = parrentIndex;
parrentIndex = Math.floor(currIndex / 2);
}
}
O(logN)
의 시간복잡도를 가진다. pop() {
const returnValue = this.heap[1];
this.heap[1] = this.heap.pop();
let currIndex = 1;
let leftIndex = 2;
let rightIndex = 3;
while (
this.heap[currIndex] < this.heap[leftIndex] ||
this.heap[currIndex] < this.heap[rightIndex]
) {
if (this.heap[leftIndex] < this.heap[rightIndex]) {
const temp = this.heap[currIndex];
this.heap[currIndex] = this.heap[rightIndex];
this.heap[rightIndex] = temp;
currIndex = rightIndex;
} else {
const temp = this.heap[currIndex];
this.heap[currIndex] = this.heap[leftIndex];
this.heap[leftIndex] = temp;
currIndex = leftIndex;
}
leftIndex = currIndex * 2;
rightIndex = currIndex * 2 + 1;
}
return returnValue;
}
class MaxHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currIndex = this.heap.length - 1;
let parrentIndex = Math.floor(currIndex / 2);
while (parrentIndex !== 0 && this.heap[parrentIndex] < value) {
const temp = this.heap[parrentIndex];
this.heap[parrentIndex] = value;
this.heap[currIndex] = temp;
currIndex = parrentIndex;
parrentIndex = Math.floor(currIndex / 2);
}
}
pop() {
const returnValue = this.heap[1];
this.heap[1] = this.heap.pop();
let currIndex = 1;
let leftIndex = 2;
let rightIndex = 3;
while (
this.heap[currIndex] < this.heap[leftIndex] ||
this.heap[currIndex] < this.heap[rightIndex]
) {
if (this.heap[leftIndex] < this.heap[rightIndex]) {
const temp = this.heap[currIndex];
this.heap[currIndex] = this.heap[rightIndex];
this.heap[rightIndex] = temp;
currIndex = rightIndex;
} else {
const temp = this.heap[currIndex];
this.heap[currIndex] = this.heap[leftIndex];
this.heap[leftIndex] = temp;
currIndex = leftIndex;
}
leftIndex = currIndex * 2;
rightIndex = currIndex * 2 + 1;
}
return returnValue;
}
}
const heap = new MaxHeap();
heap.push(45);
heap.push(36);
heap.push(54);
heap.push(27);
heap.push(63);
console.log(heap.heap); // [ null, 63, 54, 45, 27, 36 ]
const array = [];
array.push(heap.pop());
array.push(heap.pop());
array.push(heap.pop());
array.push(heap.pop());
array.push(heap.pop());
console.log(array); // [ 63, 54, 45, 36, 27 ] 힙 정렬