Is 'sub' a subtree of 't' beginning at the root?
AST sibling;
// the empty tree is always a subset of any tree.
if (sub == null) {
return true;
}
// if the tree is empty, return true if the subtree template is too.
if (t == null) {
if (sub != null) return false;
return true;
}
// Otherwise, start walking sibling lists. First mismatch, return false.
for (sibling = t;
sibling != null && sub != null;
sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) {
// as a quick optimization, check roots first.
if (sibling.getType() != sub.getType()) return false;
// if roots match, do full match test on children.
if (sibling.getFirstChild() != null) {
if (!isSubtree(sibling.getFirstChild(), sub.getFirstChild())) return false;
}
}
return true;