def test_delete_right_heavy(self): sequence = [7, 3, 11, 1, 5, 9, 13, 8, 10, 12, 14] expected = [8, 3, 11, 1, 5, 9, 13, 10, 12, 14] Tree = bst.populate_tree(sequence) Tree.delete(7) tree_sequence = [n for n in Tree.breadth_first()] self.assertEqual(tree_sequence, expected)
def test_delete_balanced(self): sequence = [7, 3, 11, 1, 5, 9, 13, 0, 2, 4, 6, 8, 10, 12, 14] expected = [6, 3, 11, 1, 5, 9, 13, 0, 2, 4, 8, 10, 12, 14] Tree = bst.populate_tree(sequence) Tree.delete(7) tree_sequence = [n for n in Tree.breadth_first()] self.assertEqual(tree_sequence, expected)
def test_predecessor_leaf(self): sequence = [7, 3, 11, 1, 5, 9, 13, 0, 2, 4, 6, 5.5, 5.25, 5.75] expected = [6, 3, 11, 1, 5, 9, 13, 0, 2, 4, 5.5, 5.25, 5.75] Tree = bst.populate_tree(sequence) Tree.delete(7) tree_sequence = [n for n in Tree.breadth_first()] self.assertEqual(tree_sequence, expected)
def test_successor_children(self): sequence = [7, 3, 11, 1, 5, 9, 13, 8, 10, 12, 14, 8.5, 8.25, 8.75] expected = [8, 3, 11, 1, 5, 9, 13, 8.5, 10, 12, 14, 8.25, 8.75] Tree = bst.populate_tree(sequence) Tree.delete(7) tree_sequence = [n for n in Tree.breadth_first()] self.assertEqual(tree_sequence, expected)
def test_in_order(self): expected = [ range(7), range(7), range(7), range(11)] for num in range(4): Tree = bst.populate_tree(self.tests[num]) tree_sequence = [i for i in Tree.in_order()] self.assertOrder(tree_sequence, expected[num])
def test_breadth_first(self): expected = [ [3, 1, 5, 0, 2, 4, 6], [5, 3, 6, 1, 4, 0, 2], [1, 0, 3, 2, 5, 4, 6], [5, 1, 9, 0, 3, 7, 10, 2, 4, 6, 8]] for num in range(4): Tree = bst.populate_tree(self.tests[num]) tree_sequence = [i for i in Tree.breadth_first()] self.assertOrder(tree_sequence, expected[num])
def test_post_order(self): expected = [ [0, 2, 1, 4, 6, 5, 3], [0, 2, 1, 4, 3, 6, 5], [0, 2, 4, 6, 5, 3, 1], [0, 2, 4, 3, 1, 6, 8, 7, 10, 9, 5]] for num in range(4): Tree = bst.populate_tree(self.tests[num]) tree_sequence = [i for i in Tree.post_order()] self.assertOrder(tree_sequence, expected[num])
def test_pre_order(self): expected = [ [3, 1, 0, 2, 5, 4, 6], [5, 3, 1, 0, 2, 4, 6], [1, 0, 3, 2, 5, 4, 6], [5, 1, 0, 3, 2, 4, 9, 7, 6, 8, 10]] for num in range(4): Tree = bst.populate_tree(self.tests[num]) tree_sequence = [i for i in Tree.pre_order()] self.assertOrder(tree_sequence, expected[num])
def test_deletion_with_children(self): sequence = [3, 1, 5, 0, 2, 4, 6] delete_order = [1, 5] expected = [ [3, 0, 5, 2, 4, 6], [3, 0, 4, 2, 6] ] Tree = bst.populate_tree(sequence) for num in xrange(2): Tree.delete(delete_order[num]) tree_sequence = [n for n in Tree.breadth_first()] self.assertEqual(tree_sequence, expected[num])
def test_delete_leaves(self): sequence = [3, 1, 5, 0, 2, 4, 6] delete_order = [0, 2, 4, 6] expected = [ [3, 1, 5, 2, 4, 6], [3, 1, 5, 4, 6], [3, 1, 5, 6], [3, 1, 5] ] Tree = bst.populate_tree(sequence) for num in xrange(4): Tree.delete(delete_order[num]) tree_sequence = [n for n in Tree.breadth_first()] self.assertEqual(tree_sequence, expected[num])
def test_delete_single(self): sequence = [7] Tree = bst.populate_tree(sequence) Tree.delete(7) self.assertEqual(Tree.val, None)