Algorithms and OOD (CSC 207 2014S) : Outlines

# Outline 45: Heaps

Held: Monday, 21 April 2014

Summary

We explore heaps, a useful and relatively efficient implementation of priority queues.

Related Pages

Overview

• Priority queues, revisited.
• Recent implementation techniques.
• Heaps.
• Removing elements from heaps.
• Asymptotic analysis.
• Storing heaps in arrays.
• Heap sort.

### Upcoming Work

• Today's writeup: No writeup.
• Part 2 of the project is due Wednesday (tentatively).

• Welcome to our prospective
• I've reviewed about half of your projects before running out of time.
We'll talk about some common (and not so common) issues for about ten minutes.
• Given the problems with MathLAN, I'm not sure what to do about the due date of the project. What do you think?
• Those of you trying to fix the problem using Wireshark or whatever probably need to stop.
• I need two graders for next semester who can do detailed comments. Yes, I will provide training.

### Extra Credit

• Math extra Thursday: Sphere Packing.
• CS Table Friday: Heartbleed.
• Iowater project April 26 - Tag drains. Mail iowater@grinnell.edu for details.

## Priority Queues, Reviewed

• What is a priority queue?
• What are the primary operations?
• What are the implementations we have? What are the running times of the key operations?

## Implementation Techniques

• What are implementation techniques we've learned since we last looked at priority queues?
• I know of two: We've started looking at trees and we've learned some clever ways of using arrays (in hash tables).
• Will either help?
• Binary search trees are cool, but would be cooler if we could keep them balanced.

## Heaps

• Simplified binary search trees.
• Binary trees with the heap property
• Each node is at least as big as the roots of its subtrees.
• Each subtree also has the same property.
• Binary trees that are nearly complete
• It's complete in the sense that most nodes have two children
• At the last level all the nodes are at the left.

• Two invariants to maintain: The shape of the tree and the heap property.
• Which is harder? Probably getting the shape right.
• So we add an element at the end of the last level.
• And then we restore the heap property by repeatedly swapping up.

## Removing Elements

• The largest (highest priority) element is at the top.
• Once we remove it, what do we do?
• Once again, two invariants to maintain: The shape of the tree and the heap property.
• So, we take the last element and put it at the root.
• And then we swap it down to the right place?
• Repeatedly swap with the larger child (provided it's smaller than the larger child).

## Using Arrays

• Here's the really clever part: We can store heaps in arrays.
• Can you figure out the index of children and parents?

## Heap Sort

• Sorting using heaps. Turn the array into a heap. Then repeatedly grab the top element and put it at the end.

Copyright (c) 2013-14 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit `http://creativecommons.org/licenses/by/3.0/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.