Queues, Stacks and Heaps - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Queues, Stacks and Heaps

Description:

The binary heap is complete all levels are full with the possible exception of ... Sift the node up the heap if it is larger than its parent until its parent is ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 18
Provided by: vjd
Category:
Tags: heap | heaps | queues | stacks

less

Transcript and Presenter's Notes

Title: Queues, Stacks and Heaps


1
Queues, Stacks and Heaps
2
Queue
  • List structure using the FIFO process
  • Nodes are removed form the front and added to the
    back

A
B
D
C
Front
Back
3
Queue
  • Removing a node (popping)
  • Then adding a node (pushing)
  • Uses include Breadth First Search and other
    graph-related algorithms

B
C
D
Front
Back
B
C
D
Front
Back
A
4
Stack
  • List structure using the FILO process
  • Nodes added to and removed from the top

Top
D
C
B
A
Bottom
5
Stack
  • Removing a node Then adding a node
  • popping pushing

Top
Top
E
C
C
B
B
A
A
Bottom
Bottom
6
Stack
  • Used in Depth First Search and other recursive
    algorithms

7
Tree Basics
  • A tree is a connected graph with no cycles
  • Nodes can have multiple children and at most one
    parent
  • Nodes with no children are called leaves
  • Topmost node called
    the root

Root
Parent of node
4
2
6
Child of node
8
5
7
3
A leaf
8
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

9
Heap
  • Properties of a heap of size n
  • Height of the heap is trunc(log2n)
  • Root of the heap contains
  • the largest value

10
8
3
1
2
5
10
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 2i1
  • The parent of node i is stored at index
    trunc(i/2)

11
Heap
  • A simple heap with array representation

9
8
3
6
2
5
1
4
5
0
1
2
3
4
5
6
7
8
9
Index
0
9
8
3
6
5
2
1
4
5
Value
12
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)

13
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
8
9
6
5
9
5
8
5
4
9
4
6
4
6
14
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
3
6
6
5
6
5
3
5
2
3
2
2
15
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

16
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
  • Dijkstras Algorithm (shortest path)
  • Prims Algorithm (a faster alternative to
    Kruskals for a minimum spanning tree)
  • Simply finding the minimum/maximum value of a
    dynamic list efficiently

17
Problem Examples
  • Shortest path is a fairly common problem, with
    The Cheese Universe from the first training camp
    being a straight-forward example. A heap priority
    queue converts Dijkstras to O((EV)log2n) from
    O(n2).
  • An example of a minimum spanning tree problem for
    which Prims Algorithm might be used is the Caves
    of Caerbannog problem from last years SACO.
Write a Comment
User Comments (0)
About PowerShow.com