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

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类,然后键入树:

最佳答案

我建议您实现一个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());
    }
}


java - 如何打印二叉树?-LMLPHP

我希望这有帮助

09-11 18:09