This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Tables
• Appropriate for problems that must manage data by value. • Some important opera9ons of tables: An ordinary table of ci2es – Inser9ng 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 implementa9ons are possible. – We have to analyze the possible implementa9ons so that we can make an intelligent choice. • Some opera9ons are implemented more eﬃciently in certain implementa9ons. 3/25/11 CS202  Fundamental Structures of Computer Science II 1 Table Opera2ons
• Some of the table opera9ons are possible:
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 may need a subset of these opera9ons or require more • Are keys in the table are unique? – We will assume that keys in our tables are unique. – But, some other tables allow duplicate keys. 3/25/11 CS202  Fundamental Structures of Computer Science II 2 Selec2ng an Implementa2on
• Since an array or a linked list represents items one aMer another, these implementa9ons are called linear. • There are four categories of linear implementa9ons: –
–
–
– 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 implementa9ons such as binary search trees. – Binary search tree implementa9on oﬀers several advantages over linear implementa9ons. 3/25/11 CS202  Fundamental Structures of Computer Science II 3 Sorted Linear Implementa2ons
Array
based implementa2on Pointer
based implementa2on 3/25/11 CS202  Fundamental Structures of Computer Science II 4 A Nonlinear Implementa2on
Binary search tree implementa2on 3/25/11 CS202  Fundamental Structures of Computer Science II 5 Which Implementa2on?
• It depends on our applica9on. • Answer the following ques9ons before selec9ng an implementa9on. 1. What opera2ons are needed? • Our applica9on may not need all opera9ons. • Some opera9ons can be implemented more eﬃciently in one implementa9on, and some others in another implementa9on. 2. How oLen is each opera2on required? • Some applica9ons may require many occurrences of an opera9on, but other applica9ons may not. – For example, some applica9ons may perform many retrievals, but not so many inser9ons and dele9ons. On the other hand, other applica9ons may perform many inser9ons and dele9ons. 3/25/11 CS202  Fundamental Structures of Computer Science II 6 How to Select an Implementa2on – Scenario A
• Let us assume that we have an applica9on: – Inserts data items into a table. – AMer all data items are inserted, traverses this table in no par9cular order. – Does not perform any retrieval and dele9on opera9ons. • Which implementa9on is appropriate for this applica9on? – Keeping the items in a sorted order provides no advantage for this applica9on. • In fact, it will be more costly for this applica9on. Unsorted implementa/on is more appropriate. 3/25/11 CS202  Fundamental Structures of Computer Science II 7 How to Select an Implementa2on – Scenario A
• Which unsorted implementa9on (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 implementa/on is more appropriate (because a pointer
based implementa/on uses extra space for pointers) • Otherwise, a pointer
based implementa/on is more appropriate (because too many entries will be empty in an array
based implementa/on) 3/25/11 CS202  Fundamental Structures of Computer Science II Time complexity of inser2on in an unsorted list: O(1) 8 How to Select an Implementa2on – Scenario B
• Let us assume that we have an applica9on: – Performs many retrievals, but few inser9ons and dele9ons • E.g., a thesaurus (to look up synonyms of a word) • For this applica9on, a sorted implementa/on is more appropriate – We can use binary search to access data, if we have sorted data. – A sorted linked
list implementa9on is not appropriate since binary search is not prac9cal with linked lists. • If we know the maximum size of the table a sorted array
based implementa/on is more appropriate for frequent retrievals.
• Otherwise a binary search tree implementa/on is more appropriate for frequent retrievals. (in fact, balanced binary search trees will be used) 3/25/11 CS202  Fundamental Structures of Computer Science II 9 How to Select an Implementa2on – Scenario C
• Let us assume that we have an applica9on: – Performs many retrievals as well as many inser9ons and dele9ons. ? Sorted Array Implementa9on • Retrievals are eﬃcient. • But inser9ons and dele9ons are not eﬃcient. a sorted array
based implementa/on is not appropriate for this applica/on. ? Sorted Linked List Implementa9on • Retrievals, inser9ons, and dele9ons are not eﬃcient. a sorted linked
list implementa/on is not appropriate for this applica/on. ? Binary Search Tree Implementa9on • Retrieval, inser9on, and dele9on are eﬃcient in the average case. a binary search tree implementa/on is appropriate for this applica/on. (provided that the height of the BST is O(logn)) 3/25/11 CS202  Fundamental Structures of Computer Science II 10 Which Implementa2on?
• Linear implementa9ons of a table can be appropriate despite its diﬃcul9es. – Linear implementa9ons are easy to understand, easy to implement. – For small tables, linear implementa9ons can be appropriate. – For large tables, linear implementa9ons may s9ll be appropriate (e.g., for the case that has only inser9ons to an unsorted table
Scenario A) • In general, a binary search tree implementa9on is a beXer choice. – Worst case: O(n) – Average case: O(log2n) for most table opera9ons for most table opera9ons • Balanced binary search trees increase the eﬃciency. 3/25/11 CS202  Fundamental Structures of Computer Science II 11 Which Implementa2on? The average
case 2me complexi2es of the table opera2ons 3/25/11 CS202  Fundamental Structures of Computer Science II 12 Binary Search Tree Implementa2on – TableB.h
#include "BST.h"
!
!
typedef TreeItemType TableItemType;! !// Binary search tree operations! class Table {!
public:!
!Table();
!// default constructor!
!
!
!// copy constructor and destructor are supplied by the compiler!
!bool tableIsEmpty() const;!
!int tableLength() const;!
!void traverseTable(FunctionType visit);!
!void tableInsert(const TableItemType& newItem) throw(TableException);!
!void tableDelete(KeyType searchKey) throw(TableException);!
!void tableRetrieve(KeyType searchKey, TableItemType& tableItem) const !
!
!
!
!
!
!
!
!throw(TableException);!
protected:!
!void setSize(int newSize);!
private:!
!BinarySearchTree bst;
!int size;
}!
3/25/11 !
! !// BST that contains the table’s items!
!// Number of items in the table! CS202  Fundamental Structures of Computer Science II 13 Binary Search Tree Implementa2on – tableInsert
#include "TableB.h" // header file!
void Table::tableInsert(const TableItemType& newItem) throw(TableException) {!
!try {!
!
!bst.searchTreeInsert(newItem);!
!
!++size;!
!}!
!catch (TreeException e){!
!
!throw TableException("Cannot insert item");!
!}!
}! 3/25/11 CS202  Fundamental Structures of Computer Science II 14 The Priority Queue
Priority queue is a varia9on of the table. • Each data item in a priority queue has a priority value. • Using a priority queue we priori9ze a list of tasks: – Job scheduling Major opera9ons: • We insert an item with a priority value into its proper posi9on in the priority queue. • Dele9on in the priority queue is not the same as the dele9on in the table. We delete the item with the highest priority.
3/25/11 CS202  Fundamental Structures of Computer Science II 15 Priority Queue Opera2ons
– determines whether a priority queue is empty or not. 3/25/11 – destroys a priority queue. – creates an empty priority queue. – inserts a new item (with a priority value) into a priority queue. – retrieves the item in a priority queue with the highest priority value, and deletes that item from the priority queue. CS202  Fundamental Structures of Computer Science II 16 Which Implementa2ons?
1. Array
based implementa9on – Inser9on will be O(n) 2. Linked
list implementa9on – Inser9on will be O(n) 3. BST implementa9on – Inser9on is O(log2n) in average but O(n) in the worst case. We need a balanced BST so that we can get beTer performance [O(logn) in the worst case] HEAP 3/25/11 CS202  Fundamental Structures of Computer Science II 17 Heaps
Deﬁni/on: 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. • Since the root contains the item with the largest search key, heap in this deﬁni9on 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. 3/25/11 CS202  Fundamental Structures of Computer Science II 18 Diﬀerences between a Heap and a BST
• A heap is NOT a binary search tree. 1. A BST can be seen as sorted, but a heap is ordered in much weaker sense. • Although it is not sorted, the order of a heap is suﬃcient for the eﬃcient implementa9on of priority queue opera9ons. 2. A BST has diﬀerent shapes, but a heap is always complete binary tree. 50 HEAPS 50 50 40 40 40 45 30 NOT HEAPS 50 42 40 40 CS202  Fundamental Structures of Computer Science II 33 50 45 40 30 3/25/11 35 45 35 19 An Array
Based Implementa2on of a Heap
An array and an integer counter are the data members for an array
based implementa9on of a heap. 3/25/11 CS202  Fundamental Structures of Computer Science II 20 Major Heap Opera2ons
• Two major heap opera9ons are inser9on and dele9on. Inser/on – Inserts a new item into a heap. – AMer the inser9on, the heap must sa9sfy the heap proper9es. Dele/on – Retrieves and deletes the root of the heap. – AMer the dele9on, the heap must sa9sfy the heap proper9es. 3/25/11 CS202  Fundamental Structures of Computer Science II 21 Heap Delete – First Step
• The ﬁrst step of heapDelete is to retrieve and delete the root. • This creates two disjoint heaps. 3/25/11 CS202  Fundamental Structures of Computer Science II 22 Heap Delete – Second Step
• Move the last item into the root. • The resul9ng structure may not be heap; it is called as semiheap. 3/25/11 CS202  Fundamental Structures of Computer Science II 23 Heap Delete – Last Step
The last step of heapDelete transforms the semiheap into a heap. Recursive calls to heapRebuild 3/25/11 CS202  Fundamental Structures of Computer Science II 24 Heap Delete
ANALYSIS • Since the height of a complete binary tree with
n nodes is always Ⱥ log2(n+1)Ⱥ heapDelete is O(log2n) 3/25/11 CS202  Fundamental Structures of Computer Science II 25 Heap Insert
A new item is inserted at the boXom of the tree, and it trickles up to its proper place ANALYSIS • Since the height of a complete binary tree with
n nodes is always Ⱥ log2(n+1)Ⱥ heapInsert is O(log2n) 3/25/11 CS202  Fundamental Structures of Computer Science II 26 Heap Implementa2on
const int MAX_HEAP = maximumsizeofheap;!
#include "KeyedItem.h" !
!
!// definition of KeyedItem!
typedef KeyedItem HeapItemType;!
class Heap {!
public:!
!Heap();
!
!
!
!// default constructor!
!
!
!
!
!
!// copy constructor and destructor are !
!
!
!
!
!
!// supplied by the compiler!
!bool heapIsEmpty() const;!
!void heapInsert(const HeapItemType& newItem) throw(HeapException);!
!void heapDelete(HeapItemType& rootItem) throw(HeapException);!
protected:!
!void heapRebuild(int root); !
!
!
!
!
!
private:!
!HeapItemType items[MAX_HEAP];
!int
size;
};!
3/25/11 !// Converts the semiheap rooted at !
!// index root into a heap!
!// array of heap items!
!// number of heap items! CS202  Fundamental Structures of Computer Science II 27 Heap Implementa2on
// Default constructor !
Heap::Heap() : size(0) {!
}! bool Heap::heapIsEmpty() const {!
!return (size == 0);!
}! 3/25/11 CS202  Fundamental Structures of Computer Science II 28 Heap Implementa2on
heapInsert
void Heap::heapInsert(const HeapItemType& newItem) throw(HeapException) {!
!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 ( (place > 0) && (items[place].getKey() > items[parent].getKey()) ) {!
!
!HeapItemType temp = items[parent];!
!
!items[parent] = items[place];!
!
!items[place] = temp;!
!
!place = parent;!
!
!parent = (place  1)/2;!
!}!
!++size;!
}!
3/25/11 CS202  Fundamental Structures of Computer Science II 29 Heap Implementa2on
heapDelete
void Heap::heapDelete(HeapItemType& rootItem) throw(HeapException) {!
!if (heapIsEmpty())!
!
!throw HeapException("HeapException: Heap empty");!
!else {!
!
!rootItem = items[0];!
!
!items[0] = items[size];!
!
!heapRebuild(0);!
!}!
}! 3/25/11 CS202  Fundamental Structures of Computer Science II 30 Heap Implementa2on
heapRebuild
void Heap::heapRebuild(int root) { ! !int child = 2 * root + 1;
!
!//
!if ( child < size ) {
!
!//
!
!
!
!
!
!//
!
!int rightChild = child + 1;
!//
!
!!
!
!if ( (rightChild < size) && !
!
!
(items[rightChild].getKey() >
!
!
!child = rightChild;
!//
!
!
!
!
!
!
!
!
!}!
}!
3/25/11 index of root's left child, if any!
root is not a leaf so that !
it has a left child!
index of a right child, if any! items[child].getKey()) )!
index of larger child! !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);!
!}! CS202  Fundamental Structures of Computer Science II 31 Heap Implementa2on of PriorityQueue
• The heap implementa9on of the priority queue is straighforward – Since the heap opera9ons and the priority queue opera9ons are the same. • When we use the heap, – Inser9on and dele9on opera9ons of the priority queue will be O(log2n). 3/25/11 CS202  Fundamental Structures of Computer Science II 32 Heap Implementa2on of PriorityQueue
#include "Heap.h" // ADT heap operations!
typedef HeapItemType PQItemType;!
class PriorityQueue {!
public:!
!// default constructor, copy constructor, and destructor !
!// are supplied by the compiler!
!// priorityqueue operations:!
!bool pqIsEmpty() const;!
!void pqInsert(const PQItemType& newItem) throw (PQException);!
!void pqDelete(PQItemType& priorityItem) throw (PQException);!
private:!
!Heap h;!
};! 3/25/11 CS202  Fundamental Structures of Computer Science II 33 Heap Implementa2on of PriorityQueue
bool PriorityQueue::pqIsEmpty() const {!
!return h.heapIsEmpty();!
}!
void PriorityQueue::pqInsert(const PQItemType& newItem) throw (PQException){!
!try {!
!
!h.heapInsert(newItem);!
!}!
!catch (HeapException e) {!
!
!throw PQueueException("Priority queue is full");!
!}!
}!
void PriorityQueue::pqDelete(PQItemType& priorityItem) throw (PQException) {!
!try {!
!
!h.heapDelete(priorityItem);!
!}!
!catch (HeapException e) {!
!
!throw PQueueException("Priority queue is empty");!
!}!
}!
3/25/11 CS202  Fundamental Structures of Computer Science II 34 Heapsort
We can make use of a heap to sort an array: 1. Create a heap from the given ini9al array with n items. 2. Swap the root of the heap with the last element in the heap. 3. Now, we have a semiheap with n
1 items, and a sorted array with one item. 4. Using heapRebuild convert this semiheap into a heap. Now we will have a heap with n
1 items. 5. Repeat the steps 2
4 as long as the number of items in the heap is more than 1. 3/25/11 CS202  Fundamental Structures of Computer Science II 35 Heapsort
Building a heap from an array
A heap corresponding to anArray The ini9al contents of anArray 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.!
}!
3/25/11 CS202  Fundamental Structures of Computer Science II 36 Heapsort
Building a heap from an array
3/25/11 CS202  Fundamental Structures of Computer Science II 37 Heapsort
heapSort(inout anArray:ArrayType, in n:integer) {
!// build an initial heap!
!for (index = (n/2) – 1 ; index >= 0 ;
!
!heapRebuild(anArray, index, n)!
!for
!
!
!
!
!
!
!
!
!}!
}! 3/25/11 !! index)! (last = n1 ; last > 0 ; last) { !
!// invariant: anArray[0..last] is a heap, !
!// anArray[last+1..n1] is sorted and !
!// contains the largest items of anArray.!
!!
!swap anArray[0] and anArray[last]!
!!
!// make the heap region a heap again!
!heapRebuild(anArray, 0, last)! CS202  Fundamental Structures of Computer Science II 38 Heapsort
• Heapsort par99ons an array into two regions. HeapRegion SortedRegion • Each step moves an item from the HeapRegion to SortedRegion. • The invariant of the heapsort algorithm is: AMer the kth step, – The SortedRegion contains the k largest value and they are in sorted order. – The items in the HeapRegion form a heap. 3/25/11 CS202  Fundamental Structures of Computer Science II 39 Heapsort
Trace 3/25/11 CS202  Fundamental Structures of Computer Science II 40 Heapsort
Trace
3/25/11 CS202  Fundamental Structures of Computer Science II 41 Heapsort
Analysis • Heapsort is O(n log n) O(n log n) at the average case at the worst case • Compared against quicksort, – Heapsort usually takes more 9me at the average case – But its worst case is also O(n log n). 3/25/11 CS202  Fundamental Structures of Computer Science II 42 ...
View
Full
Document
 Spring '11
 Pablo

Click to edit the document details