cosc2007 data structures ii
DESCRIPTION
COSC2007 Data Structures II. Chapter 12 Tables & Priority Queues II. Topics. Tables Implementation Array-based BST-based. ADT Table's Sorted Array-Based Implementation. public interface TableInterface { public boolean tableIsEmpty(); // Determines whether a table is empty - PowerPoint PPT PresentationTRANSCRIPT
COSC2007 Data Structures II
Chapter 12
Tables &
Priority Queues II
S. Xu2
Topics
Tables Implementation
Array-based BST-based
S. Xu3
ADT Table's Sorted Array-Based Implementation
public interface TableInterface {
public boolean tableIsEmpty();// Determines whether a table is empty
public int tableLength( );// Determines the number of items in a table
public void tableInsert(KeyedItem newItem) throw TableException;public boolean tableDelete(Comparable searchKey);public KeyedItem tableRetrieve(Comparable SearchKey) ;
} //end TableInterface
class TableException extends Exception{
public TableException(String message) { super ( message);
}} // end TableException
S. Xu4
ADT Table's Sorted Array-Based Implementation
//one item with a given search key at any timepublic class TableArrayBased implements TableInterface {
final int MAX_TABLE = 100; // maximum size of tableprotected KeyedItem [] items; //table itemprivate int size; //table size
public TableArrayBased() {items = new KeyedItem[MAX_TABLE];size =0;
} //default constructorpublic boolean tableIsEmpty() {
return size==0;}
public int tableLength () {return size;
}
S. Xu5
ADT Table's Sorted Array-Based Implementation
//one item with a given search key at any timepublic class TableArrayBased implements TableInterface {
final int MAX_TABLE = 100; // maximum size of tableprotected KeyedItem [] items; //table itemprivate int size; //table size
public TableArrayBased() {items = new KeyedItem[MAX_TABLE];size =0;
} //default constructorpublic boolean tableIsEmpty() {
return size==0;}
public int tableLength () {return size;
}
S. Xu6
ADT Table's Sorted Array-Based Implementation
Public void tableInsert(KeyedItem newItem) throws TableException {// 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 new TableException("TableException: Table full"); // there is room to insert; locate the position where newItem belongs int spot = position(newItem.getKey());
if ((spot<size) && (items[spot].getKey()).compareTo(newItem.getKey())==0) {//duplicate keythrow new TableException("TableException: Duplicate Key item");
}else { // 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 if} // end tableInsert
S. Xu7
ADT Table's Sorted Array-Based Implementationpublic boolean tableDelete(Comparable SearchKey) {
// Calls: Position.// locate the position where SearchKey exists/belongs int Spot = position(SearchKey); // is SearchKey present in the table? boolean success = (spot <= size) &&
(items[Spot].getKey().compareTo(SearchKey)==0); if (success) { // 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
return success;} // end tableDelete
S. Xu8
ADT Table's Sorted Array-Based Implementation
Public KeyedItem tableRetrieve(Comparable searchKey) {// Calls: position.// locate the position where SearchKey exists/belongs int spot = position(searchKey); // is searchKey present in table? boolean success = (spot <= size) &&
(items[spot].getKey().compareTo(searchKey)==0); if (success) return items[spot]; // item present; retrieve it
elsereturn null;
} // end tableRetrieve
S. Xu9
ADT Table's Sorted Array-Based Implementation
protected int position(Comparable searchKey) {// find the position of a table item or insertion point
int pos =0;while ((pos<size) &&(searchKey.compareTo(item[pos].getKey())>0))
pos++;return pos
} // end position
} //end TableArrayBased
S. Xu10
ADT Table's BST Implementation
Linear implementation are not good for general-purpose of ADT table
Non-linear (using BST) to represent the items in the table is better in most cases We can reuse BinarySearchTree class
implemented in Chapter 11
S. Xu11
ADT Table's BST Implementationpublic class TableBSTBased implements TableInterface{ protected BinarySearchTree bst; // binary search tree that contains the table’s items protected int size; // number of items in the table
public TableBSTBased () {bst = new BinarySearchTree();size=0;
}
//table operationspublic boolean tableIsEmpty() {
return size==0;} //end tableIsEmpty
public int tableLength (){return size;
} //end tablelength
S. Xu12
ADT Table's BST Implementationpublic class TableBSTBased implements TableInterface{ protected BinarySearchTree bst; // binary search tree that contains the table’s items protected int size; // number of items in the table
public TableBSTBased () {bst = new BinarySearchTree();size=0;
}
//table operationspublic boolean tableIsEmpty() {
return size==0;} //end tableIsEmpty
public int tableLength (){return size;
} //end tablelength
S. Xu13
ADT Table's BST Implementation
public void tableInsert(KeyedItem newItem) throws TableException {
if (bst.retrieve (newItem.getKey())==null) { bst.insert(newItem);
++size; } else
throw new TableException("TableException: duplicate key item");
} // end tableInsert
public KeyItem tableRetrieve(Comparable searchKey) { return bst.retrieve(searchKey);} // end tableRetrieve
S. Xu14
ADT Table's BST Implementation
public void tableInsert(KeyedItem newItem) throws TableException {
if (bst.retrieve (newItem.getKey())==null) { bst.insert(newItem);
++size; } else
throw new TableException("TableException: duplicate key item");
} // end tableInsert
public KeyItem tableRetrieve(Comparable searchKey) { return bst.retrieve(searchKey);} // end tableRetrieve
S. Xu15
ADT Table's BST Implementation
public boolean tableDelete(Comparable searchKey){
try { bst.delete(searchKey);
} // end try catch (TreeException e) {
return false; } // end catch
-- size;return true;
} // end tableDelete
protected void setSize (int newSize) { size = newSize; } // end setSize
}// End of implementation file.
S. Xu16
ADT Table's BST Implementation Test program
public class TestTable {
public statcic void main (String[] args) {TableInterface chart = new TableBSTBased ();City anItem;anItem = new City (“Windsor”, “canada”, 12000);chart.tableInsert (anItem);anItem = new City (“Soo”, “Canada”, 5000);chart.tableInsert(anItem);TableInteratorBST iter = new TableInteratorBST (chart);while (iter.gasNext())
displayCity ((City)iter.next());}public static void displayCity (City anItem) {
System.out.println (anItem.getCity());}
17
Review The ______ operation of the ADT table
throws a TableException. tableInsert tableDelete tableRetrieve tableTraverse
18
Review If the item with a given search key does
not exist in a table, the tableRetrieve operation returns ______. the value -1 the value 0 the value false a null reference
19
Review The sorted reference-based
implementation of the tableInsert operation is ______. O(1) O(n) O(n2) O(log n)
20
Review In the sorted linear implementation of a
table, the proper position of a new item to be inserted is determined ______. by the data type of the item by the size of the item by the value of the item by the value of the search key of the item
21
Review The sorted array-based implementation of
the tableInsert operation is ______. O(1) O(log n) O(n) O(n2)
22
Review Which of the following operations of the
binary search tree implementation of the ADT tree is O(n)? insertion deletion retrieval traversal