Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在二叉搜索树中查找具有当前节点值的下一个值的节点_Java_Binary Search Tree - Fatal编程技术网

Java 在二叉搜索树中查找具有当前节点值的下一个值的节点

Java 在二叉搜索树中查找具有当前节点值的下一个值的节点,java,binary-search-tree,Java,Binary Search Tree,我有BTNode的BST,每个都有两个数字,我有以下字段: BTNode root:指向树根的指针 BTNode current:指向当前节点的指针 我想编写一个方法Next(),使当前点指向具有当前节点值Next值的节点 以下是我迄今为止所做的工作: public boolean Next() { // List<E> tempList = new ArrayList<E>(); // Creating new list (I defin

我有
BTNode
的BST,每个都有两个数字,我有以下字段:

BTNode root
:指向树根的指针

BTNode current
:指向当前节点的指针

我想编写一个方法Next(),使当前点指向具有当前节点值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());
        }
    }
public boolean Next()
{
//List templast=new ArrayList();//创建新列表(我在类的开头定义了这个)
E tempValue=current.getElement();//获取当前元素的值
makeSortedList(root)//
E[]tempArray=(E[])templast.toArray();//将列表转换为数组
int index=Arrays.binarySearch(tempArray,tempValue);//查找当前节点值在数组中的位置
if(index>=count)//count是树中的节点数
{
E targetValue=tempArray[index+1];//将目标值存储在临时变量中
search(targetValue);//此方法搜索具有targetValue的节点,并将该节点作为当前节点
返回true;
}
否则返回false;
}
//此方法获取树的值并将它们放入排序列表中
私有void makeSortedList(BTNode myNode)
{
if(myNode!=null)
{
makeSortedList(myNode.getLeft());
add(myNode.getElement());
makeSortedList(myNode.getRight());
}
}

你能帮我写这个方法吗?

你检查过数组返回的索引了吗。binarySearch()是你想要的吗?
在调用元素之前,还应该对元素进行排序。从您的代码示例中还不清楚在数组中找不到值时如何处理这种情况。假设它总是在数组中,那么为什么要检索@index+1的值呢?

你的makeSortedList根本不进行排序。我尝试使用inorder traversalincompatible类型将其排序为“树排序技术”——找到java.lang.Object[],但期望E[]最简单的尝试是有一个基本的cast-
(E[])templast.toArray()
。这可能会给你你所需要的。或者,使用
.toArray(新的E[0])
方法。确定修复了该问题。现在我在int index=Collections.binarySearch(tempArray,tempValue)处出现了一个错误;我编辑了代码,这样当值不在数组中时它就可以处理了。顺便说一句,当我检查代码时,当前值仍然是相同的并且没有改变,也许makeSortedList()并没有真正创建一个排序列表?