Ideas on How to achieve information hiding among tree, list, and string operations when doing the TIP operation of the America's Most Wanted programming exercise. Note: The code below is very Pascal-like, but it's not debugged code. Treat it as if it were "pseudo-code". The idea here is just to give you a sense of the logic required to do this job. You need to think this through, and decide the details of how to make the procedures really work correctly in your program. ================================================================= C declarations dealing with pointer to function -- needed to allow passing a pointer to a function to another function. returnTypeOfFunction g() ; returnTypeOfFunction (*functionPtr)() ; functionPtr = &g ; ================================================================= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Set up for calling the Prune Procedure Tree nodes contain info fields that are of badGuyType. Elements of badGuyType have a key field which is a string containing the name of the bad guy. Elements of badGuyType also contain a field which is a list of strings. Each element of the list is one of the traits of the bad guy, like "has a texas accent", or "is computer literate". * CreateTree(testTree) * Create testTreeNodeInfo, an element of badGuyType containing a list with one element -- the string that you want to check for. The key field is arbitrary -- any thing you want, but you probably ought to set it to something, so the contents are predictable. * Insert testTreeNodeInfo into the testTree. * Make a Copy of the orignal badGuy-tree. Let's say you call the copy "treeCopy". * Now call Prune(treeCopy, HasTrait, testTree, postOrder) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Tree Operation Procedure Prune ( var tree : treeType; FUNCTION ShouldPrune : boolean ; testTree : treeType; order : traversalType) ; (* Here is where the code for PrunePreOrder and PruneInOrder should go *) Procedure PrunePostOrder( var parent : treeType ; var tree : treeType ; FUNCTION ShouldPrune : boolean ; testTree : treeType ) ; begin (* Procedure PrunePostOrder *) if tree <> nil then begin if tree^.left <> nil then PrunePostOrder(tree, tree^.left, ShouldPrune, testTree) ; if tree^.right <> nil then PrunePostOrder(tree, tree^.right, ShouldPrune, testTree) ; if ShouldPrune(tree^.info, testTree^.info) ; then if parent <> nil then DeleteElement(parent, tree^.info.key) else DeleteElement(tree, tree^.info.key) end ; end ; (* Procedure PrunePostOrder *) begin (* Procedure Prune *) case order of PreOrder: PrunePreOrder(nil, tree, evaluate, testTree) ; InOrder: PruneInOrder(nil, tree, evaluate, testTree) ; PostOrder: PrunePostOrder(nil, tree, evaluate, testTree) ; end ; end ; (* Procedure Prune *) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Operation on Elements of badGuyType (Check Trait calls list and string operations.) Function HasTrait ( badGuy : badGuyType ; testGuy : badGuyType ) : boolean ; var listPos : integer ; testTrait : stringType ; curTrait : stringType ; found : boolean ; begin (* Function HasTrait *) MakeNCurrent (testGuy.list, 1) ; RetrieveCurrent (testGuy.list, testTrait) ; (* changes all the characters to upper case *) testTrait := CanonicalForm(testTrait) ; listPos := 1 ; found := false ; while (not found) and ((ListSize(badGuy.list) >= listPos)) do begin MakeNCurrent (badGuy.list, listPos) ; RetrieveCurrent(badGuy.list, curTrait) ; (* changes all the characters to upper case *) curTrait := CanonicalForm(curTrait) ; if Compare (curTrait, testTrait) = Equal then found := true ; listPos := listPos + 1 ; end ; HasTrait := found ; end ; (* Function HasTrait *) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Note: since the names of bad guys are logically strings, it would be nice to modify the tree package given so that it can use our string.p strings as keys. This requires using the string Compare function in searches. The "right way" to do this is to pass in the Compare function to the tree package in a manner similar to how "CheckTrait" is passed in. Alternatively, packed arrays of characters can be used as keys, an then the "<" operator used in the tree code will work as is. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++