Example #1
0
def minimal_tree(vals):
    """
    Creates a binary search tree with minimal
    height given an array of sorted integers
    """
    if not vals:
        return None

    n = len(vals)
    root = TreeNode(vals[n // 2])
    root.left = minimal_tree(vals[0:n // 2])
    root.right = minimal_tree(vals[n // 2 + 1:])
    return root
Example #2
0
t.assertAlmostEqual(None, tree.inorder_successor(65))
t.assertAlmostEqual(80, tree.inorder_successor(70))
t.assertAlmostEqual(None, tree.inorder_successor(85))
t.assertAlmostEqual(100, tree.inorder_successor(90))
t.assertAlmostEqual(None, tree.inorder_successor(95))
t.assertAlmostEqual(None, tree.inorder_successor(100))
t.assertAlmostEqual(None, tree.inorder_successor(110))

tree = Tree()
tree.search(50)

node100 = TreeNode(100)
node50 = TreeNode(50)
node200 = TreeNode(200)
node25 = TreeNode(25)
node125 = TreeNode(125)
node350 = TreeNode(350)

node50.left = node25
node200.left = node125
node200.right = node350
node100.left = node50
node100.right = node200
tree.root = node100
t.assertEqual(id(node100), id(tree.search(100)))
t.assertEqual(id(node50), id(tree.search(50)))
t.assertEqual(id(node200), id(tree.search(200)))
t.assertEqual(id(node25), id(tree.search(25)))
t.assertEqual(id(node125), id(tree.search(125)))
t.assertEqual(id(node350), id(tree.search(350)))