Data Structures - Heap
A heap (Heap) is a special kind of complete binary tree that satisfies the heap property. A heap only guarantees the size relationship between parent and child nodes, and does not require a strict order between left and right subtrees.
Definition and storage of a heap
Heap (Heap) - max-heap structure and array storage
Tree view (max-heap)
15
Index 0
10
Index 1
8
Index 2
5
3
3
4
2
5
Array view (compact storage)
15
[0]
10
[1]
8
[2]
5
[3]
3
[4]
2
[5]
Parent:(i-1)/2 | Left child:2i+1 | Right child:2i+2
Complete binary tree -> naturally suited to arrays, no pointer overhead
Sift Up (insertion) O(log n)
New element placed at the end -> compare with parent -> if larger, swap -> repeat until the heap top
Sift Down (delete heap top) O(log n)
Move the last element to the top -> compare with the larger child -> if smaller, swap -> repeat until a leaf
The two types of heaps:
- Max Heap: every parent node >= its children, the root is the maximum value
- Min Heap: every parent node <= its children, the root is the minimum value
Because a heap is a complete binary tree, it is naturally suited to array storage:
| Node (index i) | Relational | Position (0-based) |
|---|---|---|
| Parent node | parent(i) | (i - 1) / 2 |
| Left child node | left(i) | 2 * i + 1 |
| Right child node | right(i) | 2 * i + 2 |
Heap insertion and deletion (heapify process)
Example
#include
#define MAX 100
/* max-heap structure */
struct
MaxHeap
{
int
arr
[
MAX
]
;
int
size
;
/* current number of elements */
}
;
void
initHeap
(
struct
MaxHeap
h
)
{
h
->
size
=
0
;
}
void
swap
(
int
a
,
int
b
)
{
int
t
=
a
;
a
=
b
;
b
=
t
;
}
/* Sift Up: a newly inserted element is adjusted from the bottom upward
compared with the parent; if greater than the parent, swap until the heap property is satisfied */
void
siftUp
(
struct
MaxHeap
h
,
int
idx
)
{
while
(
idx
0
)
{
int
parent
=
(
idx
1
)
/
2
;
if
(
h
->
arr
[
idx
]
<=
h
->
arr
[
parent
]
)
break
;
swap
(
&
h
->
arr
[
idx
]
,
&
h
->
arr
[
parent
]
)
;
idx
=
parent
;
}
}
/* insert: first place the element at the end, then sift up to adjust O(log n) */
void
insert
(
struct
MaxHeap
h
,
int
value
)
{
if
(
h
->
size
=
MAX
)
return
;
h
->
arr
[
h
->
size
]
=
value
;
siftUp
(
h
,
h
->
size
)
;
h
->
size
++;
}
/* Sift Down: adjust downward from the root
compared with the larger child; if smaller, swap until the heap property is satisfied */
void
siftDown
(
struct
MaxHeap
h
,
int
idx
)
{
while
(
1
)
{
int
largest
=
idx
;
int
left
=
2
idx
1
;
int
right
=
2
idx
2
;
if
(
left
<
h
->
size
&&
h
->
arr
[
left
]
h
->
arr
[
largest
]
)
largest
=
left;
if
(
right
<
h
->
size
&&
h
->
arr
[
right
]
h
->
arr
[
largest
]
)
largest
=
right;
if
(
largest
==
idx
)
break
;
swap
(
&
h
->
arr
[
idx
]
,
&
h
->
arr
[
largest
]
)
;
idx
=
largest
;
}
}
/* delete the heap top (maximum value): move the last element to the top, then sift down to adjust O(log n) */
int
extractMax
(
struct
MaxHeap
h
)
{
if
(
h
->
size
==
0
)
return
1
;
int
maxVal
=
h
->
arr
[
0
]
;
h
->
arr
[
0
]
=
h
->
arr
[
--
h
->
size
]
;
/* move the last element to the top */
siftDown
(
h
,
0
)
;
/* sift down adjustment */
return
maxVal
;
}
int
main
(
)
{
struct
MaxHeap h
;
initHeap
(
&
h
)
;
int
vals
[
]
=
{
3
,
10
,
5
,
8
,
2
,
15
}
;
for
(
int
i
=
0
;
i
<
6
;
i
++
)
insert
(
&
h
,
vals
[
i
]
)
;
printf
(
"Extract the maximum values in order: "
)
;
while
(
h.
size
0
)
{
printf
(
"%d "
,
extractMax
(
&
h
)
)
;
}
printf
(
"\n"
)
;
/* output: 15 10 8 5 3 2 (descending) */
return
0
;
}
Application scenarios
| Scenario | Description |
|---|---|
| Priority queue | A heap is the most common underlying implementation of a priority queue |
| Heap sort | O(n log n) in-place sorting algorithm (detailed in Chapter 18) |
| Top K problem | Use a min-heap to maintain the top K largest elements |