class TreeNode: def __init__(self, key): self.key, self.left, self.right = key, None, None def insert(root, key): if root is None: return TreeNode(key) current = root while True: if key == current.key: return root side = "left" if key < current.key else "right" child = getattr(current, side) if child is None: setattr(current, side, TreeNode(key)) return root current = child def contains(root, key): while root is not None: if key == root.key: return True root = root.left if key < root.key else root.right return False def ordered(root): result, pending = [], [] while root is not None or pending: while root is not None: pending.append(root) root = root.left root = pending.pop() result.append(root.key) root = root.right return result def delete(root, key): if root is None: return None if key < root.key: root.left = delete(root.left, key) elif key > root.key: root.right = delete(root.right, key) else: if root.left is None: return root.right if root.right is None: return root.left successor = root.right while successor.left is not None: successor = successor.left root.key = successor.key root.right = delete(root.right, successor.key) return root root = None for key in [8, 3, 12, 10, 14]: root = insert(root, key) root = delete(root, 8) root = delete(root, 99) print(ordered(root))