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; // 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  
 21    public void displayTree() {
 22      this.getChildren().clear(); // Clear the pane
 23      if (tree.getRoot() != null) {
 24        // Display tree recursively    
 25        displayTree(tree.getRoot(), getWidth() / 2, vGap,
 26          getWidth() / 4);
 27      }
 28    }
 29  
 30    /** Display a subtree rooted at position (x, y) */
 31    private void displayTree(BST.TreeNode<Integer> root,
 32        double x, double y, double hGap) {
 33      if (root.left != null) {
 34        // Draw a line to the left node
 35        getChildren().add(new Line(x - hGap, y + vGap, x, y));
 36        // Draw the left subtree recursively
 37        displayTree(root.left, x - hGap, y + vGap, hGap / 2);
 38      }
 39  
 40      if (root.right != null) {
 41        // Draw a line to the right node
 42        getChildren().add(new Line(x + hGap, y + vGap, x, y));
 43        // Draw the right subtree recursively
 44        displayTree(root.right, x + hGap, y + vGap, hGap / 2);
 45      }
 46      
 47      // Display a node
 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  }