CSCI1200 Data Structures — Fall 2010
Lecture 20 – Priority Queues and Leftist Heaps
Review from Lecture 21
•
Operators as nonmember functions, as member functions, and as friend functions.
•
Queues and Stacks, What’s a Priority Queue?
•
A Priority Queue as a Heap,
percolate_up
and
percolate_down
•
A Heap as a Tree (with nodes & pointers)
Today’s Class
•
A Heap as a Vector
•
Building a Heap
•
Heap Sort
•
Merging heaps are the motivation for
leftist heaps
•
Mathematical background & Basic algorithms
•
Homework 6 Contest Results!
20.1
Vector Implementation
•
In the vector implementation, the tree is never explicitly constructed. Instead the heap is stored as a vector,
and the child and parent “pointers” can be implicitly calculated.
•
To do this, number the nodes in the tree starting with 0 first by level (top to bottom) and then scanning across
each row (left to right). These are the vector indices. Place the values in a vector in this order.
•
As a result, for each subscript,
i
,
–
The parent, if it exists, is at location
b
(
i

1)
/
2
c
.
–
The left child, if it exists, is at location 2
i
+ 1.
–
The right child, if it exists, is at location 2
i
+ 2.
•
For a binary heap containing
n
values, the last leaf is at location
n

1 in the vector and the last internal
(nonleaf) node is at location
b
(
n

1)
/
2
c
.
•
The standard library (STL)
priority_queue
is implemented as a binary heap.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 CUTLER
 Data Structures, Binary heap, leftist heaps

Click to edit the document details