我正在实施java中的最佳二叉搜索树问题。对于我的程序,我正在阅读一个txt文件,其中第一行是节点数量,每个前面的行由空格分隔,其中第一个元素是节点,第二个元素是概率。下面是我的程序在读取样品输入:ArrayindexOutOfBoundsException与最佳二叉搜索树实现java
5
A 0.213
B 0.547
D 0.10
X 0.12
AAA 0.02
在我的计划,我想创建一个打印方法,当我运行该程序,我可以看到它是什么节点,其概率是什么,它的父母是什么,他们的孩子是什么。
Node
Key: B
Probability: 21.3%
Parent: (null)
Left Child: A
Right Child: X
,我遇到了现在的问题是,它是阅读的树细根,但随后一段时间后,它会给我一个索引出的问题:一个节点的样本输出下面提供界限。下面是我为我打印树法码
public static void printTree(String keys[], double prob[], int root[][]){
System.out.println("");
int pos = root[1][keys.length-1];
int t=pos;
for(int i = 0; i < keys.length-1; i ++){
System.out.println("Node Key "+ pos);
System.out.println("Key: "+ keys[pos]);
System.out.println("Probability: "+ prob[pos]);
if(i ==0){
System.out.println("Parent: null");
System.out.println("Left Child: "+ keys[pos-1]);
System.out.println("Right Child: "+ keys[pos+1]);
if(root[1][pos]==t){
pos-=1;
}
}
else{
System.out.println("Parent: "+ keys[pos+1]);
System.out.println("Left Child: "+ keys[pos-1]); //where the error is occurring
System.out.println("Right Child: "+ keys[pos+1]);
pos--;
}
System.out.println("");
}
}
这是我收到的输出,当我运行代码:
Node
Key: B
B is the root
Node Key 2
Key: B
Probability: 0.547
Parent: null
Left Child: A
Right Child: D
Node Key 1
Key: A
Probability: 0.213
Parent: B
Left Child: null
Right Child: B
Node Key 0
Key: null
Probability: 0.0
Parent: A
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at OBST.printTree(OBST.java:62)
at OBST.main(OBST.java:155
很显然,我已经尝试增加和减少的大小,但我当我这样做时,我仍然在和IndexOutofBoundsException。我相信问题是,它正在读取根目录,然后它正在列表中并且不停止。
如果有人可以帮助我解决这个问题,我将非常感激!
编辑: 我重建我的打印方法,包括节点,但我仍然得到一个ArrayIndexOutofBoundsException。下面是使用一个节点类
public static void printTree(int i, int j, int pos, String side, String keys[], double prob[], int root[][], Nodes[] node){
int temp;
if(i<=j){
temp = root[i][j];
System.out.println("Node");
System.out.println("Key: " + keys[pos]);
System.out.println("Probability: "+ prob[pos]+" %");
System.out.println("Parent: "+ keys[pos]);
System.out.println(keys[temp] + " is the "+ side +" child of "+ keys[pos]);
for(int k =0; k< keys.length-1; k++){
if(keys[pos].equalsIgnoreCase(node[k].getKey())){
if(side.equalsIgnoreCase("left")){
node[i].setLeftChild(keys[temp]);
}
else{
node[i].setRightChild(keys[temp]);
}
}
System.out.println(" ");
}
constructTree(i,temp-1,temp,"left", keys, prob, root, node);
constructTree(temp+1,j,temp,"right", keys, prob,root, node);
}
这是我收到的输出更新方法:
Node
Key: B
Probability: 0.547 %
Parent: B
A is the left child of B
Node
Key: B
Probability: 0.547 %
Parent: B
X is the right child of B
Node
Key: X
Probability: 0.12 %
Parent: X
D is the left child of X
Node
Key: X
Probability: 0.12 %
Parent: X
AAA is the right child of X
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at OptimalBT.Optimal.constructTree(Optimal.java:117)
at OptimalBT.Optimal.constructTree(Optimal.java:127)
at OptimalBT.Optimal.main(Optimal.java:90)
节点键值打印为0,节点键值为pos值。因此,pos为0.当您执行键[pos-1]时,您希望得到的阵列位置与键[-1]相同吗? – Asthor
我的希望是打印到节点4,这是B的正确孩子。但是,正如我所看到的,该节目正在从可能性和走向下去。所以如果它从节点2开始,那么它会打印节点1,然后是0,然后是错误 – user2580