我想为我的树编写一个搜索算法,但我被困住了。我使用递归函数,但递归部分有错误。您可以在调试时找到它。在树中搜索算法
private Node SeachNode(Node node, IComparable value)
{
if (node == null) return null;
int comp = value.CompareTo(node.Letter.LetterName);
if (comp == 0) return node; //Found it
if (comp < 0) SeachNode(node.Left, value); //The item must be in the left subtree
return SeachNode(node.Right, value); // The item must be in the right subtree
}
在这段代码中,我尝试在树中找到我的值。例如,我想在我的树中搜索“ACD”节点,即时从根开始搜索。如果root为null,则退出。否则,比较一下。如果root没有回答,则返回root。如果比较返回-1,检查根的左侧。但是代码并没有转到root的权利。我想这不是因为return语句。
谢谢你的帮助:)
请阅读[MCVE]指南并提供适当的样本来证明问题。到目前为止,您发布的代码没有什么特别的错误。 –