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<>();
9 private double radius = 15;
10 private double vGap = 50;
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
21 public void displayTree() {
22 this.getChildren().clear();
23 if (tree.getRoot() != null) {
24
25 displayTree(tree.getRoot(), getWidth() / 2, vGap,
26 getWidth() / 4);
27 }
28 }
29
30
31 private void displayTree(BST.TreeNode<Integer> root,
32 double x, double y, double hGap) {
33 if (root.left != null) {
34
35 getChildren().add(new Line(x - hGap, y + vGap, x, y));
36
37 displayTree(root.left, x - hGap, y + vGap, hGap / 2);
38 }
39
40 if (root.right != null) {
41
42 getChildren().add(new Line(x + hGap, y + vGap, x, y));
43
44 displayTree(root.right, x + hGap, y + vGap, hGap / 2);
45 }
46
47
48 Circle circle = new Circle(x, y, radius);
49 circle.setFill(Color.WHITE);
50 circle.setStroke(Color.BLACK);
51 getChildren().addAll(circle,
52 new Text(x - 4, y + 4, root.element + ""));
53 }
54 }