1  import javafx.application.Application;
  2  import javafx.geometry.Pos;
  3  import javafx.stage.Stage;
  4  import javafx.scene.Scene;
  5  import javafx.scene.control.Button;
  6  import javafx.scene.control.Label;
  7  import javafx.scene.control.TextField;
  8  import javafx.scene.layout.BorderPane;
  9  import javafx.scene.layout.HBox;
 10  
 11  public class BSTAnimation extends Application {
 12    @Override // Override the start method in the Application class
 13    public void start(Stage primaryStage) {
 14      BST<Integer> tree = new BST<>(); // Create a tree
 15  
 16      BorderPane pane = new BorderPane();
 17      BTView view = new BTView(tree); // Create a View
 18      pane.setCenter(view);
 19  
 20      TextField tfKey = new TextField();
 21      tfKey.setPrefColumnCount(3);
 22      tfKey.setAlignment(Pos.BASELINE_RIGHT);
 23      Button btInsert = new Button("Insert");
 24      Button btDelete = new Button("Delete");
 25      HBox hBox = new HBox(5);
 26      hBox.getChildren().addAll(new Label("Enter a key: "),
 27        tfKey, btInsert, btDelete);
 28      hBox.setAlignment(Pos.CENTER);
 29      pane.setBottom(hBox);
 30  
 31      btInsert.setOnAction(e -> {
 32        int key = Integer.parseInt(tfKey.getText());
 33        if (tree.search(key)) { // key is in the tree already
 34          view.displayTree();
 35          view.setStatus(key + " is already in the tree");
 36        } 
 37        else {
 38          tree.insert(key); // Insert a new key
 39          view.displayTree();
 40          view.setStatus(key + " is inserted in the tree");
 41        }
 42      });
 43  
 44      btDelete.setOnAction(e -> {
 45        int key = Integer.parseInt(tfKey.getText());
 46        if (!tree.search(key)) { // key is not in the tree
 47          view.displayTree();
 48          view.setStatus(key + " is not in the tree");
 49        } 
 50        else {
 51          tree.delete(key); // Delete a key
 52          view.displayTree();
 53          view.setStatus(key + " is deleted from the tree");
 54        }
 55      });
 56  
 57      // Create a scene and place the pane in the stage
 58      Scene scene = new Scene(pane, 450, 250);
 59      primaryStage.setTitle("BSTAnimation"); // Set the stage title
 60      primaryStage.setScene(scene); // Place the scene in the stage
 61      primaryStage.show(); // Display the stage
 62    }
 63  
 64    /**
 65     * The main method is only needed for the IDE with limited
 66     * JavaFX support. Not needed for running from the command line.
 67     */
 68    public static void main(String[] args) {
 69      launch(args);
 70    }
 71  }