Java Reference
In-Depth Information
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 }
else
{
37 tree.insert(key);
// Insert a new key
38 view.displayTree();
39 view.setStatus(key +
" is inserted in the tree"
);
40 }
41 });
42
43 btDelete.setOnAction(e -> {
44
int
key = Integer.parseInt(tfKey.getText());
45
if
(!tree.search(key)) {
// key is not in the tree
46 view.displayTree();
47 view.setStatus(key +
" is not in the tree"
);
48 }
else
{
49 tree.delete(key);
// Delete a key
50 view.displayTree();
51 view.setStatus(key +
" is deleted from the tree"
);
52 }
53 });
54
55
// Create a scene and place the pane in the stage
56 Scene scene =
new
Scene(pane,
450
,
250
);
57 primaryStage.setTitle(
"BSTAnimation"
);
// Set the stage title
58 primaryStage.setScene(scene);
// Place the scene in the stage
59 primaryStage.show();
// Display the stage
60 }
61 }
place hBox
handle insertion
insert key
display the tree
handle deletion
delete key
display the tree
L
ISTING
25.10
BTView.java
1
import
javafx.scene.layout.Pane;
2
import
javafx.scene.paint.Color;
3
import
javafx.scene.shape.Circle;
4
import
javafx.scene.shape.Line;
5
import
javafx.scene.text.Text;
6
7
public class
BTView
extends
Pane {
8
private
BST<Integer> tree =
new
BST<>();
tree to display
9
private double
radius =
15
;
// Tree node radius
10
private double
vGap =
50
;
// Gap between two levels in a tree
11
12 BTView(BST<Integer> tree) {
13
this
.tree = tree;
14 setStatus(
"Tree is empty"
);
15 }
16
17
public void
setStatus(String msg) {
18 getChildren().add(
new
Text(
20
,
20
, msg));
19 }
20
set a tree
Search WWH ::
Custom Search