Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I.

Similar presentations


Presentation on theme: "COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I."— Presentation transcript:

1 COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I

2 S. Xu 2 Topics Tables Operations Implementation

3 S. Xu 3 ADT Table Table example: City population City (key)CountryPopulation (sorted) AthensGreece 2,500,000 BarcelonaSpain 1,800,000 CairoEgypt16,500,000 LondonEngland 9,400,000 New YorkU.S.A 7,300,000 ParisFrance 2,200,000 TorontoCanada 3,200,000 VeniceItaly 300,000 Questions (if we use BST or other ADT to store) What is the population of London? Which city is in Italy?

4 S. Xu 4 ADT Table Table example: City population City (key)CountryPopulation (sorted) AthensGreece 2,500,000 BarcelonaSpain 1,800,000 CairoEgypt16,500,000 LondonEngland 9,400,000 New YorkU.S.A 7,300,000 ParisFrance 2,200,000 TorontoCanada 3,200,000 VeniceItaly 300,000 Questions (if we use BST or other ADT to store) What is the population of London? Which city is in Italy? Search can not be always done by using binary search

5 S. Xu 5 ADT Table Dictionary Member Record keystudent namehw1... 123Stan Smith49... 124Sue Margolin56... 125Billie King34... 167Roy Miller39...

6 S. Xu 6 ADT Table ADT Table (Dictionary) Elements are records containing several pieces of information Value oriented – Data are arranged to facilitate search Implementation Needs rapid retrieval of items Assumption: All items in a table have distinct search keys Some tables allow duplicate search keys Items should not be inserted if they already exist in the table

7 S. Xu 7 ADT Table

8 S. Xu 8 ADT Table Example: Populations Which implementation will be best suited for each of the following operations? Display in alphabetical order, the name of each city & its population Increase the population of each city by 10 Delete all cities with population < 1,000,000

9 S. Xu 9 ADT Table Table example: City population City (key)CountryPopulation (sorted) AthensGreece 2,500,000 BarcelonaSpain 1,800,000 CairoEgypt16,500,000 LondonEngland 9,400,000 New YorkU.S.A 7,300,000 ParisFrance 2,200,000 TorontoCanada 3,200,000 VeniceItaly 300,000

10 S. Xu 10 ADT Table Operations: KeyedItem Class public abstract class KeyedItem { private Comparable searchKey; public KeyedItem(Comparable key ) { searchKey = key; } public Comparable getKey() { return searchKey; } // end getKey } // end KeyedItem class Only one constructor is available, and not setKey () is defined --- Why?

11 S. Xu 11 ADT Table Example: Populations public class City extends KeyedItem { private string country; //city name will be the searchKey private int Pop; public City (String theCity, String theCountry, int newPop ) { super (theCity); country = theCountry; pop=newPop; }// constructors public string toString () { return getKey()+”, “ +country+” “ + pop; } // other methods }// end City class

12 S. Xu 12 ADT Table Example: Display in alphabetical order, the name of each city & its population The order of the visit is important Method to display an item should be passed as the Iterator +displayItem(anItem) Display anItem.cityName( ) Display anItem.getPopulation ( ) Increase the population of each city by 10 The order of the visit is not important Method to update the population should be passed as the Iterator +updatePopulation(anItem) anItem.setPopulation(1.1 * anItem.getPopulation( ))

13 S. Xu 13 ADT Table Example: Delete all cities with population < 1,000,000 The order of the visit is not important method to delete populations less than the desired should be passed as the iterator +deleteSmall(Table, anItem) if (anItem.getPopulation( ) < 1,000,000) t.tableDelete(anItem) Problem: Table changes (item is deleted) during traversal

14 S. Xu 14 ADT Table Implementation Linear implementation of tables could be: Unsorted Array based Reference based Sorted (by search key) Array based Reference based Both implementations should maintain a count of the items in the table More?

15 S. Xu 15 ADT Table Implementation ADT Table can also be implemented using ADT list Sorted List BST

16 S. Xu 16 ADT Table Implementation Example Sorted (by search key) Array based reference based Athens Barcelona... Venice... 9 size 01 size-1 MAX_TABLE -1 AthensBarcelonaVenice 9 size head

17 S. Xu 17 ADT Table Implementation Using ADT BST New York Cairo Venice ParisLondon Barcelona RomeAthens Toronto 9 size

18 S. Xu 18 ADT Table How to choose an implementation?

19 S. Xu 19 ADT Table How to choose an implementation? Factors affecting the selection of an implementation: Necessary operations Expected frequency of occurrence of each operation Required response times for the operations

20 S. Xu 20 Scenario A: Insert & Traverse (no Particular Order) Example: Raise money for local Charity 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 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?

21 S. Xu 21 Scenario A: Insert & Traverse (no Particular Order) Insertion for unsorted linear implementation Array-based Reference-based Athens Barcelona... Venice... K+1 New item K-1 K K+1... AthensBarcelonaVenice K+1 New item

22 S. Xu 22 Scenario B: Retrieval Example: Word Processor's Thesaurus Frequent retrievals require table implementation that allows efficient searching for items No deletions or insertions are necessary Which implementation? Reference-based implementation? Binary search performs fast retrieval? What is the problem? Sorted array-based implementation? A Balanced BST would also be suitable, why?

23 S. Xu 23 Scenario B: Retrieval Example: Word Processor's Thesaurus Frequent retrievals require table implementation that allows efficient searching for items No deletions or insertions are necessary Which implementation? Reference-based implementation?,must traverse whole the LL Binary search performs fast retrieval? What is the problem? Impractical for reference-based Sorted array-based implementation? good A Balanced BST would also be suitable, why? Since the thesaurus does not change

24 S. Xu 24 Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order Example: Computerized Library Catalog Elements are sorted Retrieval is the most frequent operation Both insertion & deletion perform two steps: 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 tableInsert & tableDelete will require amount of time ~ (O(N)) in either array or reference-based implementations More?

25 S. Xu 25 Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order Example: Computerized Library Catalog Elements are sorted Retrieval is the most frequent operation Both insertion & deletion perform two steps: 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 tableInsert & tableDelete 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

26 S. Xu 26 Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order Insertion for sorted linear implementation Array-based Reference-based New item Data Data... Data K+1 items 0i-1i i+1 K... Data New item Data head

27 S. Xu 27 Conclusion Linear implementations Less sophisticated & generally require more time than BST implementation Appropriate for tables containing small number of items BST implementation 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 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

28 S. Xu 28 Comparison of Time Complexity (average) Operation Insertion Deletion Retrieval Traversal Unsorted ArrayO(1)O(n) O(n)O(n) Unsorted reference O(1)O(n) O(n)O(n) Sorted Array O(n)O(n) O(logn)O(n) Sorted reference O(n)O(n) O(n)O(n) BST O(logn)O(logn) O(logn)O(n)

29 29 Review The ADT table is also known as a ______. map queue Heap dictionary

30 30 Review An array based implementation of an ADT is a ______ implementation. vertical linear nonlinear compound

31 31 Review Which of the following is true about a linear implementation of a table? 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

32 32 Review A(n) ______ implementation of a table is nonlinear. list linked list binary search tree array

33 33 Review In an unsorted array-based implementation of a table, a new item is inserted at location ______. items[size] items[size-1] items[size+1] items[size+2]

34 34 Review In an unsorted array-based implementation of the ADT table, the insertion operation is ______. O(1) O(n) O(n 2 ) O(log n)

35 35 Review In an unsorted array-based implementation of the ADT table, the retrieval operation is ______. O(1) O(n) O(n 2 ) O(log n)

36 36 Review The sorted reference-based implementation of the tableDelete operation is ______. O(1) O(log n) O(n) O(n 2 )


Download ppt "COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I."

Similar presentations


Ads by Google