Java图形化二叉树
二叉树是一种由节点组成的常见数据结构,每个节点最多有两个子节点,分别称为左节点和右节点。二叉树有许多应用场景,如搜索树、堆叠、表达式分析等。在Java中,我们可以使用图形界面来显示和操作二叉树,以便用户能够更直观地理解和使用这个数据结构。
在本文中,我们将使用JavaFX框架实现图形二叉树,通过点击按钮添加节点,删除节点,并实时显示树的结构。
首先,我们需要创建一个BinaryTree类来表示二叉树。这类包含一个内部Treenode来表示树的节点。节点包含一个值字段和左右子节点的引用字段。
class BinaryTree { private TreeNode root; class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; this.left = null; this.right = null; } } // 如何添加节点 public void addNode(int val) { if (root == null) { root = new TreeNode(val); } else { addTo(root, val); } } private void addTo(TreeNode node, int val) { if (val < node.val) { if (node.left == null) { node.left = new TreeNode(val); } else { addTo(node.left, val); } } else { if (node.right == null) { node.right = new TreeNode(val); } else { addTo(node.right, val); } } } // 删除节点的方法 public void deleteNode(int val) { root = deleteFrom(root, val); } private TreeNode deleteFrom(TreeNode node, int val) { if (node == null) { return null; } if (val < node.val) { node.left = deleteFrom(node.left, val); } else if (val > node.val) { node.right = deleteFrom(node.right, val); } else { if (node.left == null && node.right == null) { node = null; } else if (node.left == null) { node = node.right; } else if (node.right == null) { node = node.left; } else { TreeNode minNode = findMin(node.right); node.val = minNode.val; node.right = deleteFrom(node.right, minNode.val); } } return node; } private TreeNode findMin(TreeNode node) { while (node.left != null) { node = node.left; } return node; }}
接下来,我们使用JavaFX来创建一个视觉界面。界面包含一个文本框来输入节点的值,一个添加按钮来添加节点,一个删除按钮来删除节点,一个画布来绘制二叉树的图形。
public class Main extends Application { private BinaryTree binaryTree; private GraphicsContext gc; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { binaryTree = new BinaryTree(); gc = createCanvas(primaryStage); TextField textField = new TextField(); Button addButton = new Button("Add"); Button deleteButton = new Button("Delete"); addButton.setOnAction(e -> { int val = Integer.parseInt(textField.getText()); binaryTree.addNode(val); drawTree(); }); deleteButton.setOnAction(e -> { int val = Integer.parseInt(textField.getText()); binaryTree.deleteNode(val); drawTree(); }); VBox vBox = new VBox(textField, addButton, deleteButton); VBox.setMargin(textField, new Insets(10)); VBox.setMargin(addButton, new Insets(10)); VBox.setMargin(deleteButton, new Insets(10)); BorderPane root = new BorderPane(); root.setCenter(gc.getCanvas()); root.setBottom(vBox); primaryStage.setScene(new Scene(root, 600, 400)); primaryStage.setTitle("Binary Tree"); primaryStage.show(); } private GraphicsContext createCanvas(Stage stage) { Canvas canvas = new Canvas(600, 400); GraphicsContext gc = canvas.getGraphicscontext2D(); gc.setFill(Color.WHITE); gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); stage.setOnCloseRequest(e -> { Platform.exit(); System.exit(0); }); return gc; } private void drawTree() { gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight()); // 画二叉树的图形 // ... }}
drawtreee方法