You are on page 1of 20

Heaps

A max heap is a binary tree that satisfies the following properties:


- it is complete
- the data item stored in each node is greater than or equal to
the data items stored in its children

A heap, binary heap, or min heap is a binary tree that satisfies


the following properties:
- it is complete
- the data item stored in each node is less than the data items
stored in its children (known as the heap-order property)

Heaps are typically stored in arrays when they are implemented in


a program. (Note: it’s easier to see the heap properties when
it’s drawn as a tree)
Heaps

Is this a heap? Why or why not?


Heaps

Is this a heap? Why or why not?


Heap Operations
As mentioned earlier, there are two basic operations to be
performed:

- Inserting an item into the heap


- Finding and removing the minimum item from the heap

Conceptually, both operations are fairly simply. But as with AVL


trees, either operation could cause the heap properties to be
violated.

It’s much easier to fix the problems with a heap!


Heap Operations
Calculations (assuming starting with subscript 1):

Root: 1

Left child: 2 * parent’s position

Right child: 2 * parent’s position +1

Parent: Either child’s position / 2

Next free: number of elements + 1


Heap Operations - Insertion
Step 1: Insert the node in the next available spot to maintain
completeness

Step 2: Compare the new node with its parent. If it is smaller than
the parent, swap it with the parent.

Step 3: Continue step 2 until the heap order property is restored.

Steps 2 and 3 are known as the percolate up process.


Heap Operations - Insertion

Insert 14 into the heap


Heap Operations - Insertion

Is this still a heap?

What needs to be done?


Heap Operations - Insertion

Is this a heap yet?

What needs to be done?


Heap Operations - Insertion

How about now?


Heap Operations - Deletion
Step 1: Find the minimum node

Step 2: Replace the minimum node with the last leaf node

Step 3: Compare the replacement with its children. If one of the


children is smaller, swap the replacement with the smaller
child.

Step 4: Repeat step 3 until the heap order property is restored.

Steps 3 and 4 are known as the percolate down process.


Heap Operations - Deletion

Delete the minimum node.


Heap Operations - Deletion

Step 2: Replace with the last leaf node


Heap Operations - Deletion

Step 3: Compare the replacement with


its children. If one of the children
is smaller, swap the replacement
with the smaller child.
Heap Operations - Deletion

Step 4: Repeat step 3 until the heap


order property is restored.
Other Heap Operations
decreaseKey
reduce the value at a specific position by a specific amount.
This could violate the heap order property, so the might need
to re-heapify by doing a percolate-up.

increaseKey
increase the value at a specific position by a specific amount.
Again this could cause a violation. This one is re-heapified by
doing a percolate-down.

remove
remove a node from a specific position in the heap. Done by
performing decreaseKey followed by a regular deletion.
Other Heap Operations
buildHeap
This can be done by performing successive inserts.

or

Insert the nodes in any order as long as completeness is


maintained. Starting with the node at heapSize / 2, continually
percolate-down until a heap is achieved.
Heapify the following binary tree
Start with the node at heapSize / 2 and continually percolate-
down until a heap is achieved.
Heap Pseudocode
percolate down psuedocode:

r = subscript of root of subtree where process will begin


n = number of elements in the entire array
c = subscript of the child
temp = item at position r

temp = array[r]

while ( r * 2 <= n )
c = r * 2
if c != n AND array[c+1] < array[c]
increment c by 1
endif

if array[c] < temp


array[r] = array[c]
else
break out of loop
endif

r = c
endwhile

array[r] = temp
Heap Pseudocode
percolate up psuedocode:

h = subscript of where the item will be inserted


size = size of the heap BEFORE inserting

Increment size by 1
Set h equal to sizw

while ( h > 1 AND insertItem < array[h/2] )


array[h] = array[h/2]
h = h / 2
endwhile

array[r] = insertItem

You might also like