def test_parent(self): tree = BST() tree.add_all([8, 3, 1, 6, 7, 4, 10, 14, 13]) self.assertEqual(tree.parent(1), 3) self.assertEqual(tree.parent(3), 8) self.assertEqual(tree.parent(8), None) self.assertEqual(tree.parent(14), 10) self.assertEqual(tree.parent(13), 14)
def test_in_order(self): tree = BST() tree.add_all([8, 3, 1, 6, 7, 4, 10, 14, 13]) expected = tree.in_order() self.assertEqual(expected.__str__(), [1, 3, 4, 6, 7, 8, 10, 13, 14].__str__())
def test_successor_in_order(self): tree = BST() tree.add_all([20, 8, 4, 12, 14, 10, 22]) self.assertEqual(tree.successor_in_order(8, "with-prev-pointer"), 10) self.assertEqual(tree.successor_in_order(10, "with-prev-pointer"), 12) self.assertEqual(tree.successor_in_order(14, "with-prev-pointer"), 20) self.assertEqual(tree.successor_in_order(22, "with-prev-pointer"), None) self.assertEqual(tree.successor_in_order(8, "with-parent-pointer"), 10) self.assertEqual(tree.successor_in_order(10, "with-parent-pointer"), 12) self.assertEqual(tree.successor_in_order(14, "with-parent-pointer"), 20) self.assertEqual(tree.successor_in_order(22, "with-parent-pointer"), None)
def test_is_bst(self): tree = BST() tree.add_all([8, 3, 1, 6, 7, 4, 10, 14, 13]) self.assertTrue(tree.is_bst())
def test_post_order(self): tree = BST() tree.add_all([8, 3, 1, 6, 7, 4, 10, 14, 13]) expected = tree.post_order() self.assertEqual(expected.__str__(), [1, 4, 7, 6, 3, 13, 14, 10, 8].__str__())
def test_bst(self): bst = BST() bst.insert(5, 6) bst.insert(3, 7) bst.insert(2, 8) bst.insert(4, 9) bst.insert(8, 10) bst.insert(6, 11) bst.insert(9, 12) self.assertTrue(bst.get(6) == 11) self.assertTrue(bst.get(15) == None) self.assertTrue(bst.height() == 3) self.assertTrue(bst.preorder() == [5, 3, 2, 4, 8, 6, 9])
def minimum_cost_tree(keys, frequencies): A = minimum_cost_matrix(keys, frequencies) bst = BST() create_optimal_tree(A, bst, keys, 0, len(keys) - 1) return bst.preorder()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.test_bst = BST()
class BSTTest(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.test_bst = BST() def test_to_array(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(7) self.assertEqual(self.test_bst.to_array(), [2, 5, 7]) self.assertEqual(self.test_bst.to_array(key='preorder'), [5, 2, 7]) self.assertEqual(self.test_bst.to_array(key='postorder'), [2, 7, 5]) self.assertEqual(self.test_bst.to_array(key='levelorder'), [5, 2, 7]) def test_insert(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(3) self.assertEqual(self.test_bst.to_array(), [2, 3, 5]) def test_remove(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(3) self.test_bst.remove(3) self.assertEqual(self.test_bst.to_array(), [2, 5]) def test_get_root(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(3) self.assertEqual(self.test_bst.get_root(), 5) def test_search(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(3) self.assertEqual(5 in self.test_bst, True) self.assertEqual(2 not in self.test_bst, False) self.assertEqual(7 not in self.test_bst, True) def test_min(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(3) self.assertEqual(self.test_bst.min(), 2) def test_max(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(3) self.assertEqual(self.test_bst.max(), 5) def test_size(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(7) self.assertEqual(self.test_bst.size(), 3) def test_min_height(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(7) self.assertEqual(self.test_bst.min_height(), 1) def test_max_height(self): self.test_bst.insert(5) self.test_bst.insert(2) self.test_bst.insert(7) self.test_bst.insert(1) self.assertEqual(self.test_bst.max_height(), 2) def test_is_empty(self): self.assertEqual(self.test_bst.is_empty(), True) self.test_bst.insert(5) self.assertEqual(self.test_bst.is_empty(), False)