堆的定义:
若存在 n n n 个元素的序列 { k 1 , k 2 , … , k n } \{ k_1,k_2,\dots,k_n\} {k1,k2,…,kn},其中
{
k
i
⩽
k
2
i
k
i
⩽
k
2
i
+
1
{ki⩽k2iki⩽k2i+1
⎩
⎨
⎧ki⩽k2iki⩽k2i+1
1
◯
\text{\textcircled 1}
1◯ 或
{
k
i
⩾
k
2
i
k
i
⩾
k
2
i
+
1
{ki⩾k2iki⩾k2i+1
⎩
⎨
⎧ki⩾k2iki⩾k2i+1
2
◯
\text{\textcircled 2}
2◯ ,
i
=
1
,
2
,
…
,
⌊
n
2
⌋
i=1,2,\dots, \lfloor \dfrac{n}{2} \rfloor
i=1,2,…,⌊2n⌋
则称为堆。
小根堆: 满足条件 1 ◯ \text{\textcircled 1} 1◯的堆称为小根堆。
大根堆: 满足条件 2 ◯ \text{\textcircled 2} 2◯的堆称为大根堆。