COSC 2007 Data Structures II Chapter 12 Tables
COSC 2007 Data Structures II Chapter 12 Tables & Priority Queues I
Topics n Tables n n 2 Operations Implementation S. Xu
ADT Table n Table example: n City population City (key) (sorted) Athens Barcelona Cairo London New York Paris Toronto Venice n Population Greece Spain Egypt England U. S. A France Canada Italy 2, 500, 000 1, 800, 000 16, 500, 000 9, 400, 000 7, 300, 000 2, 200, 000 300, 000 Questions (if we use BST or other ADT to store) n n 3 Country What is the population of London? Which city is in Italy? S. Xu
ADT Table n Table example: n City population City (key) (sorted) Athens Barcelona Cairo London New York Paris Toronto Venice n n 4 Population Greece Spain Egypt England U. S. A France Canada Italy 2, 500, 000 1, 800, 000 16, 500, 000 9, 400, 000 7, 300, 000 2, 200, 000 300, 000 Questions (if we use BST or other ADT to store) n n Country What is the population of London? Which city is in Italy? Search can not be always done by using binary search S. Xu
ADT Table Dictionary key student name hw 1 123 Stan Smith 49 . . . 124 Sue Margolin 56 . . . 125 Billie King 34 . . . 167 Roy Miller 39 . . . Member Record 5 S. Xu
ADT Table n ADT Table (Dictionary) n Elements are records containing several pieces of information n Value oriented – Data are arranged to facilitate search n Implementation n n Needs rapid retrieval of items Assumption: n All items in a table have distinct search keys n n 6 Some tables allow duplicate search keys Items should not be inserted if they already exist in the table S. Xu
ADT Table 7 S. Xu
ADT Table n Example: Populations n Which implementation will be best suited for each of the following operations? Display in alphabetical order, the name of each city & its population n Increase the population of each city by 10 n Delete all cities with population < 1, 000 n 8 S. Xu
ADT Table n Table example: n City population City (key) (sorted) Athens Barcelona Cairo London New York Paris Toronto Venice 9 Country Population Greece Spain Egypt England U. S. A France Canada Italy 2, 500, 000 1, 800, 000 16, 500, 000 9, 400, 000 7, 300, 000 2, 200, 000 300, 000 S. Xu
ADT Table n Operations: Keyed. Item Class public abstract class Keyed. Item { private Comparable search. Key; public Keyed. Item(Comparable key ) { search. Key = key; } public Comparable get. Key() { return search. Key; } // end get. Key } // end Keyed. Item class n 10 Only one constructor is available, and not set. Key () is defined --- Why? S. Xu
ADT Table n 11 Example: Populations public class City extends Keyed. Item { private string country; //city name will be the search. Key private int Pop; public City (String the. City, String the. Country, int new. Pop ) { super (the. City); country = the. Country; pop=new. Pop; } // constructors public string to. String () { return get. Key()+”, “ +country+” “ + pop; } // other methods } // end City class S. Xu
ADT Table n Example: n Display in alphabetical order, the name of each city & its population n Increase the population of each city by 10 n n 12 The order of the visit is important Method to display an item should be passed as the Iterator +display. Item(an. Item) Display an. Item. city. Name( ) Display an. Item. get. Population ( ) The order of the visit is not important Method to update the population should be passed as the Iterator +update. Population(an. Item) an. Item. set. Population(1. 1 * an. Item. get. Population( )) S. Xu
ADT Table n Example: n Delete all cities with population < 1, 000 n n The order of the visit is not important method to delete populations less than the desired should be passed as the iterator +delete. Small(Table, an. Item) if (an. Item. get. Population( ) < 1, 000) t. table. Delete(an. Item) n Problem: n 13 Table changes (item is deleted) during traversal S. Xu
ADT Table n Implementation n Linear implementation of tables could be: n Unsorted n n n Array based Reference based Sorted (by search key) n n Array based Reference based Both implementations should maintain a count of the items in the table n More? n 14 S. Xu
ADT Table n Implementation n ADT Table can also be implemented using ADT list n Sorted List n BST n 15 S. Xu
ADT Table n Implementation n Example n Sorted (by search key) n Array based 9 Athens Barcelona size 16 0 n reference based 9 • Athens size head . . . 1 Venice size-1 Barcelona . . . MAX_TABLE -1 Venice S. Xu
ADT Table n Implementation n Using ADT BST New York 9 size Barcelona Athens 17 Toronto Cairo London Venice Paris Rome S. Xu
ADT Table n 18 How to choose an implementation? S. Xu
ADT Table n How to choose an implementation? n Factors affecting the selection of an implementation: Necessary operations n Expected frequency of occurrence of each operation n Required response times for the operations n 19 S. Xu
Scenario A: Insert & Traverse (no Particular Order) n Example: Raise money for local Charity n n n Insert fund-raiser ideas into a table and later print a report Items can be sorted or unsorted No operation requires a specific order Maintaining items in a specific order has no advantage n n n 20 For array based implementation, insert items after the last item in the array (Big O? ? ) For reference based implementation, insert items at the beginning of the linked list (Big O? ? ) Others? Why? S. Xu
Scenario A: Insert & Traverse (no Particular Order) n Insertion for unsorted linear implementation n Array-based K+1 Athens Barcelona . . . Venice K-1 n K+1 New item K . . . K+1. . . Reference-based • Athens Barcelona Venice New item 21 S. Xu
Scenario B: Retrieval n Example: Word Processor's Thesaurus n n n Frequent retrievals require table implementation that allows efficient searching for items No deletions or insertions are necessary Which implementation? n n 22 Reference-based implementation? Binary search performs fast retrieval? What is the problem? Sorted array-based implementation? A Balanced BST would also be suitable, why? S. Xu
Scenario B: Retrieval n Example: Word Processor's Thesaurus n n n Frequent retrievals require table implementation that allows efficient searching for items No deletions or insertions are necessary Which implementation? n n Reference-based implementation? , must traverse whole the LL Binary search performs fast retrieval? What is the problem? n n n 23 Impractical for reference-based Sorted array-based implementation? good A Balanced BST would also be suitable, why? Since thesaurus does not change S. Xu
Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order n Example: Computerized Library Catalog n n n Elements are sorted Retrieval is the most frequent operation Both insertion & deletion perform two steps: n n n 24 1. Find the appropriate position in the table 2. Insert into (or delete from) this position We need both steps together. How about efficiency? If the table is sorted, both table. Insert & table. Delete will require amount of time ~ (O(N)) in either array or reference-based implementations More? S. Xu
Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order n Example: Computerized Library Catalog n n n Elements are sorted Retrieval is the most frequent operation Both insertion & deletion perform two steps: n n n 25 1. Find the appropriate position in the table 2. Insert into (or delete from) this position We need both steps together. How about efficiency? If the table is sorted, both table. Insert & table. Delete will require amount of time ~ (O(N)) in either array or reference-based implementations More? BST implementation combines the feature of the two linear implementations S. Xu
Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order n Insertion for sorted linear implementation n Array-based New item K+1 items n Data 0 . . . i-1 i Data i+1 K . . . Reference-based • head 26 Data Data New item S. Xu
Conclusion n Linear implementations n n n BST implementation n n 27 Better if they have minimum height We can keep the height of the tree near Log 2 (N) by using the tree balancing algorithms Reference-based implementation of BST n n Less sophisticated & generally require more time than BST implementation Appropriate for tables containing small number of items Permits dynamic allocation and can handle tables whose maximum size is unknown Efficiently perform insertions & deletions ADT Table is appropriate when you have a data base to maintain and search by value S. Xu
Comparison of Time Complexity (average) Operation Insertion Deletion Retrieval Traversal Unsorted Array O(1) O(n) Unsorted reference O(1) O(n) Sorted Array O(n) O(logn) Sorted reference O(n) BST O(logn) 28 O(n) O(n) S. Xu
Review n The ADT table is also known as a ______. n n 29 map queue Heap dictionary
Review n An array based implementation of an ADT is a ______ implementation. n n 30 vertical linear nonlinear compound
Review n Which of the following is true about a linear implementation of a table? n n 31 the unsorted implementations must insert a new item into its proper position as determined by the value of its search key the sorted implementations can insert a new item into any convenient location the sorted implementations maintain a count of the current number of items in the table the unsorted implementations do not maintain a count of the current number of items in the table
Review n A(n) ______ implementation of a table is nonlinear. n n 32 list linked list binary search tree array
Review n In an unsorted array-based implementation of a table, a new item is inserted at location ______. n n 33 items[size] items[size-1] items[size+2]
Review n In an unsorted array-based implementation of the ADT table, the insertion operation is ______. n n 34 O(1) O(n 2) O(log n)
Review n In an unsorted array-based implementation of the ADT table, the retrieval operation is ______. n n 35 O(1) O(n 2) O(log n)
Review n The sorted reference-based implementation of the table. Delete operation is ______. n n 36 O(1) O(log n) O(n 2)
- Slides: 36