Driver.java (5119B)
1 package lab11; 2 import java.util.Scanner; 3 4 public class Driver { 5 Scanner in = new Scanner(System.in); 6 7 public static void main(String[] args) { 8 int userSelection; 9 Driver ui = new Driver(); 10 11 MyBinarySearchTreePlus<Item, String> BST = new MyBinarySearchTreePlus<>(); 12 MyBinarySearchTreePlus<Item, String> copyBST; 13 14 ui.listOptions(); 15 16 do { 17 userSelection = ui.menuSelection(); 18 System.out.println(userSelection); 19 switch (userSelection) { 20 case 1: 21 ui.insertKey(BST); 22 break; 23 case 2: 24 ui.removeKey(BST); 25 break; 26 case 3: 27 ui.search(BST); 28 break; 29 case 4: 30 ui.getHeight(BST); 31 break; 32 case 5: 33 ui.getSize(BST); 34 break; 35 case 6: 36 ui.displayInorder(BST); 37 break; 38 case 7: 39 ui.displayPreorder(BST); 40 break; 41 case 8: 42 ui.displayPostorder(BST); 43 break; 44 case 9: 45 copyBST = ui.copyTree(BST); 46 ui.testCopy(BST, copyBST); 47 break; 48 case 10: 49 ui.exitProgram(); 50 break; 51 } 52 } while(userSelection != 10); 53 } 54 55 56 public void insertKey(MyBinarySearchTreePlus<Item, String> BST) { 57 String key; 58 System.out.print("Enter key to insert: "); 59 key = in.next().trim(); 60 System.out.print(key); 61 BST.insert(new Item(key)); 62 System.out.println("Key " + key + " inserted"); 63 } 64 65 public void removeKey(MyBinarySearchTreePlus<Item, String > BST) { 66 String key; 67 System.out.print("Enter key to remove: "); 68 key = in.next().trim(); 69 System.out.print(key); 70 BST.delete(key); 71 System.out.println("Key " + key + " deleted"); 72 } 73 74 public void search(MyBinarySearchTreePlus<Item, String> BST) { 75 String key; 76 Item result; 77 System.out.print("Enter key to search: "); 78 key = in.next().trim(); 79 System.out.print(key); 80 result = BST.retrieve(key); 81 if (result == null) { 82 System.out.println("Key not found"); 83 } 84 else { 85 System.out.println("Key " + result.getKey() + " found"); 86 } 87 } 88 89 public void getHeight(MyBinarySearchTreePlus<Item, String> BST) { 90 System.out.println("The height of the tree is " + BST.getHeight()); 91 } 92 93 public void getSize(MyBinarySearchTreePlus<Item, String> BST) { 94 System.out.println("The size of the tree is " + BST.getSize()); 95 } 96 97 public void displayInorder(MyBinarySearchTreePlus<Item, String> BST) { 98 System.out.println(BST.toStringInorder()); 99 } 100 101 public void displayPreorder(MyBinarySearchTreePlus<Item, String> BST) { 102 System.out.println(BST.toStringPreorder()); 103 } 104 105 public void displayPostorder(MyBinarySearchTreePlus<Item, String> BST) { 106 System.out.println(BST.toStringPostorder()); 107 } 108 109 public MyBinarySearchTreePlus<Item, String> copyTree(MyBinarySearchTreePlus<Item, String> BST) { 110 return BST.getCopyOfTree(); 111 } 112 113 public void testCopy(MyBinarySearchTreePlus<Item, String> BST, MyBinarySearchTreePlus<Item, String> copyBST) { 114 System.out.println("ORIGINAL TREE INORDER"); 115 displayInorder(BST); 116 117 System.out.println("COPY TREE INORDER"); 118 displayInorder(copyBST); 119 120 System.out.println("ADDING A KEY TO ORIGINAL TREE"); 121 insertKey(BST); 122 123 System.out.println("REMOVING ITEM FROM COPY TREE"); 124 removeKey(copyBST); 125 126 System.out.println("COMPARISON OF ORIGINAL AND COPY"); 127 displayInorder(BST); 128 displayInorder(copyBST); 129 } 130 131 132 133 134 135 136 137 138 public int menuSelection() { 139 System.out.print("Make your menu selection now: "); 140 return Integer.parseInt(in.next().trim()); 141 } 142 143 public void listOptions(){ 144 System.out.println("Select from the following menu:"); 145 System.out.println(" 1. Insert key into BST"); 146 System.out.println(" 2. Remove key from BST"); 147 System.out.println(" 3. Search for key in BST"); 148 System.out.println(" 4. Display height of BST"); 149 System.out.println(" 5. Display size of BST"); 150 System.out.println(" 6. Display content of BST inorder"); 151 System.out.println(" 7. Display content of BST in preorder"); 152 System.out.println(" 8. Display content of BST in postorder"); 153 System.out.println(" 9. Build copy of tree and test it"); 154 System.out.println(" 10. Exit program"); 155 } 156 157 void exitProgram(){ 158 System.out.println("Exiting program...Good Bye"); 159 System.exit(0); 160 } 161 }