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
![Page 1: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/1.jpg)
COSC2007 Data Structures II
Chapter 12
Tables &
Priority Queues II
![Page 2: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/2.jpg)
S. Xu2
Topics
Tables Implementation
Array-based BST-based
![Page 3: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/3.jpg)
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
![Page 4: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/4.jpg)
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;
}
![Page 5: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/5.jpg)
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;
}
![Page 6: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/6.jpg)
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
![Page 7: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/7.jpg)
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
![Page 8: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/8.jpg)
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
![Page 9: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/9.jpg)
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
![Page 10: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/10.jpg)
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
![Page 11: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/11.jpg)
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
![Page 12: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/12.jpg)
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
![Page 13: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/13.jpg)
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
![Page 14: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/14.jpg)
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
![Page 15: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/15.jpg)
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.
![Page 16: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/16.jpg)
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());}
![Page 17: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/17.jpg)
17
Review The ______ operation of the ADT table
throws a TableException. tableInsert tableDelete tableRetrieve tableTraverse
![Page 18: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/18.jpg)
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
![Page 19: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/19.jpg)
19
Review The sorted reference-based
implementation of the tableInsert operation is ______. O(1) O(n) O(n2) O(log n)
![Page 20: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/20.jpg)
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
![Page 21: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/21.jpg)
21
Review The sorted array-based implementation of
the tableInsert operation is ______. O(1) O(log n) O(n) O(n2)
![Page 22: COSC2007 Data Structures II](https://reader036.vdocuments.us/reader036/viewer/2022062308/568131a1550346895d98109d/html5/thumbnails/22.jpg)
22
Review Which of the following operations of the
binary search tree implementation of the ADT tree is O(n)? insertion deletion retrieval traversal