Source of BinarySearchTreeDemo.java


  1: //BinarySearchTreeDemo.java

  3: import java.util.Scanner;

  5: public class BinarySearchTreeDemo
  6: {
  7:     public static void main(String[] args)
  8:     {
  9:         Scanner scnr = new Scanner(System.in);
 10:         BinarySearchTree tree = new BinarySearchTree();

 12:         // Ask user for values to insert
 13:         System.out.print("Enter values to insert with spaces between: ");
 14:         String userValues = scnr.nextLine();
 15:         System.out.println();

 17:         // Add each value to the tree
 18:         for (String value : userValues.split(" "))
 19:         {
 20:             int key = Integer.parseInt(value);
 21:             tree.insert(new Node(key));
 22:         }

 24:         // Show the tree
 25:         System.out.println("Initial tree:");
 26:         System.out.println(BSTPrint.treeToString(tree.getRoot()));
 27:         System.out.println();

 29:         // Ask the user for a value to remove
 30:         System.out.print("Enter value to remove: ");
 31:         String removeValueString = scnr.nextLine();
 32:         int removeValue = Integer.parseInt(removeValueString);
 33:         System.out.println();

 35:         System.out.printf("Tree after removing %d:%n", removeValue);
 36:         tree.remove(removeValue);
 37:         System.out.println(BSTPrint.treeToString(tree.getRoot()));
 38:     }
 39: }