Skip to content

Instantly share code, notes, and snippets.

@AwesomeZaidi
Created May 4, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AwesomeZaidi/1daae1fbd57b62a2a6f4e2a07132119f to your computer and use it in GitHub Desktop.
Save AwesomeZaidi/1daae1fbd57b62a2a6f4e2a07132119f to your computer and use it in GitHub Desktop.
Searching for node in tree recursively
def _find_node_recursive(self, item, node):
"""Return the node containing the given item in this binary search tree,
or None if the given item is not found. Search is performed recursively
starting from the given node (give the root node to start recursion).
TODO: Best case running time: ??? under what conditions?
TODO: Worst case running time: ??? under what conditions?"""
# Check if starting node exists
if node is None:
# Not found (base case)
return None
# TODO: Check if the given item matches the node's data
elif ...:
# Return the found node
return node
# TODO: Check if the given item is less than the node's data
elif ...:
# TODO: Recursively descend to the node's left child, if it exists
return ...
# TODO: Check if the given item is greater than the node's data
elif ...:
# TODO: Recursively descend to the node's right child, if it exists
return ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment