1 / 36

COSC2007 Data Structures II

COSC2007 Data Structures II. Chapter 12 Tables & Priority Queues I. Topics. Tables Operations Implementation. ADT Table. Table example: City population City (key) Country Population (sorted) Athens Greece 2,500,000 Barcelona Spain 1,800,000 Cairo Egypt 16,500,000

tilliet
Download Presentation

COSC2007 Data Structures II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Topics • Tables • Operations • Implementation S. Xu

  3. ADT Table • Table example: • City population City (key) Country Population (sorted) Athens Greece 2,500,000 Barcelona Spain 1,800,000 Cairo Egypt 16,500,000 London England 9,400,000 New York U.S.A 7,300,000 Paris France 2,200,000 Toronto Canada 3,200,000 Venice Italy 300,000 • Questions (if we use BST or other ADT to store) • What is the population of London? • Which city is in Italy? S. Xu

  4. ADT Table • Table example: • City population City (key) Country Population (sorted) Athens Greece 2,500,000 Barcelona Spain 1,800,000 Cairo Egypt 16,500,000 London England 9,400,000 New York U.S.A 7,300,000 Paris France 2,200,000 Toronto Canada 3,200,000 Venice Italy 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 S. Xu

  5. ADT Table Dictionary Member Record key student name hw1 123 Stan Smith 49 ... 124 Sue Margolin 56 ... 125 Billie King 34 ... ... 167 Roy Miller 39 ... 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 S. Xu

  7. ADT Table 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 S. Xu

  9. ADT Table • Table example: • City population City (key) Country Population (sorted) Athens Greece 2,500,000 Barcelona Spain 1,800,000 Cairo Egypt 16,500,000 London England 9,400,000 New York U.S.A 7,300,000 Paris France 2,200,000 Toronto Canada 3,200,000 Venice Italy 300,000 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? 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 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( )) 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 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? S. Xu

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

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

  17. New York 9 Toronto Cairo size Venice London Paris Barcelona Athens Rome ADT Table • Implementation • Using ADT BST S. Xu

  18. ADT Table • How to choose an implementation? 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 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? S. Xu

  21. K+1 Athens Barcelona . . . Venice . . . New item K-1 K K+1 ... K+1 • Athens Barcelona Venice New item Scenario A: Insert & Traverse(no Particular Order) • Insertion for unsorted linear implementation • Array-based • Reference-based 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? 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 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? 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 S. Xu

  26. items 0 i-1 i i+1 K . . . K+1 Data Data . . . Data • Data Data Data Data head New item Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order • Insertion for sorted linear implementation • Array-based • Reference-based New item 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 Log2 (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 S. Xu

  28. Comparison of Time Complexity (average) Operation Insertion Deletion Retrieval Traversal Unsorted Array O(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) S. Xu

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

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

  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. Review • A(n) ______ implementation of a table is nonlinear. • list • linked list • binary search tree • array

  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. Review • In an unsorted array-based implementation of the ADT table, the insertion operation is ______. • O(1) • O(n) • O(n2) • O(log n)

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

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

More Related