Created
May 4, 2019 17:24
-
-
Save AwesomeZaidi/1daae1fbd57b62a2a6f4e2a07132119f to your computer and use it in GitHub Desktop.
Searching for node in tree recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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