// Name : Matthew Reeves // This is a program to test trees import TreePkg.Tree; import MenuPkg.Menu; import java.util.Scanner; import java.util.InputMismatchException; import java.io.File; import java.io.FileNotFoundException; import java.util.NoSuchElementException; import java.lang.IllegalStateException; public class TreeTest { //main method to test tree methods public static void main( String args[] ) { Scanner input=new Scanner(System.in); Tree testTree=new Tree(); String menuOpts[]={"Quit","Insert Item","Delete Item","Find Item","Get Length of Tree","Determine if Tree is Empty","Clear the Tree","Print the tree in Sorted Order (In-Order)","Print the tree in Pre-Order","Print the tree in Post-Order","Print the tree in Tree Format","Save tree to a file","Build tree from a file"}; Menu displayMenu=new Menu(menuOpts); System.out.println("\nThank you for using the tree test program!\n"); int choice; boolean continueLoop=true; //accepts and processes user's menu choices do { System.out.println("\tPlease choose from the following options\n"); choice=displayMenu.runMenu(); int value; try { switch (choice) { case 1: System.out.print("Enter a value to insert: "); value=input.nextInt(); testTree.insertItem(value); System.out.println("\nInsert attempted...\n"); break; case 2: System.out.print("Enter a value to delete: "); value=input.nextInt(); testTree.deleteItem(value); System.out.println("\nDelete attempted...\n"); break; case 3: System.out.print("Enter a value to find: "); value=input.nextInt(); if (testTree.findItem(value)) { System.out.printf("\n%d found in tree\n\n", value); } else { System.out.printf("\n%d NOT found in tree\n\n", value); } break; case 4: System.out.printf("Number of nodes in the tree = %d\n\n",(testTree.lengthIs()-3)/3); break; case 5: if (testTree.isEmpty()) { System.out.println("Tree is empty\n"); } else { System.out.println("Tree is NOT empty\n"); } break; case 6: testTree.clear(); System.out.println("Tree has been cleared\n"); break; case 7: System.out.print("InOrder traversal: "); testTree.inOrderTraversal(); System.out.println("\n"); break; case 8: System.out.print("PreOrder traversal: "); testTree.preOrderTraversal(); System.out.println("\n"); break; case 9: System.out.print("PostOrder traversal: "); testTree.postOrderTraversal(); System.out.println("\n"); break; case 10: System.out.println("Level order traversal:\n"); testTree.treeFormatTraversal(); System.out.println("\n"); break; case 11: if (testTree.saveToFile()) { System.out.println("Tree saved to file\n"); } else { System.out.println("Tree NOT saved to file\n"); } break; case 12: if (buildFromFile(testTree)) { System.out.println("Tree built from file\n"); } else { System.out.println("Tree NOT built\n"); } break; case 0: continueLoop=false; break; } } catch (InputMismatchException inputMismatchException) { System.err.printf("\nException: %s\n",inputMismatchException); System.out.println("\nYou must enter a valid value to add to the tree, please try again.\n"); input.nextLine(); continue; } }while(continueLoop); } //builds a new tree from a file selected by user public static boolean buildFromFile(Tree tree) { Scanner input2=new Scanner(System.in); System.out.print("Please enter the name of the file to input from: "); String filename=input2.nextLine(); Scanner inputFile; try { inputFile = new Scanner(new File(filename)); } catch (FileNotFoundException filesNotFoundException) { System.err.println( "Exception: File not found"); return false; } try { while (inputFile.hasNext()) { tree.insertItem(inputFile.nextInt()); } } catch (NoSuchElementException noSuchElementException) { System.err.println("File not formed correctly"); return false; } catch (IllegalStateException illegalStateException) { System.out.println ("Error reading from file"); return false; } if ( inputFile != null ) { inputFile.close(); } return true; } }