def test_add(self): bst = BST() bst.add(10) assert bst.root.val == 10 assert bst.is_empty() is False assert bst.size == 1 assert bst.levels == 1 assert bst.serialize() == '10' assert bst.level_order() == [[10]] assert bst.pre_order() == [10] assert bst.in_order() == [10] assert bst.post_order() == [10] assert bst == BST(serialize='10') bst.add(1) assert bst.root.val == 10 assert bst.is_empty() is False assert bst.size == 2 assert bst.levels == 2 assert bst.serialize() == '10,1,#' assert bst.level_order() == [[10], [1]] assert bst.pre_order() == [10, 1] assert bst.in_order() == [1, 10] assert bst.post_order() == [1, 10] bst.add(20) assert bst.root.val == 10 assert bst.is_empty() is False assert bst.size == 3 assert bst.levels == 2 assert bst.serialize() == '10,1,20' assert bst.level_order() == [[10], [1, 20]] assert bst.pre_order() == [10, 1, 20] assert bst.in_order() == [1, 10, 20] assert bst.post_order() == [1, 20, 10] bst.add(5) bst.add(15) assert bst.root.val == 10 assert bst.is_empty() is False assert bst.size == 5 assert bst.levels == 3 assert bst.serialize() == '10,1,20,#,5,15,#' assert bst.level_order() == [[10], [1, 20], [5, 15]] assert bst.pre_order() == [10, 1, 5, 20, 15] assert bst.in_order() == [1, 5, 10, 15, 20] assert bst.post_order() == [5, 1, 15, 20, 10]
def test_remove_min(self): bst = BST() assert bst.remove_min() is None assert bst.is_empty() is True bst.add(10) assert bst.remove_min() == 10 assert bst.is_empty() is True bst.add(10) bst.add(20) bst.add(15) bst.add(5) bst.add(7) assert bst.remove_min() == 5 assert bst.remove_min() == 7 assert bst.remove_min() == 10 assert bst.root.val == 20 assert bst.size == 2 assert bst.remove_min() == 15 assert bst.remove_min() == 20 assert bst.is_empty() is True
def test_empty_tree(self): bst = BST() assert bst.root is None assert bst.is_empty() is True assert bst.size == 0 assert bst.levels == 0 assert bst.__repr__() == "BST(serialize='')" assert bst.serialize() == '' assert bst.level_order() == [] assert bst.pre_order() == [] assert bst.in_order() == [] assert bst.post_order() == [] assert bst == BST()
def test_add_values(self): bst = BST() bst.add_values([1, 2, 3]) assert bst.root.val == 1 assert bst.is_empty() is False assert bst.size == 3 assert bst.levels == 3 assert bst.serialize() == '1,#,2,#,#,#,3' bst.add_values([1, 2, 3]) assert bst.root.val == 1 assert bst.is_empty() is False assert bst.size == 3 assert bst.levels == 3 assert bst.serialize() == '1,#,2,#,#,#,3' bst.add_values([-3, -2, -1]) assert bst.root.val == 1 assert bst.is_empty() is False assert bst.size == 6 assert bst.levels == 4 assert bst.serialize() == '1,-3,2,#,-2,#,3,#,#,#,-1,#,#,#,#'
def test_general_functions(self): # XX print("Running test_general_functions") # Add a number of elements to at least prevent some possible toy-example working out h = BST([50, 70, 80, 60, 55, 67, 90, 57, 20, 10, 15, 0]) self.assertEqual(h.find_min(), 0) self.assertEqual(h.find_max(), 90) self.assertFalse(h.is_empty()) n = h.search(67) m = h.search(99) self.assertTrue(isinstance(n, Node)) self.assertEqual(n.get_key(), 67) self.assertEqual(m, None) self.assertTrue(h.contains(67)) self.assertFalse(h.contains(100))
class BSTSet(SetBase): def __init__(self): self._bst = BST() def get_size(self): return self._bst.size() def is_empty(self): return self._bst.is_empty() def add(self, e): return self._bst.add(e) def contains(self, e): return self._bst.contains(e) def remove(self, e): return self._bst.remove(e)
class BSTSet(UserSet): def __init__(self): self._bst = BST() def size(self): return self._bst.size() def is_empty(self): return self._bst.is_empty() def add(self, item): self._bst.add(item) def remove(self, item): self._bst.remove(item) def contains(self, item): return item in self._bst
def test_remove(self): bst = BST() with pytest.raises(ValueError): bst.remove(0) bst.add(10) with pytest.raises(ValueError): bst.remove(0) bst.remove(10) assert bst.is_empty() is True bst.add(10) bst.add(20) bst.add(15) bst.add(5) bst.add(7) bst.remove(10) assert bst.serialize() == '15,5,20,#,7,#,#' assert bst.size == 4 assert bst.root.val == 15 bst.remove(5) assert bst.serialize() == '15,7,20' assert bst.size == 3 assert bst.root.val == 15 bst.add(18) bst.remove(20) assert bst.serialize() == '15,7,18' assert bst.size == 3 assert bst.root.val == 15 bst.remove(7) assert bst.serialize() == '15,#,18' assert bst.size == 2 assert bst.root.val == 15 bst.remove(15) assert bst.serialize() == '18' assert bst.size == 1 assert bst.root.val == 18
def test_create_BST(): bst = BST() assert bst.size() == 0 assert bst.is_empty()