However, the remainder of the array is kept partially unsorted. Thus, instant access is only possible to the largest (smallest) item. Insertions are fast, so it's a good way to deal with incoming events or data and always have access to the earliest/biggest.
对于优先级队列、调度程序(需要最早的项目)等非常有用。
A heap is a tree where a parent node's value is larger than that of any of its descendant nodes.
如果你把堆想象成一个二叉树,按深度的线性顺序存储,首先是根节点(然后是那个节点的子节点,然后是那些节点的子节点) ; 然后索引 N 处的节点的子节点是2N + 1和2N + 2。此属性允许通过索引快速访问。由于堆是由交换节点操作的,因此可以进行就地排序。
The characteristic of a heap is that it is a structure that maintains data semiordered; thus, it is a good tradeoff between the cost of maintaining a complete order and the cost of searching through random chaos. That characteristic is used on many algorithms, such as selection, ordering, or classification.
Another useful characteristic of a heap is that it can be created in-place from an array!