【发布时间】:2017-04-27 23:22:16
【问题描述】:
我在弄清楚如何让我的搜索函数为我的二叉树工作时遇到问题,它为根返回 true,但我不知道如何遍历树的其余部分。
public boolean contains(Object e)
{
return search(root, e);
}
private boolean search(BinaryNode n, Object e)
{
//If the current node is null,
//then the value clearly isn't found and return false.
if (n == null)
{
return false;
}
//If the node contains the search value,
//then the value is found and return true.
if(n.getValue() == e)
{
return true;
}
//If the node isn't null but also doesn't contain the search value,
//then search both the left and right subtrees
return false;
}
【问题讨论】:
-
这是一个有据可查的过程,请使用 google。
-
是的,只是谷歌。假设这是重复的post。
-
我已经在谷歌上搜索了好几个小时。该功能必须是这样的,这样它才能与我教授给我的测试程序一起运行。