如何打印二叉树?
如何发送(node.data)SortTree
类到TreePrinter
然后用于打印树。如何打印二叉树?
import javax.swing.tree.TreeNode; public class SortTree {
static Node root;
TreePrinter type =new TreePrinter();
class Node<A extends Comparable>{
int data;
Node left, right;
Node(int d) {
data = d;
left = null;
right = null;
}
}
Node sortedArrayToBST(int arr[], int start, int end) {
if (start > end) {
return null;
}
int mid = (start + end)/2;
Node node = new Node(arr[mid]);
node.left = sortedArrayToBST(arr, start, mid - 1);
node.right = sortedArrayToBST(arr, mid + 1, end);
return node;
}
void preOrder(Node node) {
if (node == null) {
return;
}
//System.out.print(node.data + " ");
preOrder(node.left);
preOrder(node.right);
}
}
这是TreePrinter类:分类为序帮忙送(node.data)
到treeprinter
类,然后键入树
import java.io.IOException; import java.io.OutputStreamWriter;
public class TreePrinter {
public static class Node<T extends Comparable<T>> {
T value;
Node<T> left, right;
public void insertToTree(T v) {
if (value == null) {
value = v;
return;
}
if (v.compareTo(value) < 0) {
if (left == null) {
left = new Node<T>();
}
left.insertToTree(v);
} else {
if (right == null) {
right = new Node<T>();
}
right.insertToTree(v);
}
}
public void printTree(OutputStreamWriter out) throws IOException {
if (right != null) {
right.printTree(out, true, "");
}
printNodeValue(out);
if (left != null) {
left.printTree(out, false, "");
}
}
private void printNodeValue(OutputStreamWriter out) throws IOException {
if (value == null) {
out.write("<null>");
} else {
out.write(value.toString());
}
out.write('\n');
}
private void printTree(OutputStreamWriter out, boolean isRight, String indent) throws IOException {
if (right != null) {
right.printTree(out, true, indent + (isRight ? " " : " | "));
}
out.write(indent);
if (isRight) {
out.write("┌");
} else {
out.write("└");
}
out.write("────");
printNodeValue(out);
if (left != null) {
left.printTree(out, false, indent + (isRight ? " | " : " "));
}
}}}
节点:
回答:
我建议你实现一个toString,而不是这TreePrinter。
我稍微更改了Node类,将它移到了SortTree之外,导致代码可用于https://github.com/johanwitters/stackoverflow-tree-printer。
节点的实现是在这里:
package com.johanw.stackoverflow.tree; import com.johanw.stackoverflow.util.Helper;
public class Node<A extends Comparable>{
private static int AMOUNT_INDENT = 3;
private int data;
private Node left, right;
public Node(int d) {
data = d;
left = null;
right = null;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void indent(StringBuilder builder, int indent) {
builder.append(Helper.repeat(indent * (AMOUNT_INDENT + 1), " "));
}
public void newLine(StringBuilder builder) {
builder.append(System.lineSeparator());
}
public String toString(int indent) {
StringBuilder builder = new StringBuilder();
builder.append(data);
newLine(builder);
if (left != null) {
indent(builder, indent);
builder.append("└" + Helper.repeat(AMOUNT_INDENT, "─") + left.toString(indent + 1));
}
if (right != null) {
indent(builder, indent);
builder.append("└" + Helper.repeat(AMOUNT_INDENT, "─") + right.toString(indent + 1));
}
return builder.toString();
}
@Override
public String toString() {
return toString(0);
}
}
下面的单元测试给出了给定的树结构的底部输出:
public class TestSortTree { @Test
public void test() {
Node node = new Node(1);
Node left = new Node(2);
Node leftLeft = new Node(22);
Node leftRight = new Node(23);
Node leftRightLeft = new Node(24);
left.setLeft(leftLeft);
leftRight.setLeft(new Node(39));
left.setRight(leftRight);
node.setLeft(left);
node.setRight(new Node(3));
System.out.println(node.toString());
}
}
我希望这有助于
回答:
为了能够得到点头e.data TreePrinter
。我将添加以下代码
class TreePrinter<T extends Comparable<T>>{ Node<T extends Comparable<T>> root;
public TreePrinter(){
root = new Node<T extends Comparable<T>>();
}
public void insertToTree(T v) {
root.insertToTree(v);
}
现在会很容易打印使用的算法从这个职位的树:
How to print binary tree diagram?
我会通过进行以下修改将数据发送到Node sortedArrayToBST(int arr[], int start, int end)
在SortTree
void sortedArrayToBST(int arr[], int start, int end) { if (start > end) {
return;
}
int mid = (start + end)/2;
type.insertToTree(arr[mid]);
node.left = sortedArrayToBST(arr, start, mid - 1);
node.right = sortedArrayToBST(arr, mid + 1, end);
}
我还没有试过运行此所以可能需要一些调试,但我认为这个基本想法是可靠的。希望这可以帮助!
以上是 如何打印二叉树? 的全部内容, 来源链接: utcz.com/qa/267279.html