ADT Table - PowerPoint PPT Presentation

About This Presentation
Title:

ADT Table

Description:

Array-Based Insertion ... // end pqDelete Heapsort We can use a heap to sort ... array based (an unsorted array) Unsorted, pointer based (a simple linked list ... – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 60
Provided by: Ilya90
Category:
Tags: adt | based | insertion | linked | list | sort | table

less

Transcript and Presenter's Notes

Title: ADT Table


1
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.

2
An ordinary table of cities
3
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.

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.

5
Sorted Linear Implementations
  1. Array-Based
  2. Pointer-Based

6
Binary Search Tree Implementation
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.

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)

9
Insertion for unsorted linear implementations
Runtime complexity of insertion in an unsorted
list O(1)
  1. array based
  2. (b) pointer based

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 tables 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
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.

12
Insertion for sorted linear implementations
  • array based
  • (b) pointer based

13
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.

14
The average-case order of the ADT table
operations for various implementations
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

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.

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 itemsindex to newItem.
  • int position(KeyType searchKey) const
  • // Finds the position of a table item or its
    insertion point.
  • private
  • TableItemType itemsMAX_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

18
tableInsert
  • void TabletableInsert(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 gt spot
    --index)
  • itemsindex1 itemsindex
  • // make the insertion
  • itemsspot newItem
  • size
  • // end tableInsert

19
tableDelete
  • void TabletableDelete(KeyType searchKey)
  • // Calls position.
  • // locate the position where searchKey
    exists/belongs
  • int spot position(searchKey)
  • // is searchKey present in the table?
  • if ((spot gt size) (itemsspot.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 lt size
    index)
  • itemsindex itemsindex1
  • // end if
  • // end tableDelete

20
tableRetrieve
  • void TabletableRetrieve(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 gt size) (itemsspot.getKey() !
    searchKey))
  • // searchKey not in table
  • throw TableException(
  • "TableException Item not found on
    retrieve")
  • else
  • tableItem itemsspot // item present
    retrieve it
  • // end tableRetrieve

21
traverseTable
  • void TabletraverseTable(FunctionType visit)
  • for (int index 0 index lt size index)
  • visit(itemsindex)
  • // end traverseTable

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 tables items

23
Binary Search Tree Implementation TableB.cpp
  • include "TableB.h" // header file
  • void TabletableInsert(const TableItemType
    newItem)
  • try
  • bst.searchTreeInsert(newItem)
  • size
  • // end try
  • catch (TreeException e)
  • throw TableException(
  • "TableException Cannot insert item")
  • // end catch
  • // end tableInsert

24
tableDelete
  • void TabletableDelete(KeyType searchKey)
  • try
  • bst.searchTreeDelete(searchKey)
  • // end try
  • catch (TreeException e)
  • throw TableException(
  • "TableException Item not found on
    delete")
  • // end catch
  • // end tableDelete

25
tableRetrieve traverseTable
  • void TabletableRetrieve(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 TabletraverseTable(FunctionType visit)
  • bst.inorderTraverse(visit)
  • // end traverseTable
  • // End of implementation file.

26
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

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.

28
Some implementations of the ADT priority queue
(a) array based (b) pointer based (c) binary
search tree
29
Implementations Analysis
  • None of these implementations of the priority
    queue is not efficient enough.
  • Array-Based
  • Insertion will be O(n)
  • Pointer-Based
  • Insertion will be O(n)
  • 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

30
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.

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.

32
Heap Examples
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.
34
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.

35
Heap Delete First Step
36
Heap Delete Second Step
37
Heap Delete Last Step
The last step of heapDelete transforms the
semiheap into a heap.
Last Step
First Step
Second Step
38
Heap Delete
Recursive calls to heapRebuild
39
Heap Delete Analysis
  • Since the height of a complete binary tree with n
    nodes is always
  • ? log2(n1)?
  • ? heapDelete is O(log2n)

40
Heap Insert
  • A new item is inserted at the bottom of the
    tree, and
  • it trickles up to its proper place

41
Heap Insert Analysis
  • Since the height of a complete binary tree with n
    nodes is always
  • ? log2(n1)?
  • ? heapInsert is O(log2n)

42
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

43
Heap.cpp
  • //
  • // Implementation file Heap.cpp for the ADT heap.
  • //
  • include "Heap.h" // header file for class Heap
  • HeapHeap() size(0)
  • // end default constructor
  • bool HeapheapIsEmpty() const
  • return bool(size 0)
  • // end heapIsEmpty

44
heapInsert
  • void HeapheapInsert(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 gt MAX_HEAP)
  • throw HeapException("HeapException Heap
    full")
  • // place the new item at the end of the heap
  • itemssize newItem
  • // trickle new item up to its proper position
  • int place size
  • int parent (place - 1)/2
  • while ( (parent gt 0)
  • (itemsplace.getKey() gt
    itemsparent.getKey()) )
  • // swap itemsplace and itemsparent
  • HeapItemType temp itemsparent
  • itemsparent itemsplace
  • itemsplace temp
  • place parent
  • parent (place - 1)/2

45
heapDelete
  • void HeapheapDelete(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 items0
  • items0 items--size
  • heapRebuild(0)
  • // end if
  • // end heapDelete

46
heapRebuild
  • void HeapheapRebuild(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 lt 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 lt size)
  • (itemsrightChild.getKey() gt
    itemschild.getKey()) )
  • child rightChild // index of larger
    child
  • // if the root's value is smaller than the
  • // value in the larger child, swap values
  • if ( itemsroot.getKey() lt
    itemschild.getKey() )
  • HeapItemType temp itemsroot
  • itemsroot itemschild
  • itemschild temp
  • // transform the new subtree into a heap
  • heapRebuild(child)

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.

48
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)
  • throw (PQueueException)
  • private
  • Heap h
  • // end PriorityQueue class

49
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 PriorityQueuepqIsEmpty() const
  • return h.heapIsEmpty()
  • // end pqIsEmpty
  • void PriorityQueuepqInsert(const
    PQueueItemType newItem)
  • try
  • h.heapInsert(newItem)
  • // end try
  • catch (HeapException e)
  • throw PQueueException(
  • "PQueueException Priority queue full")
  • // end catch
  • // end pqInsert

50
pqDelete
  • void PriorityQueuepqDelete(PQueueItemType
    priorityItem)
  • try
  • h.heapDelete(priorityItem)
  • // end try
  • catch (HeapException e)
  • throw PQueueException(
  • "PQueueException Priority queue
    empty")
  • // end catch
  • // end pqDelete

51
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.

52
Heapsort (cont.)
  1. The initial contents of anArray
  2. anArray corresponding binary tree

53
Heapsort Building a Heap from an array
  • for (index (n/2) 1 index gt 0 index--)
  • // Invariant the tree rooted at index
  • // is a semiheap
  • heapRebuild(anArray, index, n)
  • // Assertion the tree rooted at index
  • // is a heap.

54
Heapsort Building a Heap from an array
55
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.

56
Heapsort Algorithm
  • heapSort(inout anArrayArrayType, in ninteger)
    // sorts anArray0..n-1
  • // build initial heap
  • for (index (n/2) 1 index gt 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 gt 0 last--)
  • // Invariant anArray0..last is a heap,
    anArraylast1..n-1 is sorted and
  • // contains the largest items of anArray.
  • // swap the largest item (anArray0) and the
    last item in the Heap region.
  • swap anArray0 and anArraylast
  • // make the Heap region a heap again
  • heapRebuild(anArray, 0, last)

57
Heapsort Trace
58
Heapsort Trace
59
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).
Write a Comment
User Comments (0)
About PowerShow.com