运行以下代码时出现错误。我提供了评论来描述正在发生的事情。
// Node
class TreeNode {
char value;
TreeNode left;
TreeNode right;
}
// Main class
public class BinaryTree {
// Global variables
char[] preorder;
int i = 0;
// Main method runs gatherOutput
public static void main(String[] args) throws IOException {
new BinaryTree().gatherOutput();
}
// This takes a null tree as input from the gatherOutput method
// and whenever a 0 is encountered in the preorder character array
// (from a string from System.in) a new external node is created with
// a value of 0. Whenever a letter is encountered in the character
// array, a new internal node is created with that letter as the value.
//
// =====ArrayOutOfBoundsException occurs somewhere here=====
//
public TreeNode createTree(TreeNode tree) throws IOException {
if (preorder[i] == 0) {
tree = new TreeNode();
tree.value = 0;
tree.left = tree.right = null;
i++;
} else {
tree = new TreeNode();
tree.value = preorder[i];
i++;
createTree(tree.left);
createTree(tree.right);
}
return tree;
}
// Supposed to print out contents of the created binary trees.
// Just for testing purposes, but it's not working right now for some reason.
public void preorderTraversal(TreeNode tree) {
if (tree != null) {
System.out.println(tree.value + " ");
preorderTraversal(tree.left);
preorderTraversal(tree.right);
}
}
// Reads System.in for the Strings used in making the binary tree
// and is supposed to make a different binary tree for every line of input
public void gatherOutput() throws IOException {
TreeNode tree = null;
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
preorder = reader.readLine().toCharArray();
while (reader.readLine() != null) {
tree = createTree(tree);
preorderTraversal(tree);
i = 0;
}
}
}
每当我有多行输入时,都会收到ArrayIndexOutOfBounds错误。例如:
b
b
堆栈跟踪:
线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:1
在btsmall.createTree(btsmall.java:22)
在btsmall.createTree(btsmall.java:31)
在btsmall.gatherOutput(btsmall.java:53)
在btsmall.main(btsmall.java:18)
它发生在createTree方法中,但我无法查明原因。即使我只有一行输入,preorderTraversal方法似乎也没有运行,因为我从运行程序中未获得任何输出,但我不知道为什么。谁能帮我吗?
谢谢。
编辑:我对以下两种方法进行了更改,并且我不再收到ArrayIndexOutOfBounds错误。
public void createTree(TreeNode tree) throws IOException {
if (i >= preorder.length) {
i++;
} else if (preorder[i] == '0') {
tree = new TreeNode();
tree.value = '0';
tree.left = tree.right = null;
i++;
} else {
tree = new TreeNode();
tree.value = preorder[i];
i++;
createTree(tree.left);
createTree(tree.right);
}
}
public void gatherOutput() throws IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String line = null;
TreeNode tree = new TreeNode();
while ((line = reader.readLine()) != null) {
preorder = line.toCharArray();
tree = createTree(tree);
preorderTraversal(tree);
i = 0;
}
}
但是,preorderTraversal的输出将打印出一个正方形,而不是所有预排序节点的值。
最佳答案
这里发生的事情很少。最突出的是您正在读取一行,然后尝试立即读取另一行,如下所示。
preorder = reader.readLine().toCharArray();
while (reader.readLine() != null) {
您只需要阅读该行一次。例如:
String line = null;
while ((line=reader.readLine()) != null){
System.out.println(line);
preorder = line.toCharArray();
}
另外,您的支票:
if (preorder[i] == 0) {
到达行尾时,它会爆炸,因为您永远不会检查
i
是否超出数组的范围。我不确定您真正要检查的内容。