У меня есть двоичное дерево поиска целых чисел, включая 1,2, ..., 9. Мои методы обхода работают, я знаю, что узлы есть и в правильном порядке.двоичное дерево поиска метод findNode всегда возвращает root
Я выполнил два метода поиска: тот, который находит и возвращает узел, и тот, который вызывает этот метод, и печатает, существует или нет узел в зависимости от того, что он возвращает (null означает, что он не существует).
Метод findNode (int, BSTNode), однако возвращает возвращающий корень. Когда я сравниваю его с примером кода онлайн, это кажется правильным.
вот два метода, я называю searchPrint (INT) от основного метода на 7,5,6,1,12,15 (Обратите внимание, что 12 & 15 не существует в дереве):
//this method calls findNode(int,BSTNode) and prints a message if the value is found
// in the tree
public void searchPrint(int value)
{
System.out.print(value);
if (findNode(value, root) == null)
{
System.out.println(" does not exist in the tree.");
} else
{
System.out.println(" exists in the tree.");
}
}//end searchPrint(int) ----------------------------------------------------
//this method recursively looks for the node that contains 'value' and
//returns that node if it is found. Returns null if no nodes contain
//the value.
private BSTNode findNode(int value, BSTNode current)
{
if(current != null)
{
if (value == current.getValue())
{
System.out.print(" **Test* entering "
+ "if(value = current.value loop...** ");
return current;
} else
{
if (value < current.getValue())
{
findNode(value,current.getLeft());
} else
{
findNode(value,current.getRight());
}
}
} else
{
return null;
}
return current;
}//end findNode(int,BSTNode) -----------------------------------------------
здесь выход:
Traversals:
in-order
1
2
3
4
5
6
7
8
9
pre-order
6
2
1
4
3
5
7
9
8
post-order
1
3
5
4
2
8
9
7
6
7 **Test* entering if(value = current.value loop...** exists in the tree.
5 **Test* entering if(value = current.value loop...** exists in the tree.
6 **Test* entering if(value = current.value loop...** exists in the tree.
1 **Test* entering if(value = current.value loop...** exists in the tree.
12 exists in the tree.
15 exists in the tree.
Iv'e написано на бумаге то, что происходит, когда я искать значение, и это не имеет смысла, что она возвращает корень. Что я делаю не так?