/** Is 't' a subtree of the tree rooted at 'this'? The siblings * of 'this' are ignored. */ public boolean equalsTreePartial(AST sub) { // the empty tree is always a subset of any tree. if (sub == null) { return true; } // check roots first. if (!this.equals(sub)) return false; // if roots match, do full list partial match test on children. if (this.getFirstChild() != null) { if (!this.getFirstChild().equalsListPartial(sub.getFirstChild())) return false; } return true; }
/** Is 'sub' a subtree of this list? * The siblings of the root are NOT ignored. */ public boolean equalsListPartial(AST sub) { AST sibling; // the empty tree is always a subset of any tree. if (sub == null) { return true; } // Otherwise, start walking sibling lists. First mismatch, return false. for (sibling = this; sibling != null && sub != null; sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) { // as a quick optimization, check roots first. if (!sibling.equals(sub)) return false; // if roots match, do partial list match test on children. if (sibling.getFirstChild() != null) { if (!sibling.getFirstChild().equalsListPartial(sub.getFirstChild())) return false; } } if (sibling == null && sub != null) { // nothing left to match in this tree, but subtree has more return false; } // either both are null or sibling has more, but subtree doesn't return true; }