Mastering Algorithms With C - Kyle Loudon [103]
This chapter covers:
Heaps
Trees organized so that we can determine the node with the largest value quickly. The cost to preserve this property is less than that of keeping the data sorted. We can also organize a heap so that we can determine the smallest value just as easily.
Priority queues
Data structures naturally derived from heaps. In a priority queue, data is organized in a heap so that we can determine the node with the next highest priority quickly. The "priority" of an element can mean different things in different problems.
Some applications of heaps and priority queues are:
Sorting
Specifically, an algorithm called heapsort. In heapsort, the data to be sorted begins in a heap. Nodes are extracted from the heap one at a time and placed at the end of a sorted set. As each node is extracted, the next node for the sorted set percolates to the top of the heap. Heapsort has the same runtime complexity as quicksort (see Chapter 12 ), but a good implementation of quicksort usually beats it by a small constant factor in practice.
Task scheduling
For example, that performed by operating systems to determine which process is next to run on a CPU. Operating systems continually change the priorities of processes. A priority queue is an efficient way to ensure that the highest-priority process is next to get the CPU.
Parcel sorting (illustrated in this chapter)
A process used by delivery companies to prioritize the routing of parcels. As parcels are scanned, high priorities are assigned to those requiring urgent delivery. Parcels that are less urgent are assigned lower priorities. A computer system might use a priority queue as an efficient means of ensuring that the highest priority parcels move through the system the fastest.
Huffman coding
A method of data compression that uses a Huffman tree to assign codes to symbols in the data (see Chapter 14). Frequently occurring symbols are assigned short codes, whereas symbols occuring less frequently are assigned longer ones. The Huffman tree is built by merging smaller binary trees two by two. The two trees merged at each step are extracted from a priority queue because we merge the two with the smallest key values.
Load balancing
Often usage statistics are maintained about a number of servers handling similar tasks. As connection requests arrive, a priority queue can be used to determine which server is best able to accommodate a new request.
Description of Heaps
A heap is a tree, usually a binary tree, in which each child node has a smaller value than its parent. Thus, the root node is the largest node in the tree. We may also choose to orient a heap so that each child node has a larger value than its parent. In this case, the root node is the smallest node. Trees like these are partially ordered because, although the nodes along every branch have a specific order to them, the nodes at one level are not necessarily ordered with respect to the nodes at another. A heap in which each child is smaller than its parent is top-heavy . This is because the largest node is on top (see Figure 10.1). A heap in which each child is larger than its parent is bottom-heavy .
Heaps are left-balanced (see Chapter 9), so as nodes are added, the tree grows level by level from left to right. A particularly good way to represent left-balanced binary trees, and therefore heaps, is to store nodes contiguously in an array in the order we would encounter them in a level traversal (see Chapter