Queues, stacks and heaps

Queues, Stacks and Heaps
Queue
List structure using the FIFO process
Nodes are removed form the front and
added to the back
Front
A
B
C
D
Back
Queue
Removing a node (popping)
Front
B
C
Back
D
Then adding a node (pushing)
Front
B
C
D
A
Back
Uses include Breadth First Search and other
graph-related algorithms
Stack
List structure using the FILO process
Nodes added to and removed from the top
Top
D
C
B
A
Bottom
Stack
Removing a node
Then adding a node
Top
Top
C
E
B
C
A
B
A
Bottom
Bottom
popping
pushing
Stack
Used in Depth First Search and other
recursive algorithms
Tree Basics
A tree is a connected graph with no cycles
Nodes can have multiple children and at
most one parent
Root
Parent of node
4
Nodes with no children
are called leaves
2
6
Topmost node called
the root
Child of node
8
A leaf
5
7
3
Heap
A heap is a binary tree - no more than 2
children per parent
The binary heap is complete – all levels are
full with the possible exception of the last
The value of each node is greater than or
equal to the values of each of its children
Heap
Properties of a heap of size n:
Height of the heap is trunc(log2n)
Root of the heap contains
10
the largest value
8
1
3
5
2
Heap
A heap can be conveniently stored in an array as such:
• The root is stored at index 1
• The children of node i are stored at indices 2i and 2i+1
• The parent of node i is stored at index trunc(i/2)
Heap
A simple heap with array representation
9
8
6
4
3
5
2
1
5
Index 0 1 2 3 4 5 6 7 8 9
Value 0 9 8 3 6 5 2 1 4 5
Heap
Heap construction:
• Read values into array
• For each node from the last parent down to the root:
If the node value is less than either of the children,
switch the node with the greater child
Continue until the node value is greater than or equal
to both children (automatically true if it is a leaf)
• Construction is in O(n)
Heap
Inserting a value:
• Increment the size and add the value as the last node
• Sift the node up the heap if it is larger than its parent until
its parent is greater than it or it has become the root
• Insertion is in O(log2n)
8
6
4
8
5
9
9
4
9
5
6
8
4
5
6
Heap
Deleting the root (when popping):
• Change the value of the root to the value of the last node
in the heap and decrement the size of the heap
• If the node is less than either child, swap it with the larger
child, repeat until it is greater than both children
• Deletion is in O(log2n)
9
6
2
3
5
3
6
2
6
5
3
2
5
Heap
A heap can be used to sort a list of values (heapsort):
• Heapify the list of values
• Pop the root off and reheap
• Repeat until the heap is empty
• Deletion of a node is O(log2n) and this is repeated n times,
so heapsort is in O(nlog2n) (this is also the worst case)
• Heapsort can be done in-place, but it is not a stable sort
Priority Queue
Priority queues are queues which pop the minimum or
maximum value in the queue. As the root of a heap is
always the largest or smallest value in the heap, priority
queues can use a heap structure.
Priority queues have important uses in:
• Dijkstra’s Algorithm (shortest path)
• Prim’s Algorithm (a faster alternative to Kruskal’s for a
minimum spanning tree)
• Simply finding the minimum/maximum value of a dynamic
list efficiently
Problem Examples
Shortest path is a fairly common problem, with The Cheese
Universe from the first training camp being a straightforward example. A heap priority queue converts Dijkstra’s
to O((E+V)log2n) from O(n2).
An example of a minimum spanning tree problem for which
Prim’s Algorithm might be used is the Caves of
Caerbannog problem from last years SACO.