aznishboy

TreeStatsTester

Mar 14th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.84 KB | None | 0 0
  1. import chn.util.*;
  2. public class TreeStatsTester {
  3.  /* void testFind(BSTree list)
  4.   {
  5.     int idToFind;
  6.     Item location;
  7.  
  8.     ConsoleIO console = new ConsoleIO();
  9.  
  10.     System.out.println("Testing search algorithm\n");
  11.     System.out.print("Enter Id value to search for (-1 to quit) --> ");
  12.     idToFind = console.readInt();
  13.  
  14.     while (idToFind >= 0)
  15.     {
  16.       location = (Item)list.find(new Item(idToFind, 0));
  17.       if (location == null)
  18.         System.out.println("Id = " + idToFind + "  No such part in stock");
  19.       else
  20.         System.out.println(location);
  21.       System.out.println();
  22.       System.out.print("Enter Id value to search for (-1 to quit) --> ");
  23.       idToFind = console.readInt();
  24.     }
  25.   }*/
  26.  
  27.   public void readData(BSTree list)
  28.   {
  29.     FileInput inFile;
  30.  
  31.     String fileName = "fileA.txt";
  32.  
  33.     inFile = new FileInput(fileName);
  34.     char letter;
  35.     while (inFile.hasMoreTokens())
  36.     {
  37.       letter = inFile.readToken().charAt(0);
  38.       list.insert(letter);
  39.     }
  40.   }
  41.  
  42.   public void mainMenu (BSTree head)
  43.   {
  44.     String choice;
  45.     ConsoleIO console = new ConsoleIO();
  46.  
  47.     do
  48.     {
  49.       System.out.println("BSTree List algorithm menu\n");
  50.       System.out.println("(1) Read data from disk");
  51.       System.out.println("(2) Preorder output");
  52.       System.out.println("(3) Inorder output");
  53.       System.out.println("(4) Postorder output");
  54.       System.out.println("(5) Count  the nodes in the tree");
  55.       System.out.println("(6) Count the leaves in the tree");
  56.       System.out.println("(7) Find the height of the tree");
  57.       System.out.println("(8) Find the width of the tree");
  58.       System.out.println("(9) Clear the tree");
  59.       System.out.println("(Q) Quit\n");
  60.       System.out.print("Choice ---> ");
  61.       choice = console.readLine() + " ";  // kludge to ensure choice.charAt(0) > 0
  62.  
  63.       System.out.println();
  64.  
  65.       if ('1' <= choice.charAt(0) && choice.charAt(0) <= '9')
  66.       {
  67.         switch (choice.charAt(0))
  68.         {
  69.           case '1' :
  70.             readData(head);
  71.             break;
  72.           case '2' :
  73.             System.out.println();
  74.             System.out.println("The tree printed in preorder\n");
  75.             head.preOrder();
  76.             System.out.println();
  77.             break;
  78.           case '3' :
  79.             System.out.println();
  80.             System.out.println("The tree printed inorder\n");
  81.             head.inOrder();
  82.             System.out.println();
  83.             break;
  84.           case '4' :
  85.             System.out.println();
  86.             System.out.println("The tree printed in postorder\n");
  87.             head.postOrder();
  88.             System.out.println();
  89.             break;
  90.           case '5' :
  91.             System.out.println();
  92.             System.out.println("Number of nodes = " + head.countNode());
  93.             System.out.println();
  94.             break;
  95.           case '6' :
  96.             System.out.println();
  97.             System.out.println("Number of leaves = " +head.countLeaves());
  98.             System.out.println();
  99.             break;
  100.           case '7' :
  101.             System.out.println();
  102.             System.out.println("The height of the tree = " + head.findHeight());
  103.             System.out.println();
  104.             break;
  105.           case '8' :
  106.             System.out.println();
  107.             System.out.println("The width of the tree = " + head.findWidth());
  108.             System.out.println();
  109.             break;
  110.           case '9' :
  111.             System.out.println();
  112.             head.clearTree();
  113.             System.out.println("Tree cleared");
  114.             System.out.println();
  115.             break;
  116.         }
  117.       }
  118.     }
  119.     while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
  120.   }
  121.  
  122.   public static void main(String[] args)
  123.   {
  124.     TreeStatsTester test = new TreeStatsTester();
  125.     BSTree list = new BSTree();
  126.  
  127.     test.mainMenu (list);
  128.   }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment