I am working on implementing an insert function in BST. When I print out the bst out, there are two true and two false. It supposed to be all true. I do not know what have i done wrong. I would appreciate if anyone can help me
class BTNode: def __init__(self, value, left, right): self.value = value self.left = left self.right = right class BST: def __init__(self): # reference to root node - do not modify self.root = None def insert(self, value): '''Takes a numeric value as argument. Inserts value into tree, maintaining correct ordering. Returns nothing. recursive, may use helper function''' if self.root is None: self.root = BTNode(value, None, None) else: self.insert_helper(value, self.root) # self.root = node in the parameter def insert_helper(self, value, current_node): if value < current_node.value: if current_node.left is None: current_node.left = BTNode(value, None, None) else: self.insert_helper(value, current_node.left) elif value > current_node.value is None: if current_node.right: current_node.right = BTNode(value, None, None) else: self.insert_helper(value, current_node.right) def search(self, value): if self.root: value_found = self.search_helper(value, self.root) if value_found: return True return False else: return None def search_helper(self, value, cur_node): if value > cur_node.value and cur_node.right: return self.search_helper(value, cur_node.right) elif value < cur_node.value and cur_node.left: return self.search_helper(value, cur_node.left) if value == cur_node.value: return True bst = BST() for value in [4, 2, 5, 3, 1, 6, 7]: bst.insert(value) print(bst.search(1)) print(bst.search(2)) print(bst.search(6)) print(bst.search(7))
Output:True
True
False
False