0% found this document useful (0 votes)
42 views4 pages

LectureSeven2020 Heap PDF

1. The document describes the heap data structure which can be represented as a nearly complete binary tree stored in an array. Each node of the tree corresponds to an element in the array. 2. It explains max-heaps and min-heaps, where in a max-heap the value of each node is less than or equal to its parent's value. The MaxHeapify procedure restores the max-heap property if a node's value is smaller than its children's values. 3. The BuildMaxHeap procedure converts an array into a max-heap by calling MaxHeapify on each node in bottom-up order. Heapsort uses BuildMaxHeap to build a max-heap and then

Uploaded by

Mehul Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

LectureSeven2020 Heap PDF

1. The document describes the heap data structure which can be represented as a nearly complete binary tree stored in an array. Each node of the tree corresponds to an element in the array. 2. It explains max-heaps and min-heaps, where in a max-heap the value of each node is less than or equal to its parent's value. The MaxHeapify procedure restores the max-heap property if a node's value is smaller than its children's values. 3. The BuildMaxHeap procedure converts an array into a max-heap by calling MaxHeapify on each node in bottom-up order. Heapsort uses BuildMaxHeap to build a max-heap and then

Uploaded by

Mehul Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Winter 2020 Lecture Seven ITEC2620

1
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10 1 2 3 4 5 6 7 8 9 10
2 4 1 16 14 10 8 7 9 3 2 4 1
(a) (b)

Figure 1: A max-heap viewed as (a) a complete binary tree and (b) an array. The number
within the circle at each node in the tree is the value stored at that node. The number above
a node is the corresponding index in the array.

1 Heaps
The (binary) heap data structure is an array object that can be viewed as a nearly complete
binary tree, as shown in Figure 1. Each node of the tree corresponds to an element of the
array that stores the value in the node. The tree is completely 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 attributes:

• length[A], which is the number of elements in the array, and

• heapSize[A], the number of elements in the heap stored within array A.

That is, although A[1 . . . length[A]] may contain valid numbers, no element past A[heapSize[A]],
where heapSize[A] ≤ length[A], is an element of the heap. The root of the tree is A[1], and
given the index i of a node, the indices of its parent P AREN T (i), left child LEF T (i),
and right child RIGHT (i) can be computed simply: bi/2c (floor of i/2), 2i, 2i + 1, respec-
tively. There are two kinds of binary heaps: max-heaps and min-heaps. In a max-heap, the
max-heap property is that for every node i other than the root,

A[P AREN T (i)] ≥ A[i],

that is, the value of a node is at most the value of its parent. Thus, the largest element in a
max-heap is stored at the root, and the subtree rooted at a node contains values no larger
than that contained at the node itself.
MaxHeapify is an important subroutine for manipulating max-heaps. Its inputs are an
array A and an index i into the array. When MaxHeapify is called, it is assumed that the
binary trees rooted at LEFT(i) and RIGHT(i) are max-heaps, but that A[i] may be smaller
than its children, thus violating the max-heap property. The function of MaxHeapify is to
let the value at A[i] ”float down” in the max-heap so that the subtree rooted at index i
becomes a max-heap. It can be shown that the running time of MaxHeapify on a node of
height h is O(h).

1
Winter 2020 Lecture Seven ITEC2620

MaxHeapify(A, i)
1: l <- LEFT(i)
2: r <- RIGHT(i)
3: if l <= heapSize[A] and A[l] > A[i]
4: then largest <- l
5: else largest <- i
6: if r <= heapSize[A] and A[r] > A[largest]
7: then largest <- r
8: if largest != i
9: then exchange A[i] <-> A[largest]
10: MaxHeapify(A, largest)

Figure 2: The action of MaxHeapify(A, 2), where heapSize[A] = 10. (a) The initial config-
uration, with A[2] at node i = 2 violating the max-heap property since it is not larger than
both children. The max-heap property is restored for node 2 in (b) by exchanging A[2] with
A[4], which destroys the max-heap property for node 4. The recursive call MaxHeapify(A,
4) now has i = 4. After swapping A[4] with A[9], as shown in (c), node 4 is fixed up, and
the recursive call MaxHeapify(A, 9) yields no further change to the data structure.

The procedure BuildMaxHeap We can use the procedure MaxHeapify in a bottom-up


manner to convert an array A[1 . . . n], where n = length[A], into a max-heap. The elements
in the subarray A[(bn/2c + 1) . . . n] are all leaves of the tree, and so each is a 1-element
heap to begin with. The procedure BuildMaxHeap goes through the remaining nodes of
the tree and runs BuildMaxHeap on each one. It can be shown that the running time of
BuildMaxHeap on a node of height h is O(nh), or O(n log n). Actually a tighter upper bound
can be found, that is O(n).

2
Winter 2020 Lecture Seven ITEC2620

BuildMaxHeap(A)
1: heapSize[A] <- length[A]
2: for i <- floorOf(length[A]/2) downto 1
3: do MaxHeapify(A, i)

Figure 3: The operation of BuildMaxHeap, showing the data structure before the call to
MaxHeapify in line 3 of BuildMaxHeap. (a) A 10-element input array A and the binary
tree it represents. The figure shows that the loop index i refers to node 5 before the call
MaxHeapify(A, i). (b) The data structure that results. The loop index i for the next
iteration refers to node 4. (c)-(e) Subsequent iterations of the for loop in BuildMaxHeap.
Observe that whenever MaxHeapify is called on a node, the two subtrees of that node are
both max-heaps. (f) The max-heap after BuildMaxHeap finishes.

The Heapsort algorithm starts by using BuildMaxHeap to build a max-heap on the input
array A[1 . . . n], where n = length[A]. Since the maximum element of the array is stored
at the root A[1], it can be put into its correct final position by exchanging it with A[n]. If
we now ”discard” node n from the heap (by decrementing heapSize[A]), we observe that

3
Winter 2020 Lecture Seven ITEC2620

A[1 . . . (n − 1)] can easily be made into a max-heap. The children of the root remain max-
heaps, but the new root element may violate the max-heap property. All that is needed
to restore the max-heap property, however, is one call to MaxHeapify(A, 1), which leaves a
max-heap in A[1 . . . (n − 1)]. The Heapsort algorithm then repeats this process for the max-
heap of size n − 1 down to a heap of size 2. The Heapsort procedure takes time O(n log n),
since the call to BuildMaxHeap takes time O(n) and each of the n − 1 calls to MaxHeapyify
takes time O(log n).
Heapsort(A)
1: BuildMaxHeap(A)
2: for i <- length[A] downto 2
3: do exchange A[1] <-> A[i]
4: heapSize[A] <- heapSize[A] - 1
5: MaxHeapify(A, 1)

Figure 4: The operation of Heapsort. (a) The max-heap data structure just after it has been
built by BuildMaxheap. (b)-(j) The max-heap just after each call of MaxHeapify in line 5.
The value of i at that time is shown. Only lightly shaded nodes remain in the heap. (k) The
resulting sorted array A.

You might also like