private static bool equals( this ITree tree, ITree target ) { if ( tree == null || target == null ) return false; string treeText = tree.Text; string targetText = target.Text; if ( ( treeText == null ) != ( targetText == null ) ) return false; if ( treeText == null ) return tree.Type == target.Type; return ( treeText == targetText ) && ( tree.Type == target.Type ); }
private static IEnumerable<ITree> DoWorkForFindAll( ITree nodeToSearch, ITree target, bool partialMatch ) { if ( ( partialMatch && nodeToSearch.equalsTreePartial( target ) ) || ( !partialMatch && nodeToSearch.equalsTree( target ) ) ) { yield return nodeToSearch; } for ( int i = 0; i < nodeToSearch.ChildCount; i++ ) { ITree child = nodeToSearch.GetChild( i ); if ( child != null ) { foreach ( ITree tree in DoWorkForFindAll( child, target, partialMatch ) ) yield return tree; } } }
private static bool equalsTree( this ITree tree, ITree target ) { // if either tree is nil, then they are equal only if both are nil bool treeNil = ( ( tree == null ) || ( tree.IsNil && tree.ChildCount == 0 ) ); bool targetNil = ( ( target == null ) || ( target.IsNil && target.ChildCount == 0 ) ); if ( treeNil || targetNil ) return ( treeNil == targetNil ); if ( tree.ChildCount != target.ChildCount ) return false; // check roots first if ( !tree.equals( target ) ) return false; // if roots match, do a full list match on children. // we know tree and target have the same ChildCount. int childCount = tree.ChildCount; for ( int i = 0; i < childCount; i++ ) { if ( !tree.GetChild( i ).equalsTree( target.GetChild( i ) ) ) { return false; } } return true; }
/** Walk the tree looking for all subtrees. Return * an enumeration that lets the caller walk the list * of subtree roots found herein. */ public static IEnumerable<ITree> findAllPartial( this ITree tree, ITree node ) { return DoWorkForFindAll( tree, node, true ); }
private static bool equalsTreePartial( this ITree tree, ITree target ) { // an empty tree is always a subset of any tree if ( target == null || target.IsNil ) return true; if ( target.ChildCount > tree.ChildCount ) return false; // check roots first if ( !tree.equals( target ) ) return false; // if roots match, do a full list partial test on children int childCount = Math.Min( tree.ChildCount, target.ChildCount ); for ( int i = 0; i < childCount; i++ ) { if ( !tree.GetChild( i ).equalsTreePartial( target.GetChild( i ) ) ) { return false; } } return true; }