Wed, 18 Jun 1902 08:00:00 GMT
Problem with recursion and binary search trees
I'm having a problem with recursion. I'm trying to create a function that will return the number of interior nodes in a binary search tree (bst) not including the root node. The function below returns the number of interior nodes including the root node but I can't figure out how to modify it so that it won't include the root node. Can anybody help me? I would appreciate it a lot. type bst = ^bstnode; bstnode = record number: integer; leftptr: bst; rightptr: bst; {returns the number of interior nodes in the binary search tree not including the root node} function interior(bst1: bst): integer; begin if bst1 = nil then interior := 0 else if (bst1^.leftptr = nil) and (bst1^.rightptr = nil) then interior := 0 else if (bst1^.leftptr <> nil) or (bst1^.rightptr <> nil) then interior := 1 + interior(bst1^.rightptr) + interior(bst1^.leftptr) end; {interior} Edwin Wong umwon...@cc.umanitoba.ca
|