Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class MainProgram{
  2.     public static void main(String []args){
  3.         java.util.Scanner in = new java.util.Scanner(System.in);
  4.         System.out.print("1-Manual input or 2-Random\n>");
  5.         int option=in.nextInt();
  6.         if(option==1){
  7.             manual();
  8.         }
  9.         else if(option==2){
  10.            random();
  11.         }
  12.         else{
  13.             main(args);
  14.         }
  15.        
  16.        
  17.     }
  18.     public static void manual(){
  19.         BinarySearchTree tree=new BinarySearchTree();
  20.         int value=0,option=1;
  21.         java.util.Scanner in = new java.util.Scanner(System.in);
  22.         while(option>0&&option<=3){
  23.             System.out.print("Input option ( 1 - insert , 2 - remove , 3 - print detail , other number - exit) :\n>");
  24.             option=in.nextInt();
  25.             if(option==1){
  26.                 System.out.print("Insert an Integer to add\n>");
  27.                 value=in.nextInt();
  28.                 tree.insert(value);
  29.             }
  30.             else if(option==2){
  31.                 System.out.print("Insert an Integer to remove\n>");
  32.                 value=in.nextInt();
  33.                 tree.deleteNode(value);
  34.             }
  35.             else if(option==3){
  36.                 System.out.print("\nTree Information :\n");
  37.                 printDetail(tree);
  38.             }
  39.             System.exit(0);
  40.         }
  41.         System.exit(0);
  42.     }
  43.     public static void random(){
  44.         BinarySearchTree tree=new BinarySearchTree();
  45.         int value,option;
  46.        
  47.         java.util.Scanner in = new java.util.Scanner(System.in);
  48.         System.out.print("How many time the system need to loop  :\n>");
  49.         int n=in.nextInt();
  50.         System.out.println("\nStart.....");
  51.        
  52.         for (int i=0;i<n;i++){
  53.             option=(int)Math.round((Math.random()*1));
  54.             value=(int)(Math.random()*100);
  55.             if(option==0||i==0){
  56.                 System.out.println(">Insert "+value);
  57.                 tree.insert(value);
  58.             }
  59.             else{
  60.                 System.out.println(">Delete "+value);
  61.                 tree.deleteNode(value);
  62.             }
  63.         }
  64.         System.out.print("\nTree Information :\n");
  65.         printDetail(tree);
  66.        
  67.     }
  68.     public static void printDetail(BinarySearchTree tree){
  69.        
  70.         System.out.println("Height       : "+tree.treeHeight());
  71.         System.out.println("No. of leave : "+tree.leaveCount());
  72.         System.out.println("No. of node  : "+tree.nodeCount());
  73.         System.out.println("Preorder :");
  74.         tree.preorderTraversal();
  75.         System.out.println("Inorder :");
  76.         tree.inorderTraversal();
  77.         System.out.println("Postorder :");
  78.         tree.postorderTraversal();
  79.     }
  80. }