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 pathBTView.java
More file actions
54 lines (47 loc) · 1.6 KB
/
BTView.java
File metadata and controls
54 lines (47 loc) · 1.6 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
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
public class BTView extends Pane {
private BST<Integer> tree = new BST<>();
private double radius = 15; // Tree node radius
private double vGap = 50; // Gap between two levels in a tree
BTView(BST<Integer> tree) {
this.tree = tree;
setStatus("Tree is empty");
}
public void setStatus(String msg) {
getChildren().add(new Text(20, 20, msg));
}
public void displayTree() {
this.getChildren().clear(); // Clear the pane
if (tree.getRoot() != null) {
// Display tree recursively
displayTree(tree.getRoot(), getWidth() / 2, vGap,
getWidth() / 4);
}
}
/** Display a subtree rooted at position (x, y) */
private void displayTree(BST.TreeNode<Integer> root,
double x, double y, double hGap) {
if (root.left != null) {
// Draw a line to the left node
getChildren().add(new Line(x - hGap, y + vGap, x, y));
// Draw the left subtree recursively
displayTree(root.left, x - hGap, y + vGap, hGap / 2);
}
if (root.right != null) {
// Draw a line to the right node
getChildren().add(new Line(x + hGap, y + vGap, x, y));
// Draw the right subtree recursively
displayTree(root.right, x + hGap, y + vGap, hGap / 2);
}
// Display a node
Circle circle = new Circle(x, y, radius);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
getChildren().addAll(circle,
new Text(x - 4, y + 4, root.element + ""));
}
}