Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python binary tree search

def search(node, search_item):

    # Base case for recursion:
    # The recursion will stop if the search item has been found
    if search_item == node.data:
        return True

    # Check if the search item is greater than the node data
    # and there is another node to the right to check
    elif search_item > node.data and node.right is not None:
        return search(node.right, search_item)

    # Check if the search item is less than the node data
    # and there is another node to the left to check
    elif search_item < node.data and node.left is not None:
        return search(node.left, search_item)

    # Otherwise the search item does not exist
    else:
        return False
Source by isaaccomputerscience.org #
 
PREVIOUS NEXT
Tagged: #python #binary #tree #search
ADD COMMENT
Topic
Name
8+5 =