簡體   English   中英

BST相等性檢查

[英]BST equality check

我有一種方法可以檢查BST的一個節點是否等於另一棵樹的另一個節點(在輸入內部給出)-與結構無關。

這是我到目前為止所擁有的,但似乎我缺少了一些東西。

public boolean sameValues(BSTInterface<T> other) 
{
    if(other == null && this != null)
        return false;
    else if(other.size() == 0 && this.size() == 0)
        return true;

    Object temp = null;
    Object temp2 = null;
    while(other.preorderIterator().hasNext() && this.preorderIterator().hasNext())
    {
        temp = other.preorderIterator().next();
        temp2 = this.preorderIterator().next();

        if(temp.equals(temp2))
            return true;
        else
            return false;
    }   

    return false;
}

有誰知道更好的方法嗎? 謝謝。

由於多余的return false您的while()循環在第一次迭代中終止。 它應該看起來像:

    while(other.preorderIterator().hasNext() && this.preorderIterator().hasNext())
    {
        temp = other.preorderIterator().next();
        temp2 = this.preorderIterator().next();

        if(temp.equals(temp2))    // same data found?
            return true;          // return success
        // else                   // do not return! continue!
        //    return false;
    }   

    return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM