Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 BinarySearchTree查找某些值之间最近的值_Java_Binary Search Tree_Nodes - Fatal编程技术网

Java BinarySearchTree查找某些值之间最近的值

Java BinarySearchTree查找某些值之间最近的值,java,binary-search-tree,nodes,Java,Binary Search Tree,Nodes,我有一个二元搜索树,我需要得到最接近的较高值和最接近的较低值,最接近的较低值必须在5到9之间(表示高于5或低于9) 假设我有一个ID为125的节点,距离该节点最近的较高的数是127,但它也必须在5到9之间,因此ID为130的“节点”将是我正在寻找的节点 这是我正在使用的二叉树: 这就是我目前发现最近的更高点的原因: Node currentNode = null; int currentNodeID; double min = Double.MAX_VALUE; public Nod

我有一个
二元搜索树
,我需要得到最接近的较高值最接近的较低值,最接近的较低值必须在5到9之间(表示高于5或低于9)

假设我有一个ID为125的
节点,距离该节点最近的较高的数是127,但它也必须在5到9之间,因此ID为130的“节点”将是我正在寻找的节点

这是我正在使用的二叉树:

这就是我目前发现最近的更高点的原因:

Node currentNode = null;
int currentNodeID;
double min = Double.MAX_VALUE;    

public Node closestHigherValue(Node root, double target, int low, int high) {
    min = Double.MAX_VALUE;
    closestHigherHelper(root, target, low, high);

    if(currentNodeID < (int) target) return null;
    return currentNode;
}

public void closestHigherHelper(Node root, double target, int low, int high){
    if(root==null)
        return;

    if(Math.abs(root.ID - target) < min && root.ID >target){
        min = Math.abs(root.ID-target);
        currentNodeID = root.ID;
        //If between numbers
        if(root.ID >= low && root.ID <= high) currentNode = root;
    }

    if(target < root.ID){
        closestHigherHelper(root.leftChild, target, low, high);
    } else {
        closestHigherHelper(root.rightChild, target, low, high);
    }
}
这让我想起:

Searching for 125 and Closest value = 130   //Should be 130
Searching for 100 and Closest value = null   //Should be null
Searching for 120 and Closest value = 125   //Should be 125
Searching for 130 and Closest value = 125   //Should be 135 -- This one goes wrong
由于最接近的下限类似,因此无需显示该代码,我可以在稍后修复该代码时修复该代码


有什么想法吗?

我想你应该只在“边界”内更新
min
,因此:

 min = Math.abs(root.ID-target);
 currentNodeID = root.ID;
 //If between numbers
 if(root.ID >= low && root.ID <= high) currentNode = root;
min=Math.abs(root.ID目标);
currentNodeID=root.ID;
//如果在数字之间

如果(root.ID>=low&&root.ID=low&&root.ID)在边界内(在数字5和9之间)
你所说的
是什么意思?编辑问题以回答我认为你应该只更新
min
如果(root.ID>=low&&root.ID>
 min = Math.abs(root.ID-target);
 currentNodeID = root.ID;
 //If between numbers
 if(root.ID >= low && root.ID <= high) currentNode = root;
 //If between numbers
 if(root.ID >= low && root.ID <= high){
    currentNode = root; 
    min = Math.abs(root.ID-target);
    currentNodeID = root.ID;
 }