简体   繁体   English

检查2个二叉搜索树是否相等

[英]Checking if 2 Binary search trees are equal

I'm creating a binary search tree project, and one of the questions is to create 2 trees and check if they're equal or not.我正在创建一个二叉搜索树项目,其中一个问题是创建 2 棵树并检查它们是否相等。 When I implement the method, I keep getting firstTree and secondTree are equal .当我实现该方法时,我不断得到firstTree 和 secondTree are equal Here's the relevant code:以下是相关代码:

BstTest2 firstTree = new BstTest2();
firstTree.addNode(50, "Francisco Domingo Carlos Andres Sebastián d'Anconia");
firstTree.addNode(25, "John Galt");
firstTree.addNode(15, "Hugh Akston");
firstTree.addNode(30, "Ragnar Danneskjöld");
firstTree.addNode(85, "Hank Reardan"); //implementing add method

BstTest2 secondTree = new BstTest2();
secondTree.addNode(50, "Francisco Domingo Carlos Andres Sebastián d'Anconia");
secondTree.addNode(25, "John Galt");
secondTree.addNode(15, "Hugh Akston");
secondTree.addNode(30, "Ragnar Danneskjöld");
secondTree.addNode(75, "Midas Mulligan");
secondTree.addNode(85, "Hank Reardan");

if(firstTree.isEqual(secondTree))
{
     System.out.println("firstTree and secondTree are equal");
}
else
{
     System.out.println("firstTree and secondTree are not equal");
}

isEqual and check methods for comparing the trees比较树的 isEqual 和 check 方法

public boolean isEqual(BstTest2 tree1)
{
    return check(this.rootNode, tree1.rootNode);
}
public boolean check(Node node1, Node node2)
{
    if((node1 == null) && (node2 == null))
    {
        return true;
    } 
    else if((node1 == null) || node2 != null)
    {
        return false;
    } 
    else if((node1 != null) || node2 == null)
    {
        return false;
    } 
    else
    {
        return check(node1.leftChild, node2.leftChild) && check(node1.rightChild, node2.rightChild);
    }
}

What did I do wrong in my isEqual() and check() methods that I keep getting " firstTree and secondTree are equal" when the trees are not equal?当树不相等时,我在我的 isEqual() 和 check() 方法中做错了什么,我不断得到“firstTree 和 secondTree 相等”?

You forget to check if the value of node1 and node2 are the same.您忘记检查node1node2的值是否相同。

  • If they are not the same, it means these two trees are not same.如果它们不相同,则表示这两棵树不相同。
  • If they are the same, we keep on checking if their left and right child are the same.如果它们相同,我们会继续检查它们的左右孩子是否相同。

public boolean check(Node node1, Node node2)
{
    if((node1 == null) && (node2 == null))
    {
        return true;
    } 

    // If only one node is null, it means these two trees are not the same
    // These two nodes here couldn't both be null because we check this condition earlier.
    if(node1 == null || node2 == null)
    {
        return false;
    } 

    // Check if the value of node1 and node2 are the same
    if(node1.val != node2.val)
    {
        return false;
    } 

    return check(node1.leftChild, node2.leftChild) && check(node1.rightChild, node2.rightChild);
}

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

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