Java 使用递归将二叉搜索树图存储到数组中并打印出来
我已经建立了一个二进制搜索树。我存储在树中的基本数据类型是integer。我尝试将其存储在二维字符数组中,然后按下图所示打印出来(数字表示行号和列号,我不需要打印,请忽略“-”符号,我只使用它来指示确切位置) 12号需要存储在位置[0][8],第一行的中间。 4号店在[2][4],14号店在[2][12],5号店在[4][2],9号店在[4][9]等等 第1行是第二行,“/”位于位置[1][6],而“\”位于位置[1][10]等。它们也位于两个数字之间的中间 下面是我的代码Java 使用递归将二叉搜索树图存储到数组中并打印出来,java,exception-handling,binary-search-tree,Java,Exception Handling,Binary Search Tree,我已经建立了一个二进制搜索树。我存储在树中的基本数据类型是integer。我尝试将其存储在二维字符数组中,然后按下图所示打印出来(数字表示行号和列号,我不需要打印,请忽略“-”符号,我只使用它来指示确切位置) 12号需要存储在位置[0][8],第一行的中间。 4号店在[2][4],14号店在[2][12],5号店在[4][2],9号店在[4][9]等等 第1行是第二行,“/”位于位置[1][6],而“\”位于位置[1][10]等。它们也位于两个数字之间的中间 下面是我的代码 public cla
public class MainClass {
public static void main(String[] args) {
//level represents row number;
// start indicates the column I am going to
//store number in, and end is a fixed column number
// BinarySearchTree is a BinaryTree type instance,
// I already story integers on it and follow with the format
// of binary search trees, and I did tested it.
int level=0; int start=0; int end=80;
BinaryTree.plot(BinarySearchTree, level, start, end);
}
private static class BinaryTree {
private BinaryNode root;
static char[][] offset = new char [10][20];
public BinaryTree(){
root = null;
}
public BinaryTree(Object x){
root = new BinaryNode(x);
}
public boolean isEmpty(){
return root == null;
}
public Object getRootobj() throws BinaryTreeException{
if(root == null)
throw new BinaryTreeException("Empty Tree");
else
return root.element;
}
public BinaryTree getLeft() throws BinaryTreeException{
if(root == null)
throw new BinaryTreeException("Empty Tree");
else {
BinaryTree t = new BinaryTree();
t.root = root.left;
return t;
}
}
public BinaryTree getRight() throws BinaryTreeException{
if(root == null)
throw new BinaryTreeException("Empty Tree");
else {
BinaryTree t = new BinaryTree();
t.root = root.right;
return t;
}
}
public static void plot(BinaryTree t, int level, int start, int end){
if(!t.isEmpty()){
plot(t.getLeft(), level+2, start/2, end/2);
String string = Integer.toString((Integer)t.getRootobj());
for(char c: string.toCharArray())
offset[level][start++]=c;
if(!(t.getLeft().isEmpty()))
offset[++level][start/4*3] = '/';
if(!(t.getRight().isEmpty()))
offset[++level][((start+end)/2+start)/2] = '\\';
plot(t.getRight(), level+2, end/2, end);
}
for(int i = 0; i<10; i++){
for(int j= 0; j<20; j++)
System.out.print(offset[i][j]);
}
}
}
private static class BinaryNode {
Object element;
BinaryNode left,right;
BinaryNode() {
this(0);
}
BinaryNode(Object e) {
this(e, null, null);
}
BinaryNode(Object e, BinaryNode ln, BinaryNode m){
element=e;
left=ln;
right=m;
}
}
}
public类MainClass{
公共静态void main(字符串[]args){
//级别表示行数;
//开始表示我要访问的列
//将编号存储在中,并且结束是固定的列编号
//BinarySearchTree是BinaryTree类型的实例,
//我已经在上面写了一个整数的故事,并遵循了格式
//我做了测试。
整数级=0;整数开始=0;整数结束=80;
绘图(BinarySearchTree、level、start、end);
}
私有静态类二进制树{
私有二进制节点根;
静态字符[][]偏移量=新字符[10][20];
公共二叉树(){
root=null;
}
公共二叉树(对象x){
root=新的二进制节点(x);
}
公共布尔值为空(){
返回root==null;
}
公共对象getRootobj()抛出BinaryTreeException{
if(root==null)
抛出新的BinaryTreeException(“空树”);
其他的
返回root.element;
}
public BinaryTree getLeft()抛出BinaryTreeException{
if(root==null)
抛出新的BinaryTreeException(“空树”);
否则{
BinaryTree t=新的BinaryTree();
t、 root=root.left;
返回t;
}
}
public BinaryTree getRight()抛出BinaryTreeException{
if(root==null)
抛出新的BinaryTreeException(“空树”);
否则{
BinaryTree t=新的BinaryTree();
t、 root=root.right;
返回t;
}
}
公共静态无效图(二叉树t、int-level、int-start、int-end){
如果(!t.isEmpty()){
绘图(t.getLeft(),级别+2,开始/2,结束/2);
String String=Integer.toString((Integer)t.getRootobj());
for(char c:string.toCharArray())
偏移量[level][start++]=c;
if(!(t.getLeft().isEmpty())
偏移量[++level][start/4*3]='/';
if(!(t.getRight().isEmpty())
偏移量[++级别][((开始+结束)/2+开始)/2]='\\';
绘图(t.getRight(),级别+2,结束/2,结束);
}
对于(int i=0;i您的固定大小的字符数组无法处理动态大小的二进制树。仅对于给定的示例,每行需要超过20个字符!这就是异常的来源
但是,为了让您了解另一种方法——尽管需要一段时间,您还是在代码中添加了以下内容:
首先,我向BinaryNode
类添加了一个方法:
int getDepth() {
int subTreeDepth;
if (left == null && right == null) {
subTreeDepth = 0;
} else if (left == null) {
subTreeDepth = right.getDepth();
} else if (right == null) {
subTreeDepth = left.getDepth();
} else {
subTreeDepth = Math.max(left.getDepth(), right.getDepth());
}
return 1 + subTreeDepth;
}
第二,我删除了您的固定字符数组,并替换了您的二进制树中的整个绘图算法(我只是无法理解所有这些相对数组索引操作):
我得到以下输出:
12
/ \
8 14
/ \ \
5 9 34
/ / \
3 24 35
如果出现异常,请始终添加stacktrace!请包含用于创建BinarySearchTree实例的示例代码。
public void plot() {
if (root == null) {
throw new BinaryTreeException("Empty Tree");
}
int lineCount = 2 * root.getDepth() - 1;
StringBuilder[] lines = new StringBuilder[lineCount];
for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) {
lines[lineIndex] = new StringBuilder();
}
// get the right most node (which contains the largest element value)
BinaryNode rightMostNode = root;
while (rightMostNode.right != null) {
rightMostNode = rightMostNode.right;
}
// check how many characters we have to reserve for a single node element
int maxElementLength = String.valueOf(rightMostNode.element).length();
plot(root, 0, 0, maxElementLength, lines);
for (StringBuilder singleLine : lines) {
System.out.println(singleLine.toString());
}
}
private void plot(BinaryNode subTreeRoot, int offset, int lineIndex, int elementLength, StringBuilder[] lines) {
int actualOffset;
if (subTreeRoot.left == null) {
actualOffset = offset;
} else {
actualOffset = offset + (int) Math.pow(2, subTreeRoot.left.getDepth() - 1) * elementLength;
}
StringBuilder currentLine = lines[lineIndex];
String elementValue = String.valueOf(subTreeRoot.element);
for (int lineFillIndex = currentLine.length() + elementValue.length() / 2; lineFillIndex < actualOffset; lineFillIndex++) {
currentLine.append(' ');
}
currentLine.append(elementValue);
if (subTreeRoot.left != null) {
// draw connection to left sub tree
int connectPosition = (actualOffset - offset) * 3 / 4 + offset;
StringBuilder connectLine = lines[lineIndex + 1];
for (int lineFillIndex = connectLine.length(); lineFillIndex < connectPosition; lineFillIndex++) {
connectLine.append(' ');
}
connectLine.append('/');
// insert the left part of the next value line
plot(subTreeRoot.left, offset, lineIndex + 2, elementLength, lines);
}
if (subTreeRoot.right != null) {
// draw connection to right sub tree
int connectPosition = actualOffset + elementLength - elementValue.length() / 2;
if (subTreeRoot.right.left != null) {
connectPosition += (int) Math.pow(2, subTreeRoot.right.left.getDepth() - 1) * elementLength / 2;
}
StringBuilder connectLine = lines[lineIndex + 1];
for (int lineFillIndex = connectLine.length(); lineFillIndex < connectPosition; lineFillIndex++) {
connectLine.append(' ');
}
connectLine.append('\\');
// insert the right part of the next value line
plot(subTreeRoot.right, actualOffset + elementLength, lineIndex + 2, elementLength, lines);
}
}
BinaryTree binarySearchTree = new BinaryTree(
new BinaryNode(12,
new BinaryNode(8,
new BinaryNode(5,
new BinaryNode(3),
null),
new BinaryNode(9)),
new BinaryNode(14,
null,
new BinaryNode(34,
new BinaryNode(24),
new BinaryNode(35)))));
binarySearchTree.plot();
12
/ \
8 14
/ \ \
5 9 34
/ / \
3 24 35