© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.

Slides:



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

CS Data Structures II Review COSC 2006 April 14, 2017
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
© 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.
Chapter 15 B External Methods – B-Trees. © 2004 Pearson Addison-Wesley. All rights reserved 15 B-2 B-Trees To organize the index file as an external search.
Dictionaries and Their Implementations
© 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 reserved13 A-1 Chapter 13 Hash Tables.
© 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.
© 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
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
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++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
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 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
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.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
© 2006 Pearson Addison-Wesley. All rights reserved15 A-1 Chapter 15 External Methods.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Tables and Priority Queues
Chapter 12 A Tables.
Algorithm Efficiency and Sorting
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CS202 - Fundamental Structures of Computer Science II
Dictionaries and Their Implementations
Advanced Implementation of Tables
Ch. 12 Tables and Priority Queues
Tables and Priority Queues
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues

© 2006 Pearson Addison-Wesley. All rights reserved12 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-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 A-22 Please open file carrano_ppt12_B.ppt to continue viewing chapter 12.