Lecture 017

Priority Queue Interface

Priority Queue Interface

Heap

Complexity

Complexity
A heap is a balanced binary tree

Shape of Heap:

Heap the word:

Sifting Up (adding elements)

  1. added element in one place that maintain the shape invariants
  2. swap with its parent until violation is fixed
  3. There is always at most one violation

Swapping(Sifting) Down (removing elements O(log n))

  1. remove element on the root
  2. swap right most leaf to maintain shape
  3. swap root with its child with highest priority
  4. This guarantees there are always at most two violations

Implementation

Arithmetic to Navigate the Tree

Enumerating Trees

Enumerating Trees

Using Arrays

Using Arrays

Header

Header

In practice:

Bounded Implementation

Bounded Implementation

The Only Change

The Only Change

Table of Content