こんにちは私はバイナリツリーのノードの右側に挿入するときにいくつかの問題を抱えています...私はなぜ例外が起こっているのかわかりません。これは、インセットのメソッドです:TreeViolationExceptionの問題
public void attachRight(BinaryTree<T> tree) {
if (right != null) {
throw new TreeViolationException();
}
if (tree != null) {
tree.parent = this;
right = tree;
}
}
これは私が唯一の挿入することができるよ私のメインクラス
パブリッククラスのテスト{
public static void main(String[] args) {
/* the tree to be built 30
* / \
* 12 16
* /\ /\
* null 1 2 5
*/
//Create binary tree and set root to 30
BinaryTree<Integer> root = new BinaryTree<Integer>();
root.makeRoot(30);
//System.out.println(root.getData()); //for verifying only
//Insert 12 -> left(30)
root.attachLeft(new BinaryTree<Integer>());
root.left.setData(12);
//Insert 16 -> right(30)
root.attachRight(new BinaryTree<Integer>());
root.right.setData(16);
//insert 1 -> right(12)
root.right.attachRight(new BinaryTree<Integer>());
root.right.right.setData(1);
//insert 2 -> left(16)
root.right.attachLeft(new BinaryTree<Integer>());
root.right.left.setData(2);
//insert 5 -> right(16)
root.right.attachRight(new BinaryTree<Integer>());
root.right.right.setData(5);
System.out.println(root.getData());
System.out.println(root.left.getData());
System.out.println(root.right.getData());
}
}
内のコードで、 30,12,16何が起こっているのか分かりません.... これは私が取得するエラーです。これはattachRightメソッドで発生します
スレッドの例外proj5.BinaryTree.attachRightで「主」proj5.TreeViolationException (BinaryTree.java:98)
TreeViolationExceptionは、私は与えられた情報からのRuntimeException
だと思います。 // insert 1 - > right(12) root.right.attachRight(new BinaryTree()); は にする必要があります// insert 1 - > right(12) root.left.attachRight(new BinaryTree ()); –