Data Structures and Algorithms Lecture Notes

30 March 2011 • Heaps and Priority Queues


Outline

Array-Based Trees

Storing Trees In Arrays

Binary Tree Indexing

Tree-Array Space Requirements

Heaps

Heap Characteristics

Heap Data Type

Other Heap Operations

Heap Data-Type Implementation

Heap Add

i = count++
d[i] = e
while i ≠ 0
  p = (i - 1)/2
  if d[p] ≥ d[i], break
  swap(d[p], d[i])
  i = p

  1. Bubble up the new element until it has a larger parent.
    • Or it becomes the root.

Example Heap Add

Heap Remove

Example Heap Remove

Heap Remove

Priority Queues

Heaps as Priority Queues

Summary


This page last modified on 2010 November 16.

Creative
    Commons License