© 2006 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 Tables and Priority Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 11 Tables & Priority Queues I.
Advertisements

Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
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.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
Marc Smith and Jim Ten Eyck
COSC 2007 Data Structures II
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
Priority Queues Dr. David Matuszek
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Fall 2015CS202 - Fundamental Structures of Computer Science II1 Heaps Appropriate for problems that must manage data by value. Some important operations.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Chapter 15 A External Methods. © 2004 Pearson Addison-Wesley. All rights reserved 15 A-2 A Look At External Storage External storage –Exists beyond the.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
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”
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
© 2006 Pearson Addison-Wesley. All rights reserved15 A-1 Chapter 15 External Methods.
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.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Spring 2016CS202 - Fundamental Structures of Computer Science II1 Tables Appropriate for problems that must manage data by value. Some important operations.
Tables and Priority Queues
Chapter 12 A Tables.
Tables and Priority Queues
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Efficiency of in Binary Trees
CS202 - Fundamental Structures of Computer Science II
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.
CS202 - Fundamental Structures of Computer Science II
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Ch. 12 Tables and Priority Queues
Tables and Priority Queues
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Priority Queues.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Tables and Priority Queues
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
EE 312 Software Design and Implementation I
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 Tables and Priority Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

© 2006 Pearson Addison-Wesley. All rights reserved12-2 The ADT Table The ADT table, or dictionary –Uses a search key to identify its items –Its items are records that contain several pieces of data Figure 12-1 An ordinary table of cities

© 2006 Pearson Addison-Wesley. All rights reserved12-3 The ADT Table Operations of the ADT table –Create an empty table –Determine whether a table is empty –Determine the number of items in a table –Insert a new item into a table –Delete the item with a given search key from a table –Retrieve the item with a given search key from a table –Traverse the items in a table in sorted search-key order

© 2006 Pearson Addison-Wesley. All rights reserved12-4 The ADT Table Pseudocode for the operations of the ADT table createTable() // Creates an empty table. tableIsEmpty() // Determines whether a table is empty. tableLength() // Determines the number of items in a table. tableInsert(newItem) throws TableException // Inserts newItem into a table whose items have // distinct search keys that differ from newItem’s // search key. Throws TableException if the // insertion is not successful

© 2006 Pearson Addison-Wesley. All rights reserved12-5 The ADT Table Pseudocode for the operations of the ADT table (Continued) tableDelete(searchKey) // Deletes from a table the item whose search key // equals searchKey. Returns false if no such item // exists. Returns true if the deletion was // successful. tableRetrieve(searchKey) // Returns the item in a table whose search key // equals searchKey. Returns null if no such item // exists. tableTraverse() // Traverses a table in sorted search-key order.

© 2006 Pearson Addison-Wesley. All rights reserved12-6 The ADT Table Value of the search key for an item must remain the same as long as the item is stored in the table KeyedItem class –Contains an item’s search key and a method for accessing the search-key data field –Prevents the search-key value from being modified once an item is created TableInterface interface –Defines the table operations

© 2006 Pearson Addison-Wesley. All rights reserved12-7 Selecting an Implementation Categories of linear implementations –Unsorted, array based –Unsorted, referenced based –Sorted (by search key), array based –Sorted (by search key), reference based Figure 12-3 The data fields for two sorted linear implementations of the ADT table for the data in Figure 12-1: a) array based; b) reference based

© 2006 Pearson Addison-Wesley. All rights reserved12-8 Selecting an Implementation A binary search implementation –A nonlinear implementation Figure 12-4 The data fields for a binary search tree implementation of the ADT table for the data in Figure 12-1

© 2006 Pearson Addison-Wesley. All rights reserved12-9 Selecting an Implementation The binary search tree implementation offers several advantages over linear implementations The requirements of a particular application influence the selection of an implementation –Questions to be considered about an application before choosing an implementation What operations are needed? How often is each operation required?

© 2006 Pearson Addison-Wesley. All rights reserved12-10 Scenario A: Insertion and Traversal in No Particular Order An unsorted order in efficient –Both array based and reference based tableInsert operation is O(1) Array based versus reference based –If a good estimate of the maximum possible size of the table is not available Reference based implementation is preferred –If a good estimate of the maximum possible size of the table is available The choice is mostly a matter of style

© 2006 Pearson Addison-Wesley. All rights reserved12-11 Scenario A: Insertion and Traversal in No Particular Order Figure 12-5 Insertion for unsorted linear implementations: a) array based; b) reference based

© 2006 Pearson Addison-Wesley. All rights reserved12-12 Scenario A: Insertion and Traversal in No Particular Order A binary search tree implementation is not appropriate –It does more work than the application requires It orders the table items –The insertion operation is O(log n) in the average case

© 2006 Pearson Addison-Wesley. All rights reserved12-13 Scenario B: Retrieval Binary search –An array-based implementation Binary search can be used if the array is sorted –A reference-based implementation Binary search can be performed, but is too inefficient to be practical A binary search of an array is more efficient than a sequential search of a linked list –Binary search of an array Worst case: O(log 2 n) –Sequential search of a linked list O(n)

© 2006 Pearson Addison-Wesley. All rights reserved12-14 Scenario B: Retrieval For frequent retrievals –If the table’s maximum size is known A sorted array-based implementation is appropriate –If the table’s maximum size is not known A binary search tree implementation is appropriate

© 2006 Pearson Addison-Wesley. All rights reserved12-15 Scenario C: Insertion, Deletion, Retrieval, and Traversal in Sorted Order Steps performed by both insertion and deletion –Step 1: Find the appropriate position in the table –Step 2: Insert into (or delete from) this position Step 1 –An array-based implementation is superior than a reference-based implementation Step 2 –A reference-based implementation is superior than an array-based implementation A sorted array-based implementation shifts data during insertions and deletions

© 2006 Pearson Addison-Wesley. All rights reserved12-16 Scenario C: Insertion, Deletion, Retrieval, and Traversal in Sorted Order Figure 12-6 Insertion for sorted linear implementations: a) array based; b) reference based

© 2006 Pearson Addison-Wesley. All rights reserved12-17 Scenario C: Insertion, Deletion, Retrieval, and Traversal in Sorted Order Insertion and deletion operations –Both sorted linear implementations are comparable, but neither is suitable tableInsert and tableDelete operations –Sorted array-based implementation is O(n) –Sorted reference-based implementation is O(n) –Binary search tree implementation is suitable It combines the best features of the two linear implementations

© 2006 Pearson Addison-Wesley. All rights reserved12-18 A Sorted Array-Based Implementation of the ADT Table Linear implementations –Useful for many applications despite certain difficulties A binary search tree implementation –In general, can be a better choice than a linear implementation A balanced binary search tree implementation –Increases the efficiency of the ADT table operations

© 2006 Pearson Addison-Wesley. All rights reserved12-19 A Sorted Array-Based Implementation of the ADT Table Figure 12-7 The average-case order of the operations of the ADT table for various implementations

© 2006 Pearson Addison-Wesley. All rights reserved12-20 A Sorted Array-Based Implementation of the ADT Table Reasons for studying linear implementations –Perspective –Efficiency –Motivation TableArrayBased class –Provides an array-based implementation of the ADT table –Implements TableInterface

© 2006 Pearson Addison-Wesley. All rights reserved12-21 A Binary Search Tree Implementation of the ADT Table TableBSTBased class –Represents a nonlinear reference-based implementation of the ADT table –Uses a binary search tree to represent the items in the ADT table Reuses the class BinarySearchTree

© 2006 Pearson Addison-Wesley. All rights reserved12-22 The ADT Priority Queue: A Variation of the ADT Table The ADT priority queue –Orders its items by a priority value –The first item removed is the one having the highest priority value Operations of the ADT priority queue –Create an empty priority queue –Determine whether a priority queue is empty –Insert a new item into a priority queue –Retrieve and then delete the item in a priority queue with the highest priority value

© 2006 Pearson Addison-Wesley. All rights reserved12-23 The ADT Priority Queue: A Variation of the ADT Table Pseudocode for the operations of the ADT priority queue createPQueue() // Creates an empty priority queue. pqIsEmpty() // Determines whether a priority queue is // empty.

© 2006 Pearson Addison-Wesley. All rights reserved12-24 The ADT Priority Queue: A Variation of the ADT Table Pseudocode for the operations of the ADT priority queue (Continued) pqInsert(newItem) throws PQueueException // Inserts newItem into a priority queue. // Throws PQueueException if priority queue is // full. pqDelete() // Retrieves and then deletes the item in a // priority queue with the highest priority // value.

© 2006 Pearson Addison-Wesley. All rights reserved12-25 The ADT Priority Queue: A Variation of the ADT Table Possible implementations –Sorted linear implementations Appropriate if the number of items in the priority queue is small Array-based implementation –Maintains the items sorted in ascending order of priority value Reference-based implementation –Maintains the items sorted in descending order of priority value

© 2006 Pearson Addison-Wesley. All rights reserved12-26 The ADT Priority Queue: A Variation of the ADT Table Figure 12-9a and 12-9b Some implementations of the ADT priority queue: a) array based; b) reference based

© 2006 Pearson Addison-Wesley. All rights reserved12-27 The ADT Priority Queue: A Variation of the ADT Table Possible implementations (Continued) –Binary search tree implementation Appropriate for any priority queue Figure 12-9c Some implementations of the ADT priority queue: c) binary search tree

© 2006 Pearson Addison-Wesley. All rights reserved12-28 Heaps A heap is a complete binary tree –That is empty or –Whose root contains a search key greater than or equal to the search key in each of its children, and –Whose root has heaps as its subtrees

© 2006 Pearson Addison-Wesley. All rights reserved12-29 Heaps Maxheap –A heap in which the root contains the item with the largest search key Minheap –A heap in which the root contains the item with the smallest search key

© 2006 Pearson Addison-Wesley. All rights reserved12-30 Heaps Pseudocode for the operations of the ADT heap createHeap() // Creates an empty heap. heapIsEmpty() // Determines whether a heap is empty. heapInsert(newItem) throws HeapException // Inserts newItem into a heap. Throws // HeapException if heap is full. heapDelete() // Retrieves and then deletes a heap’s root // item. This item has the largest search key.

© 2006 Pearson Addison-Wesley. All rights reserved12-31 Heaps: An Array-based Implementation of a Heap Data fields –items : an array of heap items –size : an integer equal to the number of items in the heap Figure A heap with its array representation

© 2006 Pearson Addison-Wesley. All rights reserved12-32 Heaps: heapDelete Step 1: Return the item in the root –Results in disjoint heaps Figure 12-12a a) Disjoint heaps

© 2006 Pearson Addison-Wesley. All rights reserved12-33 Heaps: heapDelete Step 2: Copy the item from the last node into the root –Results in a semiheap Figure 12-12b b) a semiheap

© 2006 Pearson Addison-Wesley. All rights reserved12-34 Heaps: heapDelete Step 3: Transform the semiheap back into a heap –Performed by the recursive algorithm heapRebuild Figure Recursive calls to heapRebuild

© 2006 Pearson Addison-Wesley. All rights reserved12-35 Heaps: heapDelete Efficiency –heapDelete is O(log n) Figure Deletion from a heap

© 2006 Pearson Addison-Wesley. All rights reserved12-36 Heaps: heapInsert Strategy –Insert newItem into the bottom of the tree –Trickle new item up to appropriate spot in the tree Efficiency: O(log n) Heap class –Represents an array-based implementation of the ADT heap Figure Insertion into a heap

© 2006 Pearson Addison-Wesley. All rights reserved12-37 A Heap Implementation of the ADT Priority Queue Priority-queue operations and heap operations are analogous –The priority value in a priority-queue corresponds to a heap item’s search key PriorityQueue class –Has an instance of the Heap class as its data field

© 2006 Pearson Addison-Wesley. All rights reserved12-38 A Heap Implementation of the ADT Priority Queue A heap implementation of a priority queue –Disadvantage Requires the knowledge of the priority queue’s maximum size –Advantage A heap is always balanced Finite, distinct priority values –A heap of queues Useful when a finite number of distinct priority values are used, which can result in many items having the same priority value

© 2006 Pearson Addison-Wesley. All rights reserved12-39 Heapsort Strategy –Transforms the array into a heap –Removes the heap's root (the largest element) by exchanging it with the heap’s last element –Transforms the resulting semiheap back into a heap Efficiency –Compared to mergesort Both heapsort and mergesort are O(n * log n) in both the worst and average cases Advantage over mergesort –Heapsort does not require a second array –Compared to quicksort Quicksort is the preferred sorting method

© 2006 Pearson Addison-Wesley. All rights reserved12-40 Heapsort Figure a) The initial contents of anArray; b) anArray’s corresponding binary tree Figure Heapsort partitions an array into two regions

© 2006 Pearson Addison-Wesley. All rights reserved12-41 Tables and Priority Queues in JFC: The JFC Map Interface Map interface –Provides the basis for numerous other implementations of different kinds of maps public interface Map methods –void clear() –boolean containsKey(Object key) –boolean containsValue(Object value) –Set > entrySet() –V get(Object key);

© 2006 Pearson Addison-Wesley. All rights reserved12-42 Tables and Priority Queues in JFC: The JFC Map Interface public interface Map methods (continued) –boolean isEmpty() –Set keySet() –V put(K key, V value) –V remove(Object key) –Collection values()

© 2006 Pearson Addison-Wesley. All rights reserved12-43 The JFC Set Interface Set interface –Ordered collection –Stores single value entries –Does not allow for duplicate elements public interface Set methods –boolean add(T o) –boolean addAll(Collection c) –void clear() –boolean contains(Object o) –boolean isEmpty()

© 2006 Pearson Addison-Wesley. All rights reserved12-44 The JFC Set Interface public interface Set methods (continued) –Iterator iterator() –boolean remove(Object o) –boolean removeAll(Collection c) –boolean retainAll(Collection c) –int size()

© 2006 Pearson Addison-Wesley. All rights reserved12-45 The JFC PriorityQueue Class PriorityQueue class –Has a single data-type parameter with ordered elements –Relies on the natural ordering of the elements As provided by the Comparable interface or a Comparator object –Elements in queue are ordered in ascending order public Class PriorityQueue methods –PriorityQueue(int initialCapacity) –PriorityQueue(int initialCapacity, Comparator comparator) –boolean add(T o) –void clear() –boolean contains(Object o)

© 2006 Pearson Addison-Wesley. All rights reserved12-46 The JFC PriorityQueue Class public Class PriorityQueue methods (continued) –Comparator comparator() –T element() –Iterator iterator() –boolean offer(T o) –T peek() –T poll() –boolean remove(Object o) –int size()

© 2006 Pearson Addison-Wesley. All rights reserved12-47 Summary The ADT table supports value-oriented operations The linear implementations (array based and reference based) of a table are adequate only in limited situations or for certain operations A nonlinear reference-based (binary search tree) implementation of the ADT table provides the best aspects of the two linear implementations A priority queue, a variation of the ADT table, has operations which allow you to retrieve and remove the item with the largest priority value

© 2006 Pearson Addison-Wesley. All rights reserved12-48 Summary A heap that uses an array-based representation of a complete binary tree is a good implementation of a priority queue when you know the maximum number of items that will be stored at any one time Efficiency –Heapsort, like mergesort, has good worst-case and average-case behaviors, but neither algorithms is as good in the average case as quicksort –Heapsort has an advantage over mergesort in that it does not require a second array Tables and priority queues in JFC –Map interface –Set interface – PriorityQueue class