【发布时间】:2014-04-10 05:41:08
【问题描述】:
我有BTNode<E> 的 BST,每个都有双数,我有以下字段:
BTNode <E> root: 指向树根的指针
BTNode <E> current:指向当前节点的指针
我想写一个方法 Next() 使当前点指向具有当前节点值的下一个值的节点
这是我到目前为止所做的:
public boolean Next()
{
// List<E> tempList = new ArrayList<E>(); // Creating new list (I defined this at the begining of the class)
E tempValue = current.getElement(); // Gets the value of current element
makeSortedList(root); //
E[] tempArray = (E[]) tempList.toArray(); // Convert the list to an array
int index = Arrays.binarySearch(tempArray, tempValue); // Find the position of current node value in the array
if(index >= count) // count is no. of nodes in the tree
{
E targetValue = tempArray[index + 1]; // Store the target value in a temporary variable
search(targetValue); // This method searches for the node that has targetValue and make that node as the current node
return true;
}
else return false;
}
// This method takes the values of the tree and puts them in sorted list
private void makeSortedList(BTNode<E> myNode)
{
if(myNode != null)
{
makeSortedList(myNode.getLeft());
tempList.add(myNode.getElement());
makeSortedList(myNode.getRight());
}
}
你能帮我写这个方法吗?
【问题讨论】:
-
您的 makeSortedList 根本不进行排序。
-
我尝试使用中序遍历将其排序为“树排序技术”
-
不兼容的类型 - 找到 java.lang.Object[] 但预期为 E[]
-
最简单的尝试是有一个基本的演员表 -
(E[])tempList.toArray()。这可能会给你你需要的东西。或者,使用.toArray(new E[0])方法。 -
好的解决了这个问题。现在我有一个错误 int index = Collections.binarySearch(tempArray, tempValue);