我有一個奇怪的問題關於下面的代碼。我編寫代碼來搜索樹。一棵樹把兩樣東西放在一起!
public TreeNode<City> search(City parent, TreeNode<City> t){
if (t.getCity().equals(parent)) {
System.out.println("ccc");
return t;
}
else if (t.hasLeftChild()){
System.out.println("bbb");
search(parent,t.getLeftChild());
}
else if(t.hasNextSibling()){
System.out.println("aaa");
search(parent,t.getNextSibling());
}
return null;
}
奇怪的是:假設我的樹中只有一個元素。然後,當我嘗試將一個元素添加到我的樹中時,該方法正常工作。但是,當有兩個元素(一個父母和一個孩子),並且我搜索孩子時,屏幕上顯示的是「ccc」,代碼返回的內容爲空!
我想沒人會遇到那種愚蠢的情況。請幫幫我!
這裏沒有足夠的代碼來解決這個問題。請包括語言,'TreeNode'的實現,以及你期望看到的輸出。 – recursive 2011-05-25 21:50:09