This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTAnimation.java
More file actions
69 lines (62 loc) · 2.24 KB
/
BSTAnimation.java
File metadata and controls
69 lines (62 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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");
HBox hBox = new HBox(5);
hBox.getChildren().addAll(new Label("Enter a key: "),
tfKey, btInsert, btDelete);
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");
}
});
// Create a scene and place the pane in the stage
Scene scene = new Scene(pane, 450, 250);
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);
}
}