以後有什麼@hyde告訴我,這是我做的:getNumberOfInteriorNodes二叉樹的Java
Node<E> current = root;
int count = 0;
public int getNumberOfInteriorNodes() {
if (current == null || (current.left == null && current.right == null)) {
return count;
}
else {
if (current.right != null) {
Node<E> tmp = current;
current = current.right;
count += getNumberOfInteriorNodes();
current = tmp;
}
if (current.left != null) {
Node<E> tmp = current;
current = current.left;
count += getNumberOfInteriorNodes();
current = tmp;
}
return count + 1;
}
}
下面是我的測試方法是什麼樣子:
public static void testGetNumberOfInteriorNodes() {
BinarySearchTree<Integer> t;
t = new BinarySearchTree<Integer>();
Assert.assertEquals(0, t.getNumberOfInteriorNodes());
t.add(2);
Assert.assertEquals(0, t.getNumberOfInteriorNodes());
t.add(1);
Assert.assertEquals(1, t.getNumberOfInteriorNodes());
t.add(5);
Assert.assertEquals(1, t.getNumberOfInteriorNodes());
t.add(4);
Assert.assertEquals(2, t.getNumberOfInteriorNodes());
t.add(3);
Assert.assertEquals(3, t.getNumberOfInteriorNodes());
t.add(6);
Assert.assertEquals(3, t.getNumberOfInteriorNodes());
}
我的測試失敗,在第三斷言與錯誤。計數永遠不會超過零。以下是錯誤我得到:
Failure: junit.framework.AssertionFailedError: expected:<1> but was:<0>
任何進一步的幫助,將不勝感激。
如果一個節點沒有任何子節點,它將是* leaf *節點,而不是* interior *節點。 – 2013-04-09 16:44:52
您在每次調用_getNumberOfInteriorNodes()_時將** count **分配給零。我不知道這是否會導致您的問題。 – AcId 2013-04-09 16:50:08
@HunterMcMillen不是這行代碼: 'code'if(current == null || root == null ||(current.left == null && current.right == null)){ return count; }'\ code' 如果不是,你能否提出改進建議。我一直在爲此工作很久...... – mikros0ft 2013-04-09 17:22:04