How to return nothing from a function that returning value?

大憨熊 提交于 2020-01-02 08:23:41

问题


I have a binary search tree, and I want to delete a node.

I need to get its parent, so I wrote a function:

private BSTreeNode<T> getParent(BSTreeNode<T> root, BSTreeNode<T> node) {
        if(root == null) 
            return null;
        if(node.element().lessThan(root.element())) {
            if(root.getLeft() != null && root.getLeft().element().equal(node.element())) 
                return root;
            else
                getParent(root.getLeft(), node);
        } else {
            if(root.getRight() != null && root.getRight().element().equal(node.element()))
                return root;
            else
                getParent(root.getRight(), node);
        }
    }

Unlike C/C++, Java is giving me the following error:

This method must return a result of type BSTreeNode<T>

It forces me to return a value in the last line of the function.

How can I fix it in the context of my function?


回答1:


Your function does not have a return for every possible circumstance. You have:

if (null)...

if (less than root)...
else ( if ...)
       else (no return!)

What do you return if it is not null, and goes to the final else? Nothing is returned.

You can either return getParent... in the else statement. or return null at the end of the function (not in an if or else statement)

I often see code like so to cover the event of neither if statement returning a value.

public int getAnswer()
{
    if (answer.equals("yes"))
        return 0;
    else if (answer.equals("no"))
        return 1;

    return null;
}



回答2:


You should be good with this:

private BSTreeNode<T> getParent(BSTreeNode<T> root, BSTreeNode<T> node) {
    if(root == null) 
        return null;
    if(node.element().lessThan(root.element())) {
        if(root.getLeft() != null && root.getLeft().element().equal(node.element())) 
            return root;
        else
            return getParent(root.getLeft(), node);
    } else {
        if(root.getRight() != null && root.getRight().element().equal(node.element()))
            return root;
        else
            return getParent(root.getRight(), node);
    }
}



回答3:


Add return null; statement at the end of the method . I think this statement will be unreachable in normal cases but add it just to fix the compilation error and try what will happen.

private BSTreeNode<T> getParent(BSTreeNode<T> root, BSTreeNode<T> node) {
        if(root == null) 
            return null;
        if(node.element().lessThan(root.element())) {
            if(root.getLeft() != null && root.getLeft().element().equal(node.element())) 
                return root;
            else
                getParent(root.getLeft(), node);
        } else {
            if(root.getRight() != null && root.getRight().element().equal(node.element()))
                return root;
            else
                getParent(root.getRight(), node);
        }
return null;
    }


来源:https://stackoverflow.com/questions/20622156/how-to-return-nothing-from-a-function-that-returning-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!