Heap
A heap is a complete tree, usually stored in an array, where every parent obeys a priority relation with its children. In a min-heap the smallest value stays at the root; in a max-heap the largest does.
이 멘탈 모델부터 시작하세요
A heap is a complete tree, usually stored in an array, where every parent obeys a priority relation with its children. In a min-heap the smallest value stays at the root; in a max-heap the largest does.
불필요한 전문용어 없이 동작 방식 이해하기
A heap is not fully sorted. It only guarantees the parent/child heap property.
Insert and remove-root repair the heap by bubbling a value up or down.
알고리즘을 연속된 의사결정으로 읽기
Append new value.
Bubble it upward while it violates the heap property.
For root removal, move the last value to root and bubble it down.
A min-heap used by Dijkstra always exposes the currently cheapest frontier node.
class MinHeap {
data = [];
push(value) {
this.data.push(value);
let i = this.data.length - 1;
while (i > 0) {
const p = Math.floor((i - 1) / 2);
if (this.data[p] <= this.data[i]) break;
[this.data[p], this.data[i]] = [this.data[i], this.data[p]];
i = p;
}
}
}문법을 외우지 마세요. 각 줄에서 바뀌는 상태를 따라가고 위에서 설명한 불변식과 연결하세요.
peek O(1), insert/remove O(log n)
Do not quote this complexity mechanically. Ask which operation or input dimension it describes, then derive the cost from the loops, recursion depth, data-structure operations and any preprocessing assumptions.
적합한 경우
- Choose it when its dominant operations match your workload.
- Compare memory overhead as well as Big-O.
보통 어디에서 문제가 생기는가
- Memorizing complexity without understanding representation.
- Ignoring worst-case shape or resizing/rebalancing costs.
전문적인 답변 구조
Do not say a heap is a sorted tree. Emphasize O(1) access to the best-priority item and O(log n) insertion/removal.
What problem does Heap solve?
A heap is a complete tree, usually stored in an array, where every parent obeys a priority relation with its children. In a min-heap the smallest value stays at the root; in a max-heap the largest does.
복잡도에 대해 무엇을 언급해야 하나요?
Its typical cost here is peek O(1), insert/remove O(log n), but a complete answer should connect that cost to the representation, input constraints and the dominant operations.
가장 흔한 실수는 무엇인가요?
Memorizing complexity without understanding representation.