1 / 22

COSC2007 Data Structures II

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

Download Presentation

COSC2007 Data Structures II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COSC2007 Data Structures II Chapter 12 Tables & Priority Queues II

  2. Topics • Tables • Implementation • Array-based • BST-based S. Xu

  3. ADT Table's Sorted Array-Based Implementation public interface TableInterface { public booleantableIsEmpty(); // 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. Xu

  4. ADT Table's Sorted Array-Based Implementation //one item with a given search key at any time public class TableArrayBased implements TableInterface { final int MAX_TABLE = 100; // maximum size of table protected KeyedItem [] items; //table item private int size; //table size public TableArrayBased() { items = new KeyedItem[MAX_TABLE]; size =0; } //default constructor public boolean tableIsEmpty() { return size==0; } public int tableLength () { return size; } S. Xu

  5. ADT Table's Sorted Array-Based Implementation //one item with a given search key at any time public class TableArrayBased implements TableInterface { final int MAX_TABLE = 100; // maximum size of table protected KeyedItem [] items; //table item private int size; //table size public TableArrayBased() { items = new KeyedItem[MAX_TABLE]; size =0; } //default constructor public boolean tableIsEmpty() { return size==0; } public int tableLength () { return size; } S. Xu

  6. 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 key throw 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. Xu

  7. ADT Table's Sorted Array-Based Implementation public 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. Xu

  8. 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 else return null; } // end tableRetrieve S. Xu

  9. 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. Xu

  10. 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. Xu

  11. ADT Table's BST Implementation public 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 operations public boolean tableIsEmpty() { return size==0; } //end tableIsEmpty public int tableLength (){ return size; } //end tablelength S. Xu

  12. ADT Table's BST Implementation public 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 operations public boolean tableIsEmpty() { return size==0; } //end tableIsEmpty public int tableLength (){ return size; } //end tablelength S. Xu

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

  14. 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. Xu

  15. 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. Xu

  16. 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()); } S. Xu

  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(logn)

  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(logn) • 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

More Related