def test_add_right(): tree = BinarySearchTree() tree.add(10) tree.add(15) actual = tree.root.right.value expected = 15 assert actual == expected
def test_add_left(): tree = BinarySearchTree() tree.add(10) tree.add(5) actual = tree.root.left.value expected = 5 assert actual == expected
def test_removal(): bst = BinarySearchTree() bst.add(2) bst.add(4) bst.add(55) assert bst.size == 3 bst.remove(4) assert bst.size == 2 assert bst.root.right.data == 55
def test_insertion(): bst = BinarySearchTree() bst.add(22) assert bst.size == 1 assert bst.root.data == 22 assert bst.root.left is None assert bst.root.right is None bst.add(56) bst.add(13) assert bst.root.right.data == 56 assert bst.root.left.data == 13 assert bst.size == 3
def main(): # creating a binary search tree data = [17, 13, 10, 15, 4, 11, 16, 21, 24, 23, 27, 25, 26] bst = BinarySearchTree(data) print(bst.breadth_first_list()) # Removing a few items... print('\nDeleting a few items...') bst.remove(4) bst.remove(10) bst.remove(27) bst.remove(13) print(bst.breadth_first_list()) print("\nadding 45 to BST...") bst.add(45) print(bst.breadth_first_list()) print('\nTHe height of the tree :', bst.height) print('\nBST contains 45:', bst.contains(45))
def test_contains(self): binary_search_tree = BinarySearchTree() binary_search_tree.add(50) binary_search_tree.add(100) binary_search_tree.add(0) binary_search_tree.add(75) binary_search_tree.add(25) binary_search_tree.add(88) binary_search_tree.add(33) binary_search_tree.add(66) binary_search_tree.add(11) assert (binary_search_tree.contains(50) is True) assert (binary_search_tree.contains(100) is True) assert (binary_search_tree.contains(0) is True) assert (binary_search_tree.contains(75) is True) assert (binary_search_tree.contains(25) is True) assert (binary_search_tree.contains(88) is True) assert (binary_search_tree.contains(33) is True) assert (binary_search_tree.contains(66) is True) assert (binary_search_tree.contains(11) is True) assert (binary_search_tree.contains(99) is False)
def test_add(self): binary_search_tree = BinarySearchTree() binary_search_tree.add(50) binary_search_tree.add(100) binary_search_tree.add(0) binary_search_tree.add(75) binary_search_tree.add(25) binary_search_tree.add(88) binary_search_tree.add(33) binary_search_tree.add(66) binary_search_tree.add(11) assert (binary_search_tree.size == 9)
def tree(): tree = BinarySearchTree() tree.add(10) tree.add(5) tree.add(15) return tree
def test_add_to_empty(): tree = BinarySearchTree() tree.add("apples") actual = tree.root.value expected = "apples" assert actual == expected