# Binary Heaps

A binary heap data structure is a binary tree that is completely filled on all levels, except possibly the lowest, which will be filled from the left up to a point. Due to these characteristics, it is easy to represent the tree in an array. Figure 1 shows the logical structure (top) of the heap and also how it can be stored in an array (bottom). Notice how each layer of the tree occupies consecutive slots in the array. The root of the tree is $A\left[1\right]$, and given the index $i$ of a node, the indices of its parent PARENT $\left(i\right)$, left child LEFT $\left(i\right)$ and right child RIGHT $\left(i\right)$ can be computed simply.
PARENT $\left(i\right)$
1    return  $⌊i/2⌋$
LEFT $\left(i\right)$
1    return  $2i$
RIGHT $\left(i\right)$
1    return  $2i+1$
Binary heaps can either be a maximum heap or minimum heap. As a maximum heap, every node indexed by $i$, other than the root (i.e. $i\ne 1$), has $A\left[\text{PARENT}\left(i\right)\right]\ge A\left[i\right]$. Conversely in a minimum heap, every node indexed by $i$, other than the root, has $A\left[\text{PARENT}\left(i\right)\right]\le A\left[i\right]$. The remainder of this lecture assumes a maximum heap.
Note: This property makes a binary heap different from a binary search tree. In a binary tree, $\text{left child}\le \text{parent}<\text{right child}$. However, in a maximum heap, $\text{parent}\ge \text{left child}$ and $\text{parent}\ge \text{right child}$. So a binary search tree can be viewed as sorted, while a heap cannot.
Figure 1: The structure of a maximum heap. The top diagram shows the logical heap, the bottom diagram shows how it can be stored in an array, still showing the heap structure. Numbers in the circles/boxes are keys and those outside are indices. Notice how each layer of the tree occupies consecutive slots in the array.

## 1  Maintaining the heap property

Assuming that we already have a maximum heap, operations on a maximum heap, for example inserting or deleting, may cause the heap to lose its maximum heap property. The MAX-HEAPIFY routine can be used to rectify this. It takes an array $A$ and index $i$, and assumes that $A$ is a maximum heap except that $A\left[i\right]$ may be less than its children.
MAX-HEAPIFY $\left(A,i\right)$
1     $l$ $←$  LEFT $\left(i\right)$
2     $r$ $←$  RIGHT $\left(i\right)$
3    if  $l\le \text{heap-size}\left[A\right]$ and $A\left[l\right]>A\left[i\right]$
4          then  $\text{largest}$ $←$  $l$
5          else   $\text{largest}$ $←$  $i$
6    if  $r\le \text{heap-size}\left[A\right]$ and $A\left[r\right]>A\left[\text{largest}\right]$
7          then  $\text{largest}$ $←$  $r$
8    if  $\text{largest}\ne i$
9          then Swap $A\left[i\right]$ and $A\left[\text{largest}\right]$
10                       MAX-HEAPIFY $\left(A,\text{largest}\right)$
Figure 2: In (a), 5 has been added to position 2 in a maximum heap. The first iteration of MAX-HEAPIFY swaps 5 with 14, in (b), and then swaps 5 with 12 in (c).

## 2  Building a heap

The BUILD-MAX-HEAP routine takes any array $A$, and, working successively from the bottom of tree to the top, uses MAX-HEAPIFY to build the maximum heap.
BUILD-MAX-HEAP $\left(A\right)$
1     $\text{heap-size}\left[A\right]$ $←$  $\text{length}\left[A\right]$
2    for  $i$ $←$  $⌊\text{length}\left[A\right]/2⌋$ downto  $1$
3             do  MAX-HEAPIFY $\left(A,i\right)$
Figure 3: (a) The initial tree. BUILD-MAX-HEAP works successively from the bottom. 18 is greater than its children, so nothing is done. (b) MAX-HEAPIFY is called on 3, and so is swapped with 12. (c) 1 will be swapped with 8. (d) 2 will be sent to the bottom of the tree, first swapped with 18, then with 4. (e) When MAX-HEAPIFY is called on 5, it is sent to the bottom of tree. (f) The resulting maximum heap.

## 3  Priority Queues

A priority queue is a data structure for maintaining a set $S$ of elements, each with an associated value called a key.
Priority queues are often used to efficiently extract an element with a particular property out of a dynamic set. They do this by maintaining the position of element in the set that holds that property, and keeping the rest of the set ordered in such a way that modifying the set will lead to easily finding the new element with the given property.
A max-priority queue supports the following operations.
• INSERT ( $S,x$) inserts the element $x$ into the set $S$., i.e., $S←S\cup \left\{x\right\}$.
• MAXIMUM( $S$) returns the element of $S$ with the largest key.
• EXTRACT-MAX( $S$) removes and returns the element of $S$ with the largest key.
• INCREASE-KEY( $S,x,k$) increases the value of element $x$'s key to the new value $k$, which is assumed to be at least as large as $x\text{'}$s current key value.
A maximum heap can be used as a max-priority queue, as it always maintains that the first element is the maxmimum. Using MAX-HEAPIFY, it can easily ensure that certain changes, for example removing the maximum element, will maintain the first element as the maximum.
HEAP-MAXIMUM $\left(A\right)$
1    return  $A\left[1\right]$
HEAP-EXTRACT-MAX $\left(A\right)$
1    if  $\text{heap-size}\left[A\right]<1$
2          then error "Heap underflow"
3     $\text{max}$ $←$  $A\left[1\right]$
4     $A\left[1\right]$ $←$  $A\left[\text{heap-size}\left[A\right]\right]$
5     $\text{heap-size}\left[A\right]$ $←$  $\text{heap-size}\left[A\right]-1$
6     MAX-HEAPIFY $\left(A,1\right)$
7    return  $\text{max}$
HEAP-SIFT-UP $\left(A,i\right)$
1    while  $i>1$ and $A\left[\text{PARENT}$(i) $\right]
2                  do Swap $A\left[i\right]$ with $A\left[\text{PARENT}$(i) $\right]$
3                         $i$ $←$  PARENT $\left(i\right)$
HEAP-INCREASE-KEY $\left(A,i,k\right)$
1    if  $k
2          then error "New key is smaller than current key"
3     $A\left[i\right]$ $←$  $k$
4     HEAP-SIFT-UP $\left(A,i\right)$
MAX-HEAP-INSERT $\left(A,k\right)$
1     $\text{heap-size}\left[A\right]$ $←$  $\text{heap-size}\left[A\right]+1$
2     $A\left[\text{heap-size}\left[A\right]\right]$ $←$  $k$
3     HEAP-SIFT-UP $\left(A,\text{heap-size}\left[A\right]\right)$
Figure 4: HEAP-INCREASE-KEY (a) The original maximum heap with the key to be increased shaded in yellow. (b) The key is increased to 17. (c) 17 is swapped with its parent. (d) The maximum heap property is restored, swapping 17 with its parent again.

File translated from TEX by TTM, version 3.67.
On 31 Mar 2006, 18:12.