Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment04;
- import java.util.ArrayList;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- public class BSTAnimation extends Application {
- @Override // Override the start method in the Application class
- public void start(Stage primaryStage) {
- BST<Integer> tree = new BST<>(); // Create a tree
- BorderPane pane = new BorderPane();
- BTView view = new BTView(tree); // Create a View
- pane.setCenter(view);
- TextField tfKey = new TextField();
- tfKey.setPrefColumnCount(3);
- tfKey.setAlignment(Pos.BASELINE_RIGHT);
- Button btInsert = new Button("Insert");
- Button btDelete = new Button("Delete");
- Button search = new Button("Search");
- Button inOrder = new Button("Inorder");
- Button preOrder = new Button("Preorder");
- Button postOrder = new Button("Postorder");
- Button breadthFirst = new Button("Breadth-First");
- Button height = new Button("Height");
- Button clear = new Button("Clear");
- HBox hBox = new HBox(5);
- hBox.getChildren().addAll(new Label("Enter a key: "),
- tfKey, btInsert, btDelete, search, inOrder,
- preOrder, postOrder, breadthFirst, height, clear);
- hBox.setAlignment(Pos.CENTER);
- pane.setBottom(hBox);
- btInsert.setOnAction(e -> {
- int key = Integer.parseInt(tfKey.getText());
- if (tree.search(key)) { // key is in the tree already
- view.displayTree();
- view.setStatus(key + " is already in the tree");
- }
- else {
- tree.insert(key); // Insert a new key
- view.displayTree();
- view.setStatus(key + " is inserted in the tree");
- }
- });
- btDelete.setOnAction(e -> {
- int key = Integer.parseInt(tfKey.getText());
- if (!tree.search(key)) { // key is not in the tree
- view.displayTree();
- view.setStatus(key + " is not in the tree");
- }
- else {
- tree.delete(key); // Delete a key
- view.displayTree();
- view.setStatus(key + " is deleted from the tree");
- }
- });
- search.setOnAction(e -> {
- int key = Integer.parseInt(tfKey.getText());
- ArrayList<BST.TreeNode<Integer>> path = tree.path(key);
- if(!tree.search(key))
- {
- view.displayTree(path);
- view.setStatus(key + " is not in the tree");
- }
- else
- {
- view.displayTree(path);
- view.setStatus("Found " + key + " in the tree");
- }
- });
- inOrder.setOnAction(e -> {
- view.displayTree();
- view.setStatus("" + tree.inorderList());
- });
- postOrder.setOnAction(e -> {
- view.displayTree();
- view.setStatus("" + tree.preorderList());
- });
- preOrder.setOnAction(e -> {
- view.displayTree();
- view.setStatus("" + tree.postorderList());
- });
- breadthFirst.setOnAction(e -> {
- view.displayTree();
- view.setStatus("" + tree.breadthFirstOrderList());
- });
- height.setOnAction(e -> {
- int tHeight = tree.height() - 1;
- view.displayTree();
- view.setStatus("Tree height is " + tHeight);
- });
- clear.setOnAction(e -> {
- tree.clear();
- view.displayTree();
- view.setStatus("Tree has been cleared");
- });
- // Create a scene and place the pane in the stage
- Scene scene = new Scene(pane, 750, 350);
- primaryStage.setTitle("BSTAnimation"); // Set the stage title
- primaryStage.setScene(scene); // Place the scene in the stage
- primaryStage.show(); // Display the stage
- }
- /**
- * The main method is only needed for the IDE with limited
- * JavaFX support. Not needed for running from the command line.
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement