Pattern matching is your friend. Your Bst can either be Empty or a Node, so at the toplevel, your search function will be
search Empty = ...
search (Node left x right) = ...
Can an Empty tree possibly contain the target value? With a Node the target value, if present, will be either the node value (x above), in the left subtree, in the right subtree—or perhaps some combination of these.
By “return[ing] the number of its children,” I assume you mean the total number of descendants of the Bst rooted at a Node whose value is the target, which is an interesting combination of problems. You will want another function, say numChildren, whose definition uses pattern matching as above. Considerations:
- How many descendants does an
Empty tree have?
- In the
Node case, x doesn’t count because you want descendants. If only you had a function to count the number of children in the left and right subtrees …
Maybe a = Nothing | Just ato express that an element wasn't found.Treeimplementation here. There are functions calledtreeInsertandtreeElem. They can give an intuition of how to traverse the tree to implement your functions.