Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS202 - Fundamental Structures of Computer Science II

Similar presentations


Presentation on theme: "CS202 - Fundamental Structures of Computer Science II"— Presentation transcript:

1 CS202 - Fundamental Structures of Computer Science II
lec05-heaps November 20, 2018 ADT Table The ADT table is appropriate for problems that must manage data by value. Some important operations of the ADT table are: Inserting a data item containing the value x. Delete a data item containing the value x. Retrieve a data item containing the value x. Various table implementations are possible for the ADT table. We have to analyze the possible implementations so that we can make an intelligent choice. Some operations are implemented more efficiently in certain implementations. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

2 An ordinary table of cities
lec05-heaps November 20, 2018 An ordinary table of cities 11/20/2018 CS202 - Fundamental Structures of Computer Science II

3 CS202 - Fundamental Structures of Computer Science II
ADT Table Operations Various sets of table operations are possible. Some of them are: Create an empty table. Destroy a table. Determine whether a table is empty. Determine the number of items in the table. Insert a new item into a table. Delete the item with a given search key. Retrieve the item with a given search key. Traverse the table. The client of the ADT table may need a subset of these operations, or require more operations on the table. Are keys in the table are unique? We will assume that keys in our table are unique. But, some other tables allow duplicate keys. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

4 Selecting an Implementation
Since an array or a linked list represents items one after another, these implementations are called linear. There are four categories of linear implementations: Unsorted, array based (an unsorted array) Unsorted, pointer based (a simple linked list) Sorted (by search key), array based (a sorted array) Sorted (by search key), pointer based (a sorted linked list). We have also nonlinear implementations such as binary search tree. Binary search tree implementation offers several advantages over linear implementations. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

5 Sorted Linear Implementations
Array-Based Pointer-Based 11/20/2018 CS202 - Fundamental Structures of Computer Science II

6 Binary Search Tree Implementation
11/20/2018 CS202 - Fundamental Structures of Computer Science II

7 Which Implementation should be used?
Which implementation is appropriate depends on our application. Before we select an implementation, we should answer following questions about our application: What operations are needed? Our application may not need all operations. Some operation is implemented more efficiently in an implementation, and another operation is implemented more efficiently in another implementation. How often is each operation is required? Some applications may require many occurrences of an operation, but other applications may not. For example, some applications may perform many retrievals, but not so many insertions and deletions. On the other hand, other applications may perform many insertions and deletions. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

8 How to Select an Implementation – Scenario A
Let us assume that we have an application: inserts data items into a table. after all data items are inserted traverse this table in no particular order. does not perform any retrieval and deletion operations. Which implementation is appropriate for this application? Keeping the items in a sorted order does not provide any advantage for this application. In fact, it will be more costly for this application.  Unsorted implementation will be more appropriate. Which unsorted implementation (array-based, pointer-based)? Do we know the maximum size of the table? If we know the expected size is close to the maximum size of the table  an array-based will be more appropriate (because pointer-based use extra space for pointers) Otherwise,  a pointer-based will be more appropriate (because too many entries will be empty in the array-based implementation) 11/20/2018 CS202 - Fundamental Structures of Computer Science II

9 Insertion for unsorted linear implementations
Runtime complexity of insertion in an unsorted list: O(1) array based; (b) pointer based 11/20/2018 CS202 - Fundamental Structures of Computer Science II

10 How to Select an Implementation – Scenario B
Let us assume that we have an application: performs many retrievals, but no insertions and deletions (or so few insertions or deletions, so that we ignore their costs). for example, a thesaurus (to look up synonyms of a word) For this application, a sorted implementation is more appropriate. If we use a sorted array, we can use binary search to access data. Since the binary search is not practical with linked lists, sorted linked list implementation will not be appropriate. We can also use a binary search tree (in fact, we can use a balanced binary search tree). If we know the table’s maximum size, a sorted array-based implementation is more appropriate for frequent retrievals. Otherwise, a binary search tree implementation will be more appropriate for frequent retrievals. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

11 How to Select an Implementation – Scenario C
Let us assume that we have an application: performs many retrievals, insertions and deletions. ? Sorted Array Implementation: Retrieval is efficient. But insertion and deletion are not efficient. We have to shift data items.  sorted-array implementation is not appropriate. ? Sorted Linked List Implementation: Retrieval, insertion, and deletion are not efficient (although we do not shift data items)  sorted linked list implementation is not appropriate. ? Binary Search Tree Implementation: Retrieval, insertion, and deletion are efficient.  binary search tree implementation is appropriate. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

12 Insertion for sorted linear implementations
array based; (b) pointer based 11/20/2018 CS202 - Fundamental Structures of Computer Science II

13 CS202 - Fundamental Structures of Computer Science II
Which Implementation? Despite difficulties, linear implementations of a table can be appropriate. Linear implementations are easy to understand, easy to implement. Linear implementations can be appropriate for small tables. For large tables, if there are few deletions and retrievals, linear implementations may be appropriate. In general, a binary search tree implementation is a better choice. Worst case: O(n) for most table operations Average case: O(log2n) for most table operations Balanced binary search tree increases the efficiency of the ADT table operations. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

14 CS202 - Fundamental Structures of Computer Science II
The average-case order of the ADT table operations for various implementations 11/20/2018 CS202 - Fundamental Structures of Computer Science II

15 Sorted Array-Based Implementation
// Header file TableA.h for the ADT table. // Sorted array-based implementation. // Assumption: A table contains at most one item with a // given search key at any time. #include "KeyedItem.h" // definition of KeyedItem and KeyType #include "TableException.h" const int MAX_TABLE = maximum-size-of-table; typedef KeyedItem TableItemType; typedef void (*FunctionType)(TableItemType& anItem); class Table { public: Table(); // default constructor // copy constructor and destructor are supplied by the compiler 11/20/2018 CS202 - Fundamental Structures of Computer Science II

16 Sorted Array-Based Implementation (cont.)
// Table operations: virtual bool tableIsEmpty() const; // Determines whether a table is empty. virtual int tableLength() const; // Determines the length of a table. virtual void tableInsert(const TableItemType& newItem)throw (TableException); // Inserts an item into a table in its proper sorted // order according to the item's search key. virtual void tableDelete(KeyType searchKey)throw (TableException); // Deletes an item with a given search key from a table. virtual void tableRetrieve(KeyType searchKey, TableItemType& tableItem) const throw (TableException); // Retrieves an item with a given search key from a table. virtual void traverseTable(FunctionType visit); // Traverses a table in sorted search-key order, calling // function visit() once for each item. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

17 Sorted Array-Based Implementation (cont.)
protected: void setSize(int newSize); // Sets the private data member size to newSize. void setItem(const TableItemType& newItem, int index); // Sets items[index] to newItem. int position(KeyType searchKey) const; // Finds the position of a table item or its insertion point. private: TableItemType items[MAX_TABLE]; // table items int size; // table size int keyIndex(int first, int last, KeyType searchKey) const; // Searches a particular portion of the private array // items for a given search key by using a binary search. }; // end Table class 11/20/2018 CS202 - Fundamental Structures of Computer Science II

18 CS202 - Fundamental Structures of Computer Science II
tableInsert void Table::tableInsert(const TableItemType& newItem) { // Note: Insertion is unsuccessful if the table is full, // that is, if the table already contains MAX_TABLE items. // Calls: position. if (size == MAX_TABLE) throw TableException("TableException: Table full"); // there is room to insert; // locate the position where newItem belongs int spot = position(newItem.getKey()); // shift up to make room for the new item for (int index = size-1; index >= spot; --index) items[index+1] = items[index]; // make the insertion items[spot] = newItem; ++size; } // end tableInsert 11/20/2018 CS202 - Fundamental Structures of Computer Science II

19 CS202 - Fundamental Structures of Computer Science II
tableDelete void Table::tableDelete(KeyType searchKey) { // Calls: position. // locate the position where searchKey exists/belongs int spot = position(searchKey); // is searchKey present in the table? if ((spot > size) || (items[spot].getKey() != searchKey)) // searchKey not in table throw TableException( "TableException: Item not found on delete"); else { // searchKey in table --size; // delete the item // shift down to fill the gap for (int index = spot; index < size; ++index) items[index] = items[index+1]; } // end if } // end tableDelete 11/20/2018 CS202 - Fundamental Structures of Computer Science II

20 CS202 - Fundamental Structures of Computer Science II
tableRetrieve void Table::tableRetrieve(KeyType searchKey, TableItemType& tableItem) const // Calls: position. { // locate the position where searchKey exists/belongs int spot = position(searchKey); // is searchKey present in table? if ((spot > size) || (items[spot].getKey() != searchKey)) // searchKey not in table throw TableException( "TableException: Item not found on retrieve"); else tableItem = items[spot]; // item present; retrieve it } // end tableRetrieve 11/20/2018 CS202 - Fundamental Structures of Computer Science II

21 CS202 - Fundamental Structures of Computer Science II
traverseTable void Table::traverseTable(FunctionType visit) { for (int index = 0; index < size; ++index) visit(items[index]); } // end traverseTable 11/20/2018 CS202 - Fundamental Structures of Computer Science II

22 Binary Search Tree Implementation – TableB.h
#include "BST.h" // binary search tree operations #include "TableException.h" typedef TreeItemType TableItemType; class Table { public: Table(); // default constructor // copy constructor and destructor are supplied by the compiler // Table operations: virtual bool tableIsEmpty() const; virtual int tableLength() const; virtual void tableInsert(const TableItemType& newItem) throw(TableException); virtual void tableDelete(KeyType searchKey) throw(TableException); virtual void tableRetrieve(KeyType searchKey, TableItemType& tableItem) const throw(TableException); virtual void traverseTable(FunctionType visit); protected: void setSize(int newSize); private: BinarySearchTree bst; // binary search tree that contains the table’s items int size; // number of items in the table }; // end Table class 11/20/2018 CS202 - Fundamental Structures of Computer Science II

23 Binary Search Tree Implementation – TableB.cpp
#include "TableB.h" // header file void Table::tableInsert(const TableItemType& newItem) { try bst.searchTreeInsert(newItem); ++size; } // end try catch (TreeException e) { throw TableException( "TableException: Cannot insert item"); } // end catch } // end tableInsert 11/20/2018 CS202 - Fundamental Structures of Computer Science II

24 CS202 - Fundamental Structures of Computer Science II
tableDelete void Table::tableDelete(KeyType searchKey) { try { bst.searchTreeDelete(searchKey); } // end try catch (TreeException e) { throw TableException( "TableException: Item not found on delete"); } // end catch } // end tableDelete 11/20/2018 CS202 - Fundamental Structures of Computer Science II

25 tableRetrieve & traverseTable
void Table::tableRetrieve(KeyType searchKey, TableItemType& tableItem) const { try { bst.searchTreeRetrieve(searchKey, tableItem); } // end try catch (TreeException e) { throw TableException( "TableException: Item not found on retrieve"); } // end catch } // end tableRetrieve void Table::traverseTable(FunctionType visit) { bst.inorderTraverse(visit); } // end traverseTable // End of implementation file. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

26 CS202 - Fundamental Structures of Computer Science II
The ADT Priority Queue Priority queue is a variation of the table. Each data item in a priority queue has a priority value. We insert an item with a priority value into its proper position in the priority queue. Deletion in the priority queue is not same as the deletion in the table. We delete operation deletes the item with the highest priority. Using a priority queue we prioritize a list of tasks: Job scheduling 11/20/2018 CS202 - Fundamental Structures of Computer Science II

27 ADT Priority Queue Operations
createPriorityQueue() – create an empty priority queue. destroyPriorityQueue – destroys a priority queue. isEmpty – determines whether a priority queue is empty or not. insert – Inserts a new item (with a priority value) into a priority queue. delete – retrieves the item in a priority queue with the highest priority value, and deletes that item from the priority queue. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

28 Some implementations of the ADT priority queue
(a) array based; (b) pointer based; (c) binary search tree 11/20/2018 CS202 - Fundamental Structures of Computer Science II

29 Implementations – Analysis
None of these implementations of the priority queue is not efficient enough. Array-Based – Insertion will be O(n) Pointer-Based – BST Implementation – Insertion is O(log2n) in average, but O(n) in the worst case. We need a balanced BST so that we can get better performance ( O(logn) in the worst case)  Heap 11/20/2018 CS202 - Fundamental Structures of Computer Science II

30 CS202 - Fundamental Structures of Computer Science II
Heaps Definition: A heap is a complete binary tree such that It is empty, or Its root contains a search key greater than or equal to the search key in each of its children, and each of its children is also a heap. In this definition, since the root contains the item with the largest search key, heap in this definition is also known as maxheap. On the other hand, a heap which places the smallest search key in its root is know as minheap. We will talk about maxheap as heap in the rest of our discussions. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

31 Difference between Heap and BST
A heap is NOT a binary search tree. Differences between heap and BST: While we can see a binary search tree as sorted, but a heap is ordered in much weaker sense. The order of the heap (although it is not sorted) is sufficient for the efficient performance of the priority queue operations. While binary search trees come in many different shapes, heaps are always complete binary trees. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

32 CS202 - Fundamental Structures of Computer Science II
Heap Examples 50 40 45 30 35 33 HEAPS 42 40 45 50 30 35 NOT HEAPS 11/20/2018 CS202 - Fundamental Structures of Computer Science II

33 An Array-Based Implementation of a Heap
An array and an integer counter are the data members for an array-based implementation of a heap. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

34 CS202 - Fundamental Structures of Computer Science II
Major Heap Operations Two major heap operations are insertion and deletion. Insertion Inserts a new item into a heap. After the insertion, the heap must satisfy heap properties. Deletion Retrieves and deletes the root of the heap. After the deletion, the heap must satisfy heap properties. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

35 Heap Delete – First Step
First step of heapDelete is to retrieve and delete the root. We create two disjoint heaps. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

36 Heap Delete – Second Step
Move to the last item into the root. The resulting structure may not be heap, and it is called as semiheap. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

37 CS202 - Fundamental Structures of Computer Science II
Heap Delete – Last Step The last step of heapDelete transforms the semiheap into a heap. First Step Second Step Last Step 11/20/2018 CS202 - Fundamental Structures of Computer Science II

38 CS202 - Fundamental Structures of Computer Science II
Heap Delete Recursive calls to heapRebuild 11/20/2018 CS202 - Fundamental Structures of Computer Science II

39 CS202 - Fundamental Structures of Computer Science II
Heap Delete – Analysis Since the height of a complete binary tree with n nodes is always  log2(n+1)  heapDelete is O(log2n) 11/20/2018 CS202 - Fundamental Structures of Computer Science II

40 CS202 - Fundamental Structures of Computer Science II
Heap Insert A new item is inserted at the bottom of the tree, and it trickles up to its proper place 11/20/2018 CS202 - Fundamental Structures of Computer Science II

41 CS202 - Fundamental Structures of Computer Science II
Heap Insert – Analysis Since the height of a complete binary tree with n nodes is always  log2(n+1)  heapInsert is O(log2n) 11/20/2018 CS202 - Fundamental Structures of Computer Science II

42 CS202 - Fundamental Structures of Computer Science II
Heap.h const int MAX_HEAP = maximum-size-of-heap; #include "KeyedItem.h" // definition of KeyedItem typedef KeyedItem HeapItemType; class Heap { public: Heap(); // default constructor // copy constructor and destructor are supplied by the compiler // Heap operations: virtual bool heapIsEmpty() const; // Determines whether a heap is empty. virtual void heapInsert(const HeapItemType& newItem) throw(HeapException); // Inserts an item into a heap. virtual void heapDelete(HeapItemType& rootItem) throw(HeapException); // Retrieves and deletes the item in the root of a heap. // This item has the largest search key in the heap. protected: void heapRebuild(int root); // Converts the semiheap rooted at index root into a heap. private: HeapItemType items[MAX_HEAP]; // array of heap items int size; // number of heap items }; // end class 11/20/2018 CS202 - Fundamental Structures of Computer Science II

43 CS202 - Fundamental Structures of Computer Science II
Heap.cpp // ********************************************************* // Implementation file Heap.cpp for the ADT heap. #include "Heap.h" // header file for class Heap Heap::Heap() : size(0) { } // end default constructor bool Heap::heapIsEmpty() const return bool(size == 0); } // end heapIsEmpty 11/20/2018 CS202 - Fundamental Structures of Computer Science II

44 CS202 - Fundamental Structures of Computer Science II
heapInsert void Heap::heapInsert(const HeapItemType& newItem) { // Method: Inserts the new item after the last item in the heap and // trickles it up to its proper position. // The heap is full when it contains MAX_HEAP items. if (size > MAX_HEAP) throw HeapException("HeapException: Heap full"); // place the new item at the end of the heap items[size] = newItem; // trickle new item up to its proper position int place = size; int parent = (place - 1)/2; while ( (parent >= 0) && (items[place].getKey() > items[parent].getKey()) ) { // swap items[place] and items[parent] HeapItemType temp = items[parent]; items[parent] = items[place]; items[place] = temp; place = parent; parent = (place - 1)/2; } // end while ++size; } // end heapInsert 11/20/2018 CS202 - Fundamental Structures of Computer Science II

45 CS202 - Fundamental Structures of Computer Science II
heapDelete void Heap::heapDelete(HeapItemType& rootItem) // Method: Swaps the last item in the heap with the root // and trickles it down to its proper position. { if (heapIsEmpty()) throw HeapException("HeapException: Heap empty"); else { rootItem = items[0]; items[0] = items[--size]; heapRebuild(0); } // end if } // end heapDelete 11/20/2018 CS202 - Fundamental Structures of Computer Science II

46 CS202 - Fundamental Structures of Computer Science II
heapRebuild void Heap::heapRebuild(int root) { // if the root is not a leaf and the root's search key // is less than the larger of the search keys in the root's children int child = 2 * root + 1; // index of root's left child, if any if ( child < size ) { // root is not a leaf, so it has a left child at child int rightChild = child + 1; // index of right child, if any // if root has a right child, find larger child if ( (rightChild < size) && (items[rightChild].getKey() > items[child].getKey()) ) child = rightChild; // index of larger child // if the root's value is smaller than the // value in the larger child, swap values if ( items[root].getKey() < items[child].getKey() ) { HeapItemType temp = items[root]; items[root] = items[child]; items[child] = temp; // transform the new subtree into a heap heapRebuild(child); } // end if // if root is a leaf, do nothing } // end heapRebuild 11/20/2018 CS202 - Fundamental Structures of Computer Science II

47 Heap Implementation of Priority Queue
Since the heap operations and the priority queue operations are same, the heap implementation of the priority queue is straightforward. Both insertion and deletion operations of the priority queue will be O(log2n), when we use the heap. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

48 CS202 - Fundamental Structures of Computer Science II
PQ.h // Header file PQ.h for the ADT priority queue. // Heap implementation. #include "Heap.h" // ADT heap operations typedef HeapItemType PQueueItemType; class PriorityQueue { public: // default constructor, copy constructor, and // destructor are supplied by the compiler // priority-queue operations: virtual bool pqIsEmpty() const; virtual void pqInsert(const PQueueItemType& newItem) throw (PQueueException); virtual void pqDelete(PQueueItemType& PQueueItemType) private: Heap h; }; // end PriorityQueue class 11/20/2018 CS202 - Fundamental Structures of Computer Science II

49 CS202 - Fundamental Structures of Computer Science II
PQ.cpp // Implementation file PQ.cpp for the ADT priority queue. // A heap represents the priority queue. #include "PQ.h" // header file for priority queue bool PriorityQueue::pqIsEmpty() const { return h.heapIsEmpty(); } // end pqIsEmpty void PriorityQueue::pqInsert(const PQueueItemType& newItem) { try { h.heapInsert(newItem); } // end try catch (HeapException e) { throw PQueueException( "PQueueException: Priority queue full"); } // end catch } // end pqInsert 11/20/2018 CS202 - Fundamental Structures of Computer Science II

50 CS202 - Fundamental Structures of Computer Science II
pqDelete void PriorityQueue::pqDelete(PQueueItemType& priorityItem) { try h.heapDelete(priorityItem); } // end try catch (HeapException e) { throw PQueueException( "PQueueException: Priority queue empty"); } // end catch } // end pqDelete 11/20/2018 CS202 - Fundamental Structures of Computer Science II

51 CS202 - Fundamental Structures of Computer Science II
Heapsort We can use a heap to sort an array: Create a heap from the given initial array with n items. Swap the root of the heap with the last element in the heap. Now, we have a semiheap with n-1 items, and a sorted array with one item. Using heapRebuild convert this semiheap into a heap. Now we will have a heap with n-1 items. Repeat the steps 2-4 as long as the number of items in the heap is more than 1. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

52 CS202 - Fundamental Structures of Computer Science II
Heapsort (cont.) The initial contents of anArray; anArray corresponding binary tree 11/20/2018 CS202 - Fundamental Structures of Computer Science II

53 Heapsort – Building a Heap from an array
for (index = (n/2) – 1 ; index >= 0 ; index--) { // Invariant: the tree rooted at index // is a semiheap heapRebuild(anArray, index, n) // Assertion: the tree rooted at index // is a heap. } 11/20/2018 CS202 - Fundamental Structures of Computer Science II

54 Heapsort – Building a Heap from an array
11/20/2018 CS202 - Fundamental Structures of Computer Science II

55 CS202 - Fundamental Structures of Computer Science II
Heapsort Heapsort partitions an array into two regions Each step of the algorithm moves an item from the Heap region to Sorted region. The invariant of the heapsort algorithm is After step k, the Sorted region contains the k largest value in anArray, and they are in sorted order. The items in the Heap region form a heap. 11/20/2018 CS202 - Fundamental Structures of Computer Science II

56 CS202 - Fundamental Structures of Computer Science II
Heapsort Algorithm heapSort(inout anArray:ArrayType, in n:integer) { // sorts anArray[0..n-1] // build initial heap for (index = (n/2) – 1 ; index >= 0 ; index--) { // Invariant: the tree rooted at index is a semiheap heapRebuild(anArray, index, n) // Assertion: the tree rooted at index is a heap. } for (last = n-1 ; last > 0 ; last--) { // Invariant: anArray[0..last] is a heap, anArray[last+1..n-1] is sorted and // contains the largest items of anArray. // swap the largest item (anArray[0]) and the last item in the Heap region. swap anArray[0] and anArray[last] // make the Heap region a heap again heapRebuild(anArray, 0, last) 11/20/2018 CS202 - Fundamental Structures of Computer Science II

57 CS202 - Fundamental Structures of Computer Science II
Heapsort – Trace 11/20/2018 CS202 - Fundamental Structures of Computer Science II

58 CS202 - Fundamental Structures of Computer Science II
Heapsort – Trace 11/20/2018 CS202 - Fundamental Structures of Computer Science II

59 CS202 - Fundamental Structures of Computer Science II
Heapsort – Analysis Heapsort is O(n * log n) at average case O(n * log n) at worst case Heapsort is slower than quicksort at average case, but its worst case is also O(n * log n). 11/20/2018 CS202 - Fundamental Structures of Computer Science II


Download ppt "CS202 - Fundamental Structures of Computer Science II"

Similar presentations


Ads by Google