def test_scale(self): N = 100000 root = None for i in range(N): root = insert(root, i) assert valid_bst_order(root) assert valid_rbtree_paths(root) assert valid_rbtree_colors(root)
def should_insert_inorder(): for i in range(5): inputs = list(rand.randint(100, size=35)) print inputs tree = None for j, val in enumerate(inputs): tree = insert(val, tree) values_in_order = inorder(tree) assert j + 1 == len(values_in_order) assert sorted(inputs[0: len(values_in_order)]) == values_in_order assert is_valid(tree) assert sorted(inputs) == inorder(tree)
def should_insert_inorder(): for i in range(5): inputs = list(rand.randint(100, size=35)) print inputs tree = None for j, val in enumerate(inputs): tree = insert(val, tree) values_in_order = inorder(tree) assert j + 1 == len(values_in_order) assert sorted(inputs[0:len(values_in_order)]) == values_in_order assert is_valid(tree) assert sorted(inputs) == inorder(tree)
def should_work_for_a_large_tree(): for i in range(10): inputs = list(rand.randint(10000, size=3000)) tree = reduce(lambda tree, val: insert(val, tree), inputs, None) assert is_valid(tree) assert inorder(tree) == sorted(inputs) for i in range(2500): val = inputs.pop() # print "delete %s from tree %s" % (val, strify(tree)) tree = delete(val, tree) # assert is_valid(tree), strify(tree) assert is_valid(tree) assert inorder(tree) == sorted(inputs)
def test_find(self): N = 100 in_tree = range(0, N, 2) not_in_tree = range(1, N, 2) root = None for value in in_tree: root = insert(root, value) for value in in_tree: assert find(root, value) for value in not_in_tree: assert not find(root, value)
def test_insert(self): N = 10 test_cases = [("sorted", range(N)), ("reversed", reversed(range(N))), ("random 1", [randint(1, N) for i in range(N)]), ("random 2", [randint(1, 10 * N) for i in range(N)])] for name, values in test_cases: root = None for value in values: root = insert(root, value) print('\nTest case "{}":'.format(name)) root.render() assert valid_bst_order(root) # TODO: Uncomment these once you believe you've implemented a # working red-black tree. assert valid_rbtree_paths(root) assert valid_rbtree_colors(root)