heapsort

Introduction to Algorithms Third Edition

The (binary) heap data structure is an array object that we can view as a
nearly complete binary tree (see Section B.5.3), as shown in Figure 6.1. Each
node of the tree corresponds to an element of the array. The tree is com-
pletely filled on all levels except possibly the lowest, which is filled from the
left up to a point. An array A that represents a heap is an object with two at-
tributes: A: length, which (as usual) gives the number of elements in the array, and
A: heap-size, which represents how many elements in the heap are stored within
array A. That is, although AŒ1 : : A: length may contain numbers, only the ele-
ments in AŒ1 : : A: heap-size, where 0 Ä A: heap-size Ä A: length, are valid ele-
ments of the heap. The root of the tree is AŒ1, and given the index i of a node, we
can easily compute the indices of its parent, left child, and right child:

indiex indices 下标

For the heapsort algorithm, we use max-heaps. Min-heaps commonly imple-
ment priority queues, which we discuss in Section 6.5.

最小堆通常用于构造优先队列。

the largest element in a max-heap is stored at the root, and the subtree rooted at a node contains6.1 Heaps 153
values no larger than that contained at the node itself. A min-heap is organized in the opposite way.

a max-heap
A[PARENT(i)]>=A[i] a min-heap
A[PARENT(i)]<=A[i]

(上根下枝)

上一篇:Django发HTML邮件


下一篇:1226: [SDOI2009]学校食堂Dining - BZOJ