Source of Tree234Demo.java


  1: //Tree234Demo.java

  3: import java.util.Scanner;

  5: public class Tree234Demo
  6: {
  7:     public static void main(String[] args)
  8:     {
  9:         Scanner scnr = new Scanner(System.in);
 10:         System.out.print("Enter insert values with spaces between: ");
 11:         String userValues = scnr.nextLine();
 12:         System.out.println();

 14:         // Create a 2-3-4 tree and add the values
 15:         Tree234 tree = new Tree234();
 16:         for (String value : userValues.split(" "))
 17:         {
 18:             tree.insert(Integer.parseInt(value));
 19:         }

 21:         // Display the height after all inserts are complete.
 22:         System.out.printf("2-3-4 tree with %d keys has height %d%n",
 23:                           tree.length(), tree.getHeight());

 25:         // Read user input to get a list of values to remove
 26:         System.out.print("Enter remove values with spaces between: ");
 27:         userValues = scnr.nextLine();
 28:         System.out.println();

 30:         for (String value : userValues.split(" "))
 31:         {
 32:             Integer toRemove = Integer.parseInt(value);
 33:             System.out.printf("Remove node %s: ", value);
 34:             if (tree.remove(toRemove))
 35:             {
 36:                 System.out.printf
 37:                 (
 38:                     "2-3-4 tree with %d keys has height %d%n",
 39:                     tree.length(), tree.getHeight()
 40:                 );
 41:             }
 42:             else
 43:             {
 44:                 System.out.println
 45:                 (
 46:                     "*** Key not found. Tree is not changed. ***"
 47:                 );
 48:             }
 49:         }
 50:     }
 51: }