How would I be able to create common tree operations such as insert and search while passing in functions to reduce redundancy. For example, a recursive function calls itself on the left branch when the value passed in is greater than the current node. If I were able to pass in functions like insert and search, I'd be able to factor out a lot of the traversal. The main problem area I'm seeing is that both functions have different base cases. Example solutions in python would be appreciated.
def insert(n, node = root):
if node == None:
node.data = n
node.left, node.right, node.middle = None
elif node.data == n:
insert(node.middle)
elif node.data < n:
insert(right)
else:
insert(left)
def search(n, node = root):
if node == None:
return false
elif node.data == n:
return true
elif node.data < n:
search(right)
else:
search(left)