Data Structures/Min-Heap
heappriority-queuecomplete-tree
Press play to start
1void insert(int heap[], int *size, int val) {
2 heap[(*size)++] = val;
3 int i = *size - 1;
4 while (i > 0 && heap[i] < heap[(i - 1) / 2]) {
5 int t = heap[i]; heap[i] = heap[(i-1)/2]; heap[(i-1)/2] = t;
6 i = (i - 1) / 2;
7 }
8}
Step 1/0

Practice

LeetCode·#215 Kth Largest ElementMediumHackerRank·Jesse and CookiesEasy
OperationBestAverageWorst
insertO(log n)O(log n)O(log n)
extract minO(log n)O(log n)O(log n)
get minO(1)O(1)O(1)
heapifyO(n)O(n)O(n)
SpaceO(n)