乐闻世界logo
搜索文章和话题

How to print binary tree diagram in Java?

1个答案

1

In Java, printing a graphical representation of a binary tree can be achieved through multiple approaches. Below, I will provide a common method that utilizes recursion for level-order traversal and prints node values at each level.

Define the Binary Tree Node: First, define a TreeNode class containing an integer value and two references to its child nodes.

Implement Level-Order Traversal: Use a queue to implement level-order traversal. The queue initially holds the root node, and nodes are processed level by level, with non-null child nodes added to the queue.

Print Nodes: To make the output visually clear for the tree structure, nodes at each level can be represented with indentation or prefixes, and child nodes can be offset relative to their parent nodes.

Below is a simple implementation example:

java
import java.util.*; class TreeNode { int value; TreeNode left; TreeNode right; TreeNode(int value) { this.value = value; this.left = null; this.right = null; } } public class BinaryTreePrinter { public static void printNode(TreeNode root) { int maxLevel = BinaryTreePrinter.maxLevel(root); printNodeInternal(Collections.singletonList(root), 1, maxLevel); } private static void printNodeInternal(List<TreeNode> nodes, int level, int maxLevel) { if (nodes.isEmpty() || BinaryTreePrinter.isAllElementsNull(nodes)) return; int floor = maxLevel - level; int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0))); int firstSpaces = (int) Math.pow(2, (floor)) - 1; int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1; BinaryTreePrinter.printWhitespaces(firstSpaces); List<TreeNode> newNodes = new ArrayList<TreeNode>(); for (TreeNode node : nodes) { if (node != null) { System.out.print(node.value); newNodes.add(node.left); newNodes.add(node.right); } else { newNodes.add(null); newNodes.add(null); System.out.print(" "); } BinaryTreePrinter.printWhitespaces(betweenSpaces); } System.out.println(""); for (int i = 1; i <= endgeLines; i++) { for (int j = 0; j < nodes.size(); j++) { BinaryTreePrinter.printWhitespaces(firstSpaces - i); if (nodes.get(j) == null) { BinaryTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1); continue; } if (nodes.get(j).left != null) System.out.print("/"); else BinaryTreePrinter.printWhitespaces(1); BinaryTreePrinter.printWhitespaces(i + i - 1); if (nodes.get(j).right != null) System.out.print("\\"); else BinaryTreePrinter.printWhitespaces(1); BinaryTreePrinter.printWhitespaces(endgeLines + endgeLines - i); } System.out.println(""); } printNodeInternal(newNodes, level + 1, maxLevel); } private static void printWhitespaces(int count) { for (int i = 0; i < count; i++) System.out.print(" "); } private static int maxLevel(TreeNode node) { if (node == null) return 0; return Math.max(BinaryTreePrinter.maxLevel(node.left), BinaryTreePrinter.maxLevel(node.right)) + 1; } private static boolean isAllElementsNull(List<TreeNode> list) { for (Object object : list) { if (object != null) return false; } return true; } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); root.right.left = new TreeNode(6); root.right.right = new TreeNode(7); printNode(root); } }

This code defines a simple binary tree and prints a graphical representation using the printNode method. The position of each node aims to maintain a two-dimensional structure to visually display the tree's hierarchy and structure.

2024年6月29日 12:07 回复

你的答案