Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.



Question

In algorithms/python, implement the search method below for BinarySearchTree class:

class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class BinarySearchTree:        
    def __init__(self):            
        self.root_node = None
...
...

    # method returns True if data found, False otherwise
    def search(self, data):
        #### IMPLEMENT THE BODY OF THIS METHOD #########

Answer
    # method returns True if data found, False otherwise
    def search(self, data):
        current = self.root_node
        while current:
            if data == current.data:
                return True
            elif data < current.data:
                current = current.left_child
            else:
                current = current.right_child
        return False

Question

In algorithms/python, implement the search method below for BinarySearchTree class:

class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class BinarySearchTree:        
    def __init__(self):            
        self.root_node = None
...
...

    # method returns True if data found, False otherwise
    def search(self, data):
        #### IMPLEMENT THE BODY OF THIS METHOD #########

Answer
?

Question

In algorithms/python, implement the search method below for BinarySearchTree class:

class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class BinarySearchTree:        
    def __init__(self):            
        self.root_node = None
...
...

    # method returns True if data found, False otherwise
    def search(self, data):
        #### IMPLEMENT THE BODY OF THIS METHOD #########

Answer
    # method returns True if data found, False otherwise
    def search(self, data):
        current = self.root_node
        while current:
            if data == current.data:
                return True
            elif data < current.data:
                current = current.left_child
            else:
                current = current.right_child
        return False
If you want to change selection, open document below and click on "Move attachment"

pdf

owner: kkhosravi - (no access) - PYTHON_DATA_STRUCTURES_AND_ALGORITHMS.pdf, p160

Summary

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

Details

No repetitions


Discussion

Do you want to join discussion? Click here to log in or create user.