1 of 30

2 of 30

3 of 30

4 of 30

A priority queue.

5 of 30

The most common implementation of a priority queue is the binary heap.

6 of 30

Binary Heap

Week 7

7 of 30

A Binary Heap is a complete binary tree which satisfies the heap two fundamental properties:

8 of 30

A Binary Heap is a complete binary tree which satisfies the heap two fundamental properties:

  • Completeness: All of the tree's levels are filled in, with one possible exception of the highest level, which is supposed to be completed from left to right.

9 of 30

A Binary Heap is a complete binary tree which satisfies the heap two fundamental properties:

  • Completeness: All of the tree's levels are filled in, with one possible exception of the highest level, which is supposed to be completed from left to right.

  • Heap Property: Ensures that the node values are in order. The Min-Heap property (The smallest element is always at the root)

which avoids a child node from having a value smaller than its parent node.

10 of 30

11 of 30

12 of 30

Binary Heaps

  1. Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

Insert (step 1)

13 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

Insert (step 2)

14 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

Insert (step 3)

15 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

What happens when we want to send the most important message?

16 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

What happens when we want to send the most important message?

ExtractMin (step 1)

17 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

What happens when we want to send the most important message?

ExtractMin (step 2)

18 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

What happens when we want to send the most important message?

ExtractMin (step 3)

19 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

What happens when we want to send the most important message?

ExtractMin (step 4)

20 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.

21 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.

22 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.

DecreaseKey (step 1)

23 of 30

Binary Heaps

  • Every level is full, last one from left to right

  • Heap Property: No child is smaller than its parent

Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.

DecreaseKey (step 2)

24 of 30

O(1)

O(log n)

O(log n)

O(log n)

25 of 30

For any given element at position i, its:

  • Left Child will be at position 2*i + 1
  • Right Child will be at position 2*i + 2
  • Parent Node will be at position (i-1)/2 (using integer division)

26 of 30

Initialization and Structure

27 of 30

Inserting a Key

O(log n)

28 of 30

Getting the Minimum

O(1)

29 of 30

Extracting the Minimum

O(log n)

30 of 30

DecreaseKey

O(log n)