Tables, Priority Queues, Heaps Table ADT � A table in generic terms has M columns and N rows � Table ADT � each row contains a separate record � purpose, implementations � each column contains a different component, or field, of the same record � Priority Queue ADT � Each table, or set of data, is also generally sorted, or accessed, by a key record component � variation on Table ADT � a single set of data can be organized into several different � Heaps tables, sorted according to different keys � Another common terms is a dictionary, whose entries � purpose, implementation are records, inserted and accessed according to a key � heapsort value � key may be a field in the record or not � may also be used as frontends for data base access EECS 268 Programming II 1 EECS 268 Programming II 2 ADT Table � Example ADT Table � Operations � A simple and obvious set of operations can be used � The ADT table, or dictionary for a wide range of program activities � Uses a search key to identify its items � Create and Destroy Table instance � Its items are records that contain several pieces of � Determine the number of items including zero data � Insert an item in a table using a key value � Delete an item with a given key value � Retrieve an item with a given key value � Retrieve the items in the table (sorted or unsorted) � Entries with identical key values maybe forbidden, but can be handled with a little imagination 3 EECS 268 Programming II 4
The ADT Table The ADT Table � void tableInsert(ItemType& item) : � Our table assumes distinct search keys � store item under its key � boolean tableDelete(KeyType � other tables could allow duplicate search keys key_value) : � The traverseTable operation visits table � delete item with key == key_value, if present items in a specified order � ItemType* tableRetrieve(KeyType key_value) : � one common order is by sorted search key � return pointer to item with � a client-defined visit function is supplied as an key==key_value � void traverseTable(Functor visitor): argument to the traversal � Functor: a function-object, much like a � called once for each item in the table fn pointer � visitor is executed for each node in table 5 EECS 268 Programming II 6 Selecting an Implementation Selecting an Implementation � Linear implementations: Four categories � Nonlinear implementations � Unsorted: array based or pointer based � Binary search tree � Sorted (by search key): array based or pointer implementation based � Offers several advantages over linear implementations Figure 11-4 The data members for a binary search tree implementation of the Figure 11-3 The data members for two sorted linear implementations of the ADT table for the data ADT table for the data in in Figure 11-1: (a) array based; (b) pointer based Figure 11-1 EECS 268 Programming II 7 8
Comparing Linear Implementations Selecting an Implementation � Unsorted array-based implementation � The requirements of a particular application � Insertion is made efficiently after the last table item in an array influence the selection of an implementation � Deletion usually requires shifting data � Questions to be considered about an application � Retrieval requires a sequential search before choosing an implementation � What operations are needed? � How often is each operation required? � Are frequently used operations efficient given a particular implementation? Figure 11-5a Insertion for unsorted linear implementations: array based 10 EECS 268 Programming II 9 Comparing Linear Implementations Comparing Linear Implementations � Unsorted pointer-based implementation � No data shifts � Sorted array-based implementation � Insertion is made efficiently at the beginning of a linked list � Both insertions and deletions require shifting � Deletion requires a sequential search data � Retrieval requires a sequential search � Retrieval can use an efficient binary search Figure 11-6a Insertion for sorted linear implementations: array based Figure 11-5b Insertion for unsorted linear implementations: pointer based 11 12
Comparing Linear Implementations Selecting an Implementation � Linear � Sorted pointer-based implementation � Easy to understand conceptually � No data shifts � May be appropriate for small tables or unsorted � Insertions, deletions, and retrievals each tables with few deletions require a sequential search � Nonlinear � Is usually a better choice than a linear implementation � A balanced binary search tree � Increases the efficiency of the table operations Figure 11-6b Insertion for sorted linear implementations: pointer based 13 EECS 268 Programming II 14 Selecting an Implementation for a Selecting an Implementation Particular Application � Frequent insertions and infrequent traversals in no particular order � Unsorted linear implementation � Frequent retrievals � Sorted array-based implementation � Binary search � Balanced binary search tree � Frequent retrievals, insertions, deletions, traversals Figure 11-7 The average-case order of the ADT table operations for various implementations � Binary search tree (preferably balanced) EECS 268 Programming II 15 EECS 268 Programming II 16
Generalized Data Set Management Keyed Base Class #include <string> using namespace std; � Problem of managing a set of data items occurs � Create base class for typedef string KeyType; many times in many contexts associating key with an class KeyedItem � arbitrary set of data represented by an arbitrary key arbitrary item { value within the set � Maintains key outside the public: � Strict separation of the set of data from the key item fields KeyedItem() {} helps with abstraction and generalization KeyedItem(const KeyType& � Rows of Table are derived keyValue) � Data Set classes of this class : searchKey(keyValue){} � class or structure defined in application terms � Inserting item in Table KeyType getKey() const { return searchKey; � Container class creates instance of derived } class and stores it under � STL terminology private: key � holds key and data set items KeyType searchKey; }; EECS 268 Programming II 17 EECS 268 Programming II 18 Table Item Class A Sorted Array-Based Implementation of the ADT Table class City : public KeyedItem { public: � Create table of cities � Default constructor and virtual destructor City() : KeyedItem() {} indexed by city name City(const string& name, � Copy constructor supplied by the compiler const string& ctry, � Might create struct for const int& num) � ������������������������������������������������ each city : KeyedItem(name), country(ctry), pop(num) {} � Public methods are virtual � name, popu., country � Or, might derive this string cityName() const; � Protected methods: setSize, setItem, and int getPopulation() const; class from KeyedItem position void setPopulation(int newPop); � Delegates chosen key to private: // city's name is search-key value base class storage string country; int pop; }; EECS 268 Programming II 19 EECS 268 Programming II 20
A Binary Search Tree Implementation Priority Queue of the ADT Table � Binary Search Tree is an excellent data structure, but � Reuses BinarySearchTree not always � An instance is a private data member � simple in concept and implementation � BST supports many useful operations well � Default constructor and virtual destructor � insert, delete, deleteMax, deleteMin, search, searchMax, searchMin, sort � Copy constructor supplied by the compiler � efficient average case behavior T(n) = O(log n) � Public methods are virtual � However, BST is not good in all respects for all purposes � Protected method: setSize � brittle with respect to balance � worst case T(n) = O(n) � Balanced Trees are possible but more complex EECS 268 Programming II 21 EECS 268 Programming II 22 Priority Queue Priority Queue � Priority Queue semantics are useful when items � The deletion operation for a priority queue is are added to the set in arbitrary order, but are different from the one for a table removed in either ascending or descending � ������������������������������������������� priority order � item removed is the one having the highest � priority can have a flexible definition priority value � any property of the set elements imposing a total order on the set members � Priority queues do not have retrieval and � If only a partial order is imposed (multiple items with traversal operations equal priority) a secondary tiebreaking rule can be used to create a total order EECS 268 Programming II 23 EECS 268 Programming II 24
Recommend
More recommend