Chapter 9 The Priority Queue ADT

Slides:



Advertisements
Similar presentations
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Advertisements

1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
Heaps and Priority Queues CS 3358 – Data Structures.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
Ceng-112 Data Structures I Figure 9-1 The heap is guaranteed to hold the largest node of the tree in the root. The smaller nodes of a heap can be.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Heaps & Priority Queues
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CSE373: Data Structures & Algorithms Priority Queues
Partially Ordered Data ,Heap,Binary Heap
CSE373: Data Structures & Algorithms
Chapter 9 Priority Queues, Heaps, and Graphs
Heap Chapter 9 Objectives Define and implement heap structures
Data Structures and Algorithms for Information Processing
CS 201 Data Structures and Algorithms
Section 9.2b Adding and Removing from a Heap.
Source: Muangsin / Weiss
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heap Chapter 9 Objectives Upon completion you will be able to:
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Priority Queues, Heaps, and Graphs
- Alan Perlis Heaps "You think you know when you can learn,
7/23/2009 Many thanks to David Sun for some of the included slides!
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Priority Queues.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CSCE 3110 Data Structures and Algorithm Analysis
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Computer Science 2 Heaps.
Chapter 16 Tree Implementations
Ch. 12 Tables and Priority Queues
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Chapter 9 Priority Queues and Sets
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
Heaps and Priority Queues
Priority Queues.
CS 367 – Introduction to Data Structures
Priority Queues.
C++ Plus Data Structures
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Heaps.
Presentation transcript:

Chapter 9 The Priority Queue ADT

Chapter 9: The Priority Queue ADT 9.1 – The Priority Queue Interface 9.2 – Priority Queue Implementations 9.3 – The Heap 9.4 – The Heap Implementation

9.1 The Priority Queue Interface A priority queue is an abstract data type with an interesting accessing protocol - only the highest-priority element can be accessed Priority queues are useful for any application that involves processing items by priority

The Interface package ch09.priorityQueues; public interface PriQueueInterface<T> { void enqueue(T element); // Throws PriQOverflowException if this priority queue is full; // otherwise, adds element to this priority queue. T dequeue(); // Throws PriQUnderflowException if this priority queue is empty; // otherwise, removes element with highest priority from this // priority queue and returns it. boolean isEmpty(); // Returns true if this priority queue is empty; otherwise, returns false. boolean isFull(); // Returns true if this priority queue is full; otherwise, returns false. int size(); // Returns the number of elements in this priority queue. }

9.2 Priority Queue Implementations There are many ways to implement a priority queue An Unsorted List - dequeuing would require searching through the entire list An Array-Based Sorted List - Enqueuing is expensive A Sorted Linked List - Enqueuing again is 0(N) A Binary Search Tree - On average, 0(log2N) steps for both enqueue and dequeue A Heap - (next section) guarantees 0(log2N) steps, even in the worst case

9.3 The Heap Heap  An implementation of a Priority Queue based on a complete binary tree, each of whose elements contains a value that is greater than or equal to the value of each of its children In other words, a heap is an implementation of a Priority Queue that uses a binary tree that satisfies two properties the shape property: the tree must be a complete binary tree the order property: for every node in the tree, the value stored in that node is greater than or equal to the value in each of its children.

Tree Terminology A full binary tree A complete binary tree

Examples

Two Heaps Containing the Letters ‘A’ through ‘J’

The dequeue operation Steps d,e,f represent the “reheap down” operation

The enqueue operation steps b, c represent the “reheap up” operation

9.4 The Heap Implementation

A Non-linked Representation of Binary Trees A binary tree can be stored in an array in such a way that the relationships in the tree are not physically represented by link members, but are implicit in the algorithms that manipulate the tree stored in the array. We store the tree elements in the array, level by level, left-to-right. We call the array elements and store the index of the last tree element in a variable lastIndex. The tree elements are stored with the root in elements[0] and the last node in elements[lastIndex].

A Binary Tree and Its Array Representation

A Binary Search Tree Stored in an Array with Dummy Values

Array Representation continued To implement the algorithms that manipulate the tree, we must be able to find the left and right child of a node in the tree: elements[index] left child is in elements[index*2 + 1] elements[index] right child is in elements[index*2 + 2] We can also can determine the location of its parent node: elements[index]’s parent is in elements[(index – 1)/2]. This representation works best, space wise, if the tree is complete (which it is for a heap)

Beginning of HeapPriQ.java //------------------------------------------------------------------------- // HeapPriQ.java by Dale/Joyce/Weems Chapter 9 // Priority Queue using Heap (implemented with an ArrayList) // // Two constructors are provided: one that use the natural order of the // elements as defined by their compareTo method and one that uses an // ordering based on a comparator argument. package ch09.priorityQueues; import java.util.*; // ArrayList, Comparator public class HeapPriQ<T> implements PriQueueInterface<T> { protected ArrayList<T> elements; // priority queue elements protected int lastIndex; // index of last element in priority queue protected int maxIndex; // index of last position in ArrayList protected Comparator<T> comp; . . .

The enqueue method public void enqueue(T element) throws PriQOverflowException // Throws PriQOverflowException if this priority queue is full; // otherwise, adds element to this priority queue. { if (lastIndex == maxIndex) throw new PriQOverflowException("Priority queue is full"); else lastIndex++; elements.add(lastIndex, element); reheapUp(element); } The reheapUp algorithm is pictured on the next slide

reheapUp operation private void reheapUp(T element) // Current lastIndex position is empty. // Inserts element into the tree and ensures shape and order properties. { int hole = lastIndex; while ((hole > 0) // hole is not root and element > hole's parent && (comp.compare(element, elements.get((hole - 1) / 2)) > 0)) elements.set(hole,elements.get((hole - 1) / 2)); // move hole's parent down hole = (hole - 1) / 2; // move hole up } elements.set(hole, element); // place element into final hole

The dequeue method public T dequeue() throws PriQUnderflowException // Throws PriQUnderflowException if this priority queue is empty; // otherwise, removes element with highest priority from this // priority queue and returns it. { T hold; // element to be dequeued and returned T toMove; // element to move down heap if (lastIndex == -1) throw new PriQUnderflowException("Priority queue is empty"); else hold = elements.get(0); // remember element to be returned toMove = elements.remove(lastIndex); // element to reheap down lastIndex--; // decrease priority queue size if (lastIndex != -1) reheapDown(toMove); // restore heap properties return hold; // return largest element } The reheapDown algorithm is pictured on the next slide

reheapDown operation private void reheapDown(T element) // Current root position is "empty"; // Inserts element into the tree and ensures shape and order properties. { int hole = 0; // current index of hole int next; // next index where hole should move to next = newHole(hole, element); // find next hole while (next != hole) elements.set(hole,elements.get(next)); // move element up hole = next; // move hole down next = newHole(hole, element); // find next hole } elements.set(hole, element); // fill in the final hole

The newHole method private int newHole(int hole, T element) // If either child of hole is larger than element return the index // of the larger child; otherwise return the index of hole. { int left = (hole * 2) + 1; int right = (hole * 2) + 2; if (left > lastIndex) // hole has no children return hole; else if (left == lastIndex) // hole has left child only if (comp.compare(element, elements.get(left)) < 0) // element < left child return left; // element >= left child // hole has two children if (comp.compare(elements.get(left), elements.get(right)) < 0) // left child < right child if (comp.compare(elements.get(right), element) <= 0) // right child <= element // element < right child return right; // left child >= right child if (comp.compare(elements.get(left), element) <= 0) // left child <= element } The newHole method

Heaps Versus Other Representations of Priority Queues