This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CSCI-1200 Data Structures Fall 2010 Lecture 20 Priority Queues and Leftist Heaps Review from Lecture 21 Operators as non-member functions, as member functions, and as friend functions. Queues and Stacks, Whats a Priority Queue? A Priority Queue as a Heap, percolate_up and percolate_down A Heap as a Tree (with nodes & pointers) Todays 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 (non-leaf) node is at location b ( n- 1) / 2 c . The standard library (STL) priority_queue is implemented as a binary heap....
View Full Document
This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.
- Fall '08
- Data Structures