def test_insert_avl_single_left_branch(): tree = BSTree() for i in xrange(7, 0, -1): tree.insert(i) actual = [n.key for n in tree.breadth_first()] expected = [4, 2, 6, 1, 3, 5, 7] assert tree.depth() == 3 assert tree.balance() == 0 assert actual == expected
def test_rebalance_rotate_right(): tree = BSTree(3) tree.left = BSTree(2, tree) tree.left.left = BSTree(1, tree.left) tree.rebalance() actual = [n.key for n in tree.breadth_first()] expected = [2, 1, 3] assert tree.depth() == 2 assert tree.balance() == 0 assert actual == expected
def tree_rebalance_right_right(): tree = BSTree(2) tree.left = BSTree(1, tree) tree.right = BSTree(4, tree) tree.right.left = BSTree(3, tree.right) tree.right.right = BSTree(6, tree.right) tree.right.right.left = BSTree(5, tree.right.right) tree.right.right.right = BSTree(7, tree.right.right) tree.rebalance() actual = [n.key for n in tree.breadth_first()] expected = [4, 2, 6, 1, 3, 5, 7] assert tree.depth() == 3 assert tree.balance() == 0 assert actual == expected
class testPrePostAndBreadth(unittest.TestCase): def setUp(self): self.my_bst = BSTree() self.filled_tree = BSTree() self.filled_tree.insert(10) self.filled_tree.insert(5) self.filled_tree.insert(15) self.filled_tree.insert(2) self.filled_tree.insert(12) self.filled_tree.insert(7) self.filled_tree.insert(17) self.filled_tree.insert(1) self.filled_tree.insert(4) self.filled_tree.insert(6) self.filled_tree.insert(9) self.filled_tree.insert(11) self.filled_tree.insert(14) self.filled_tree.insert(16) self.filled_tree.insert(23) self.imbalanced_tree = BSTree() self.imbalanced_tree.insert(10) self.imbalanced_tree.insert(5) self.imbalanced_tree.insert(15) self.imbalanced_tree.insert(2) self.imbalanced_tree.insert(12) self.imbalanced_tree.insert(7) self.imbalanced_tree.insert(17) self.imbalanced_tree.insert(4) self.imbalanced_tree.insert(6) self.imbalanced_tree.insert(9) self.imbalanced_tree.insert(14) self.imbalanced_tree.insert(23) self.number_catcher = [] def test_pre_order_on_empty_bst(self): for i in self.my_bst.pre_order(): self.number_catcher.append(i) self.assertEqual(self.number_catcher, []) def test_pre_order_on_filled_bst(self): for i in self.filled_tree.pre_order(): self.number_catcher.append(i) self.assertEqual(self.number_catcher, [10, 5, 2, 1, 4, 7, 6, 9, 15, 12, 11, 14, 17, 16, 23]) def test_post_order_on_empty_bst(self): for i in self.my_bst.post_order(): self.number_catcher.append(i) self.assertEqual(self.number_catcher, []) def test_post_order_on_filled_bst(self): for i in self.filled_tree.post_order(): self.number_catcher.append(i) self.assertEqual(self.number_catcher, [1, 4, 2, 6, 9, 7, 5, 11, 14, 12, 16, 23, 17, 15, 10]) def test_breadth_first_on_empty(self): for i in self.my_bst.breadth_first(): self.number_catcher.append(i) self.assertEqual(self.number_catcher, []) pass def test_breadth_first_on_filled(self): for i in self.filled_tree.breadth_first(): self.number_catcher.append(i) self.assertEqual(self.number_catcher, [10, 5, 15, 2, 7, 12, 17, 1, 4, 6, 9, 11, 14, 16, 23]) def test_breadth_first_on_imbalanced_tree(self): for i in self.imbalanced_tree.breadth_first(): self.number_catcher.append(i) self.assertEqual(self.number_catcher, [10, 5, 15, 2, 7, 12, 17, 4, 6, 9, 14, 23]) def testing_the_delete_method(self): pass