Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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/0/search/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 在AVL树中迭代查找节点_Java_Search - Fatal编程技术网

Java 在AVL树中迭代查找节点

Java 在AVL树中迭代查找节点,java,search,Java,Search,有人知道我如何迭代搜索AVL树中的特定节点吗?我的意思是,树高的复杂性。我写了一些代码,但实际上我不知道如何迭代搜索节点(递归搜索不会有问题): public class AuDTreeAn只是一种特殊的二叉搜索树,您可以使用相同的方法: 我们首先检查根节点。如果树为空,则我们正在搜索的密钥在树中不存在。否则,如果键等于根的键,则搜索成功并返回节点。如果键小于根的键,则搜索左子树。类似地,如果键大于根的键,我们将搜索正确的子树。重复此过程,直到找到键或剩余子树为空。如果在到达空子树之前未找到搜索

有人知道我如何迭代搜索AVL树中的特定节点吗?我的意思是,树高的复杂性。我写了一些代码,但实际上我不知道如何迭代搜索节点(递归搜索不会有问题):

public class AuDTreeAn只是一种特殊的二叉搜索树,您可以使用相同的方法:

我们首先检查根节点。如果树为空,则我们正在搜索的密钥在树中不存在。否则,如果键等于根的键,则搜索成功并返回节点。如果键小于根的键,则搜索左子树。类似地,如果键大于根的键,我们将搜索正确的子树。重复此过程,直到找到键或剩余子树为空。如果在到达空子树之前未找到搜索的键,则该键在树中不存在

对于您的实现,它看起来像这样:

public boolean contains(E value) {
    Node current = root;

    while (current != null) {
        int comparison = value.compareTo(current.value);
        if (comparison == 0) {
            return true;
        } else if (comparison < 0) {
            current = current.left;
        } else { //comparison > 0
            current = current.right;
        }
    }

    return false;
}
公共布尔包含(E值){
节点电流=根;
while(当前!=null){
int comparison=value.comparieto(当前值);
如果(比较==0){
返回true;
}否则如果(比较<0){
current=current.left;
}else{//比较>0
current=current.right;
}
}
返回false;
}

谢谢,现在我知道了:)
public boolean contains(E value) {
    Node current = root;

    while (current != null) {
        int comparison = value.compareTo(current.value);
        if (comparison == 0) {
            return true;
        } else if (comparison < 0) {
            current = current.left;
        } else { //comparison > 0
            current = current.right;
        }
    }

    return false;
}