1 · Learn and explore
Study this example, identify inputs and outputs, then trace the key variables. Run and adapt it in Python 3.
class Node:
def __init__(self, value):
self.value, self.left, self.right = value, None, None
def insert(root, value):
if root is None: return Node(value)
if value < root.value: root.left = insert(root.left, value)
elif value > root.value: root.right = insert(root.right, value)
return root
def contains(root, target):
while root is not None:
if target == root.value: return True
root = root.left if target < root.value else root.right
return False
root = None
for n in (8, 3, 10, 1, 6): root=insert(root,n)
