# Lecture 17: Heaps

## Announcement

Office hours canceled today

1. Heaps

## Last Time: BST and AVL Trees

2 Ingredients:

1. Binary search trees
• restriction on values
2. Balanced binary trees
• restriction on tree structure

AVL trees give efficient worst-case performance

• add, remove, find all in $O(\log n)$ time

Performance between BST vs AVL trees depends on usage

• often unbalanced BST is sufficient
• AVL exponentially faster sometimes (e.g., adding in sorted order)

## Binary Heaps

Another Tree Representation:

• Binary Heaps

Goal. Implement a priority queue with $O(\log n)$-time operations

Exercise. How could this goal be achieved with an AVL tree?

## Binary Heap Structure

1. Binary tree stores comparable elemements
• comparison by priority
2. Heap property (restriction on values)
• children always store larger values than parent
3. Complete binary tree (restriction on structure):
• all leaves have (almost) same depth
• very restrictive!

Goal. Use & maintain these properties for an efficient implementation of a priority queue:

• add(x, p)
• min()
• removeMin()

## Heap Property

$T$ a binary tree

• each node stores a comparable element, $v$

$T$ has the heap property if for every node storing value $v$ with children $u$ and $w$, we have $v \leq u$ and $v \leq w$.

## Question

If $T$ has the heap property, what can we say about the value of the root?

## Complete Binary Tree, Formally

$T$ is a complete binary tree of depth $D$ if:

1. every node at depth $d \leq D - 2$ has 2 children
2. If $v$ is at depth $D-1$ and $v$ has only one child, $v$’s child is a left child
3. if $v$ is at depth $D - 1$ and $v$ has a child, then every depth $D-1$ node to the left of $v$ has $2$ children
4. if $v$ is at depth $D-1$ and $D$ has fewer than $2$ children, then every depth $D-1$ node to the right of $v$ has no children

## Question 1

If $T$ is a complete binary tree, where can we add a node to maintain completeness?

## Question 2

If $T$ is a complete binary tree, what nodes can we remove and maintain completeness?

## Question 3

If $T$ is a complete binary tree with $n$ nodes, what is its depth?

## Binary Heaps

A binary tree $T$ storing comparable elements is a binary heap if:

1. $T$ satisfies the heap property, and
2. $T$ is a complete binary tree

## Heap Priority Queue: min

Given a binary heap $T$, how can we implement min()?

Given a binary heap $T$, how can we add an element?

## “Bubble Up” Procedure

1. Add element at unique location where a node can be added, w
2. Repeat
• if w < w.parent, swap w and w.parent
• else break

## Why Does Bubble Up Work?

1. Add element at unique location where a node can be added, w
2. Repeat
• if w < w.parent, swap w and w.parent
• else break

## What is Bubble Up Running Time?

1. Add element at unique location where a node can be added, w
2. Repeat
• if w < w.parent, swap w and w.parent
• else break

## Heap Priority Queue: removeMin

Given a binary heap $T$, how can we removeMin?

## “Trickle Down” Procedure

1. Copy value from unique removable leaf to root and remove leaf
2. Set w to root
3. Repeat:
• if w > some child, v =  smaller child
• swap v and w values
• else break

## Why does “Trickle Down” Work?

1. Set w to root
2. Repeat
• if w > child, v =  smaller child
• swap v and w values
• else break

## “Trickle Down” Running Time?

1. Set w to root
2. Repeat
• if w > child, v =  smaller child
• swap v and w values
• else break

## Representing Complete Binary Trees

Previously:

• trees represented as linked nodes

Complete binary trees have much more predictable structure

• can use an array to store complete binary trees efficiently!

## Question 1

For an index $i$, what is the index of $i$’s left child? Right child?

## Question 2

For an index $i$, what is the index of $i$’s parent?

## Question 3

Why didn’t we use arrays to represent (non complete) binary trees?

## Assignment 07

Implement a priority queue using a binary heap!