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; }
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; }