java - BST equality check -
i have method checks see if node of bst equal node of tree (given inside input) - regardless of structure.
this have far seems missing something.
public boolean samevalues(bstinterface<t> other) { if(other == null && != 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; }
does know better approach this? thanks.
your while()
loop terminates in first iteration due superfluous return false
. should rather like:
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 // not return! continue! // return false; } return false; }
Comments
Post a Comment