如何打印二叉树?

时间:2017-12-27 19:58:20

标签: java printing binary-tree

如何从SortTree Class发送(node.data)到TreePrinter然后用于打印A树。

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类:

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 ? " |      " : "      "));
}
}}}

节点按预先排序,将(node.data)发送到treeprinter类的任何帮助然后键入树:

2 个答案:

答案 0 :(得分:0)

我建议你实现一个toString,而不是这个TreePrinter。

我稍微更改了您的Node类,将其移到SortTree之外,从而导致代码可以从https://github.com/johanwitters/stackoverflow-tree-printer获得。

Node的实现在这里:

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());
    }
}

enter image description here

我希望这会有所帮助

答案 1 :(得分:0)

为了能够在TreePrinter中接收node.data。我会添加以下代码

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);
}

我没有尝试过运行这个,所以可能需要一些调试,但我认为潜在的想法是可靠的。希望这有帮助!