我正在實施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