Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainProgram{
- public static void main(String []args){
- java.util.Scanner in = new java.util.Scanner(System.in);
- System.out.print("1-Manual input or 2-Random\n>");
- int option=in.nextInt();
- if(option==1){
- manual();
- }
- else if(option==2){
- random();
- }
- else{
- main(args);
- }
- }
- public static void manual(){
- BinarySearchTree tree=new BinarySearchTree();
- int value=0,option=1;
- java.util.Scanner in = new java.util.Scanner(System.in);
- while(option>0&&option<=3){
- System.out.print("Input option ( 1 - insert , 2 - remove , 3 - print detail , other number - exit) :\n>");
- option=in.nextInt();
- if(option==1){
- System.out.print("Insert an Integer to add\n>");
- value=in.nextInt();
- tree.insert(value);
- }
- else if(option==2){
- System.out.print("Insert an Integer to remove\n>");
- value=in.nextInt();
- tree.deleteNode(value);
- }
- else if(option==3){
- System.out.print("\nTree Information :\n");
- printDetail(tree);
- }
- System.exit(0);
- }
- System.exit(0);
- }
- public static void random(){
- BinarySearchTree tree=new BinarySearchTree();
- int value,option;
- java.util.Scanner in = new java.util.Scanner(System.in);
- System.out.print("How many time the system need to loop :\n>");
- int n=in.nextInt();
- System.out.println("\nStart.....");
- for (int i=0;i<n;i++){
- option=(int)Math.round((Math.random()*1));
- value=(int)(Math.random()*100);
- if(option==0||i==0){
- System.out.println(">Insert "+value);
- tree.insert(value);
- }
- else{
- System.out.println(">Delete "+value);
- tree.deleteNode(value);
- }
- }
- System.out.print("\nTree Information :\n");
- printDetail(tree);
- }
- public static void printDetail(BinarySearchTree tree){
- System.out.println("Height : "+tree.treeHeight());
- System.out.println("No. of leave : "+tree.leaveCount());
- System.out.println("No. of node : "+tree.nodeCount());
- System.out.println("Preorder :");
- tree.preorderTraversal();
- System.out.println("Inorder :");
- tree.inorderTraversal();
- System.out.println("Postorder :");
- tree.postorderTraversal();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment