Rule: left subtree < node < right subtree
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, value):
node = root
while node is not None:
if value == node.value:
return True
node = node.left if value < node.value else node.right
return False
bst = None
for num in [50, 30, 70, 20, 40, 60, 80]:
bst = insert(bst, num)
print(contains(bst, 60))
print(inorder(bst))
chain = None
for num in [20, 30, 40, 50, 60]:
chain = insert(chain, num)
Lookup costs O(h). Balanced (bst, h = 3): about log n. Sorted inserts build a degenerate chain (chain, h = n): O(n), no better than a list scan.