Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chn.util.*;
- public class TreeStatsTester {
- /* void testFind(BSTree list)
- {
- int idToFind;
- Item location;
- ConsoleIO console = new ConsoleIO();
- System.out.println("Testing search algorithm\n");
- System.out.print("Enter Id value to search for (-1 to quit) --> ");
- idToFind = console.readInt();
- while (idToFind >= 0)
- {
- location = (Item)list.find(new Item(idToFind, 0));
- if (location == null)
- System.out.println("Id = " + idToFind + " No such part in stock");
- else
- System.out.println(location);
- System.out.println();
- System.out.print("Enter Id value to search for (-1 to quit) --> ");
- idToFind = console.readInt();
- }
- }*/
- public void readData(BSTree list)
- {
- FileInput inFile;
- String fileName = "fileA.txt";
- inFile = new FileInput(fileName);
- char letter;
- while (inFile.hasMoreTokens())
- {
- letter = inFile.readToken().charAt(0);
- list.insert(letter);
- }
- }
- public void mainMenu (BSTree head)
- {
- String choice;
- ConsoleIO console = new ConsoleIO();
- do
- {
- System.out.println("BSTree List algorithm menu\n");
- System.out.println("(1) Read data from disk");
- System.out.println("(2) Preorder output");
- System.out.println("(3) Inorder output");
- System.out.println("(4) Postorder output");
- System.out.println("(5) Count the nodes in the tree");
- System.out.println("(6) Count the leaves in the tree");
- System.out.println("(7) Find the height of the tree");
- System.out.println("(8) Find the width of the tree");
- System.out.println("(9) Clear the tree");
- System.out.println("(Q) Quit\n");
- System.out.print("Choice ---> ");
- choice = console.readLine() + " "; // kludge to ensure choice.charAt(0) > 0
- System.out.println();
- if ('1' <= choice.charAt(0) && choice.charAt(0) <= '9')
- {
- switch (choice.charAt(0))
- {
- case '1' :
- readData(head);
- break;
- case '2' :
- System.out.println();
- System.out.println("The tree printed in preorder\n");
- head.preOrder();
- System.out.println();
- break;
- case '3' :
- System.out.println();
- System.out.println("The tree printed inorder\n");
- head.inOrder();
- System.out.println();
- break;
- case '4' :
- System.out.println();
- System.out.println("The tree printed in postorder\n");
- head.postOrder();
- System.out.println();
- break;
- case '5' :
- System.out.println();
- System.out.println("Number of nodes = " + head.countNode());
- System.out.println();
- break;
- case '6' :
- System.out.println();
- System.out.println("Number of leaves = " +head.countLeaves());
- System.out.println();
- break;
- case '7' :
- System.out.println();
- System.out.println("The height of the tree = " + head.findHeight());
- System.out.println();
- break;
- case '8' :
- System.out.println();
- System.out.println("The width of the tree = " + head.findWidth());
- System.out.println();
- break;
- case '9' :
- System.out.println();
- head.clearTree();
- System.out.println("Tree cleared");
- System.out.println();
- break;
- }
- }
- }
- while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
- }
- public static void main(String[] args)
- {
- TreeStatsTester test = new TreeStatsTester();
- BSTree list = new BSTree();
- test.mainMenu (list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment