开发者

boolean recursion part 2

开发者 https://www.devze.com 2023-02-11 02:12 出处:网络
trying to write a boolean function that returns true if \'m\' is a ancestor of the current class. \'m\' is an ancestor if it is a mom or dad, or a ancestor of 开发者_如何学Gomom or dad.

trying to write a boolean function that returns true if 'm' is a ancestor of the current class. 'm' is an ancestor if it is a mom or dad, or a ancestor of 开发者_如何学Gomom or dad.

will this get me there?

public boolean isAncestor(member m){
        if (mom == m || dad == m){
            return true;
        }
        else{
            if(isAncestor(m.mom) || isAncestor(m.dad)){
                return true;
            }
        }
        return false;
    }

thanks!


Yes, more or less. What if you get to a layer of the ancestry where mom or dad aren't known, and are null?

public boolean isAncestor(member m){
        if (m == null)
            return false;
        if (mom.equals(m) || dad.equals(m))
            return true;
        else if(isAncestor(m.mom) || isAncestor(m.dad))
            return true;
        return false;
    }


The logic will get you there, however one must take care with the equal signs.

In Java == compares for equality of instances. Odds are good that over time you will have two instances which are comparatively equal but exist in different instances. If your code has been written to prevent such a thing from occurring, then you don't need to change the == to .equals(...); but, if you didn't put in any safeguards to ensure only one instance of each "same" object exists, then you might want to change the comparison to .equals(...) and implement an custom "public boolean equals(Object other) {...}` method for the base "person" class.


If you're going to use recurrsion, you are going to need to have a stop condition. Does everyone have a mom or a dad? consider this:

public boolean isAncestor(Member m) {
     // stop condition
    if(this.mom == null && this.dad == null) return false;
    // stop condition
    if(this.mom == m || this.dad == m) return true;
    // loop condition
    return mom.isAncestor(m) || dad.isAncestor(m);
}


It might but it is not 100% correct. You should also check if mum and dad exist otherwise you might end up with NullPointerException.

Moreover the others are correct with the == usage, quite suspicious.


When does the recursion stop, if there is no relationship?


Actually, you need a way to know which two members you are comparing for relationship. You'll probably want to do something like this (untested):

/**
 * @param m1 the member we want to get ancestry from
 * @param m2 the member we presume is an ancestor
 */
public boolean isAncestor(member m1, member m2){
    if (m1 == NULL || m2 == NULL) {
        return false;
    }
    if (m1.isMom(m2) || m1.isDad(m2)){
        return true;
    }
    else if(isAncestor(m1.mom, m2) || isAncestor(m1.dad, m2)) {
        return true;
    }
    return false;
}

You'd need to write isMom and isDad to compare the relationship of m2 to m1.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号