Chapter 12 A Tables.

Slides:



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

Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 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.
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 reserved12-1 Chapter 12 Tables and Priority Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
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.
Marc Smith and Jim Ten Eyck
COSC 2007 Data Structures II
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.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I.
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 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.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
© 2006 Pearson Addison-Wesley. All rights reserved15 A-1 Chapter 15 External Methods.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Tables and Priority Queues
Algorithm Efficiency and Sorting
Data Abstraction: The Walls
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
CHP - 9 File Structures.
CC 215 Data Structures Queue ADT
Linked Lists Chapter 5 (continued)
Trees Chapter 11 (continued)
Data Abstraction: The Walls
Efficiency of in Binary Trees
Binary Search Tree Chapter 10.
Data Structures Using C++ 2E
External Methods Chapter 15 (continued)
Searching.
CS202 - Fundamental Structures of Computer Science II
A Binary Search Tree Implementation
Searching and Sorting Linear Search Binary Search ; Reading p
Chapter 15 Lists Objectives
Chapter 19 Binary Search Trees.
CS202 - Fundamental Structures of Computer Science II
Dictionaries and Their Implementations
searching Concept: Linear search Binary search
CSE 373: Data Structures and Algorithms
Data Abstraction: The Walls
Binary Trees: Motivation
Advanced Implementation of Tables
Ch. 12 Tables and Priority Queues
Tables and Priority Queues
Advanced Implementation of Tables
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Algorithm Efficiency and Sorting
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Data Abstraction: The Walls
Linked Lists Chapter 5 (continued)
Algorithm Efficiency and Sorting
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Chapter 12 A Tables

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 © 2004 Pearson Addison-Wesley. All rights reserved

The ADT Table Arrange the data to facilitate the search for an item given the value of its search key. 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 © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

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. © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

The ADT Table Interface TableInterface interface Defines the table operations package Tables; import SearchKeys.KeyedItem; public interface TableInterface <T extends KeyedItem<KT>, KT extends Comparable<? Super KT>>{ // Table operations: // Precondition for all operations: // No two items of the table have the same search key. // The table's items are sorted by search key. © 2004 Pearson Addison-Wesley. All rights reserved

public boolean tableIsEmpty(); // Determines whether a table is empty. // Postcondition: Returns true if the table is // empty; otherwise returns false. public int tableLength(); // Determines the length of a table. // Postcondition: Returns the number of items in the table. public void tableInsert(T newItem) throws TableException; // Inserts an item into a table in its proper sorted // order according to the item's search key. // Precondition: The item to be inserted into the // table is newItem, whose search key differs from // all search keys presently in the table. // Postcondition: If the insertion was successful, // newItem is in its proper order in the table. // Otherwise, the table is unchanged, and // TableException is thrown. © 2004 Pearson Addison-Wesley. All rights reserved

public boolean tableDelete(KT searchKey); // Deletes an item with a given search key from a table. // Precondition: searchKey is the search key of the // item to be deleted. // Postcondition: If the item whose search key equals // searchKey existed in the table, the item was // deleted and method returns true. Otherwise, the // table is unchanged and the method returns false. public T tableRetrieve(KT searchKey); // Retrieves an item with a given search key from a table. // item to be retrieved. // Postcondition: If the retrieval was successful, // the table item with the matching search key is // returned. If no such item exists, the method // returns a null reference. } // end TableInterface © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

Selecting an Implementation A binary search tree 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 11.1 © 2004 Pearson Addison-Wesley. All rights reserved

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? © 2004 Pearson Addison-Wesley. All rights reserved

Scenario A: Insertion and Traversal in No Particular Order An unsorted implementation is 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 © 2004 Pearson Addison-Wesley. All rights reserved

Scenario A: Insertion and Traversal in No Particular Order Figure 12.5 Insertion for unsorted linear implementations: a) array based; b) reference based © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

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(log2n) Sequential search of a linked list O(n) © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

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

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 © 2004 Pearson Addison-Wesley. All rights reserved

Summary Linear implementations A binary search tree implementation 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 © 2004 Pearson Addison-Wesley. All rights reserved

Summary * *: unsorted traversal Unsorted reference based Figure 12.7 The average-case order of the operations of the ADT table for various implementations © 2004 Pearson Addison-Wesley. All rights reserved

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 Code: Pages 659 – 661 © 2004 Pearson Addison-Wesley. All rights reserved

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 Code: Pages 661 – 663 © 2004 Pearson Addison-Wesley. All rights reserved