简体   繁体   English

如何在二叉查找树的查找操作中使用return关键字

[英]how to use return keyword in a find operation in binary search tree

This is my method to find if a particular node is there in a binary tree.Here's my method and it works fine. 这是我发现二叉树中是否存在特定节点的方法,这是我的方法,可以正常工作。

public boolean find(BinaryNode p,int x){
        if(p==null){
            return false ;
        }
        else{
            if(x==p.element){
                return true;

        }       
        else if(x<p.element){
            return find(p.left,x);
        }
        else {
            return find(p.right,x);
        }
    }


}  

My question is if I don't insert return keyword inside else if(x<p.element){ and else { I get an error as missing return statement. 我的问题是,如果不在else if(x<p.element){else {内插入return关键字else if(x<p.element){由于缺少return语句而收到错误。
Say I have a binary tree consisting of elements 5,4,6,60,25,10 . 假设我有一棵由元素5,4,6,60,25,10组成的二叉树。
So if i am searching for 10 there's a time that 因此,如果我要搜索10,

if(x==p.element){
    return true;

is satisfied because of recursive calls.Then there's a return statement to be found. 由于递归调用而感到满意,然后找到一个return语句。
If i am searching for an element that's not in tree eventually I would reach the statement 如果我正在搜索不在树中的元素,最终我会到达以下语句

if(p==null){ return false ; } if(p==null){ return false ; } ,there we find a return statement. if(p==null){ return false ; } ,我们找到一个return语句。

Therefore even I don't have the return in else if and else clauses somehow there's a way that I finally reach a return statement right?So what's wrong with not having return keyword in else if and else clauses. 因此,即使我在else ifelse子句中没有return的某种方式,也有一种方法可以使我最终到达return语句,那么在else if和else子句中没有return关键字怎么了?
Why do I have to have it there? 为什么我必须在那里?
Why can't I do it as 我为什么不能这样做

`public boolean find(BinaryNode p,int x){
        if(p==null){
            return false ;
        }
    else{
        if(x==p.element){
            return true;

        }       
        else if(x<p.element){
             find(p.left,x);
        }
        else {
             find(p.right,x);
        }
    }


}`

您需要return语句,因为else if-else语句中的find函数将在完成后返回给调用方,但是首次调用函数仍必须向调用方返回一个值

The closest to the way you want your if-else if-else clause to behave is using the ? 您希望if-else if-else子句的行为最接近的方式是使用? conditional expression: 条件表达式:

public boolean find(BinaryNode p,int x)    
{    
        if(p==null) {    
                return false ;    
        }    
        else {    
         return (x==p.element)?true:(x<p.element?find(p.left,x):find(p.right,x));
        }
}

Other option is to store the value to be returned in a local variable and only return it at the end of your method: 另一个选择是将要返回的值存储在局部变量中,并且仅在方法末尾返回它:

public boolean find(BinaryNode p,int x)
{
    boolean returnValue = false;
    if(p!=null)
    {
        if(x==p.element){
            returnValue = true;

        }       
        else if(x<p.element){
            returnValue = find(p.left,x);
        }
        else {
            returnValue = find(p.right,x);
        }
    }
    return returnValue;
}

And my favorite way, using short-circuit evaluation of logical expressions: 我最喜欢的方法是使用逻辑表达式的短路求值:

public boolean find(BinaryNode p,int x)
{
    if(p==null) return false;
    return x==p.element || (x<p.element && find(p.left,x)) || find(p.right,x);
}

Since Java's || 由于Java的|| and && operators won't evaluate their right part expression when the left part already determines their result. 当左侧部分已经确定其结果时, &&运算符将不会评估其右侧部分的表达式。 If x==p.element is true , then true will be returned without evaluation the rest of the line. 如果x==p.elementtrue ,那么将返回true而不评估其余的行。 If not, then (x<p.element && find(p.left,x)) will be evaluated following the same rule. 如果不是,则将遵循相同规则评估(x<p.element && find(p.left,x)) Note how find(p.left,x) won't be evaluated when x<p.element is false. 请注意find(p.left,x)x<p.element为false时find(p.left,x)如何不计算find(p.left,x)

Therefore even I don't have the return in else if and else clauses somehow there's a way that I finally reach a return statement right? 因此,即使我在else if和else子句中都没有返回值,以某种方式我最终还是可以到达return语句的吗?

No compiler doesn't know about it. 没有编译器对此一无所知。 Compiler doesn't know what will be value of x and p at run-time. 编译器在运行时不知道x和p的值是什么。

Compiler simply checks for all the possibilities of the return statement and there must be exit point of the method. 编译器仅检查return语句的所有可能性,并且方法的出口必须存在。

You need to provide the logic to move either in right direction or left direction of the binary tree. 您需要提供在二进制树的向右或向左移动的逻辑。

The last two else-if are not responsible to actually return the result of the find method its used just to move in the right direction of the tree. 最后两个else-if并不负责实际返回只用于沿树的正确方向移动的find方法的结果。 Ultimately final result of the the find method will come out by first two if-else clause. 最终,find方法的最终结果将由前两个if-else子句得出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM