私はJavaで最適なバイナリ検索ツリーの問題を実装しようとしています。私のプログラムでは、最初の行がノードの数であり、各前の行が最初の要素がノードで、2番目の要素が確率であるスペースで区切られたtxtファイルを読み込んでいます。以下は、私のプログラムを読み込み、サンプル入力である:私のプログラムで最適なバイナリ検索ツリーを実装したArrayindexOutOfBoundsException java
5
A 0.213
B 0.547
D 0.10
X 0.12
AAA 0.02
が、私はプログラムを実行するとき、私はその確率が何であるか、それが何であるかのノードを参照することができるように、印刷法を作成しようとしていますその親は何か、そして彼らの子供は何か。 1つのノードの出力例を以下に示す。
Node
Key: B
Probability: 21.3%
Parent: (null)
Left Child: A
Right Child: X
私は今に実行しています問題は、それが木の罰金のルートを読んでいるということですが、その後しばらくした後、それは私のうち指標を与えます境界。私はサイズを増減しようとしている
明らか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
、私:以下は私のプリントツリー方式のための私のコード
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(""); } }
は、これは私が自分のコードを実行したとき、私は受け付けており出力されています私はそれを行うときにまだ取得してIndexOutofBoundsExceptionです。私は問題が何であるか、ルートを読んでいることを信じています。それからリストの下に落ちて止まりません。
誰かがこの問題で私を助けることができたら、私は非常に感謝します!
EDIT: ノードを含む印刷メソッドを再構築しましたが、ArrayIndexOutofBoundsExceptionがまだ表示されています。
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の値です。ポジションは0です。キー[-1]と同じ[[-1]キーを押したときにどのような配列位置が得られますか? – Asthor
私は、それがBの右の子であるノード4に印刷することを望んでいます。しかし、私が見ているように、プログラムは可能性が高いから落ちています。したがって、ノード2で始まっていれば、ノード1を印刷してから0を返し、次にエラー – user2580