比较两棵树的等于方法

发布于 2024-10-24 03:26:42 字数 559 浏览 4 评论 0原文

只是另一个小问题。我还必须为 IntTree 类创建一个 equals 方法,该方法循环遍历两棵树并比较节点。如果树中的所有值都相等,则返回 true,否则返回 false。这是我到目前为止的代码:

private boolean equals(IntTreeNode node1, IntTreeNode node2){
    if ((node1 != null) || (node2 != null)){
        if (node1.equals(node2)){
            equals(node1.left, node2.left);
            equals(node1.right, node2.right);
            return node1.equals(node2);
        }
    }
    return false;
}

当我在驱动程序上调用此方法来比较两棵完全相同的树(blah1.equals(blah2))时,我得到错误。然而,当我打电话给 blah1.equals(blah1) 时,我明白了...... 我也不确定我的退货声明是否正确

just another slight problem. I must also create an equals method for my IntTree class which cycles through two tree's and compares the nodes. If all of the values in the tree are equal, then it returns true, otherwise it returns false. Here is the code I have so far:

private boolean equals(IntTreeNode node1, IntTreeNode node2){
    if ((node1 != null) || (node2 != null)){
        if (node1.equals(node2)){
            equals(node1.left, node2.left);
            equals(node1.right, node2.right);
            return node1.equals(node2);
        }
    }
    return false;
}

When I call this method on my Driver program to compare two tree's that are exactly alike (blah1.equals(blah2)), I get false. However, when I call blah1.equals(blah1) I get true...
I'm not sure if my return statement is correct either

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乙白 2024-10-31 03:26:45

我冒昧地猜测您没有重写 IntTreeNode 的 equals 方法或者没有正确完成。

我会这样做:

public boolean equals(Node other) {
    if (other == null ) return false;

    boolean valuesEqual = this.value == other.value;
    boolean leftEquals = this.left == null ? other.left == null : this.left.equals(other.left);
    boolean rightEquals = this.right == null ? other.right == null : this.right.equals(other.right);

    return valuesEqual && leftEquals && rightEquals;
}

然后要比较两棵树,您可以调用 rootNode.equals(otherRootNode)

I'm going to hazard a guess that you haven't overridden the equals method of IntTreeNode or haven't done it correctly.

I would do that something like this:

public boolean equals(Node other) {
    if (other == null ) return false;

    boolean valuesEqual = this.value == other.value;
    boolean leftEquals = this.left == null ? other.left == null : this.left.equals(other.left);
    boolean rightEquals = this.right == null ? other.right == null : this.right.equals(other.right);

    return valuesEqual && leftEquals && rightEquals;
}

And then to compare the two trees you can just call rootNode.equals(otherRootNode)

金橙橙 2024-10-31 03:26:45
private boolean equals(IntTreeNode node1, IntTreeNode node2) {
    if (node1 == node2) {
        return true;
    }

    if (node1 == null || !node1.equals(node2)) {
        return false;
    }

    return equals(node1.left, node2.left) && equals(node1.right, node2.right);
}
private boolean equals(IntTreeNode node1, IntTreeNode node2) {
    if (node1 == node2) {
        return true;
    }

    if (node1 == null || !node1.equals(node2)) {
        return false;
    }

    return equals(node1.left, node2.left) && equals(node1.right, node2.right);
}
挥剑断情 2024-10-31 03:26:44

为什么要执行两个 equals 而不处理结果?

我会改变内在,如果只是

return equals(node1.left, node2.left) && equals(node1.right, node2.right); 

Why do you do two equals without handling the result?

I would change the inner if by just

return equals(node1.left, node2.left) && equals(node1.right, node2.right); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文