def test_delete_lone_leftmost_rightsubtree_child_with_right_child(): binary_search_tree = BinarySearchTree() binary_search_tree.insert(1) binary_search_tree.insert(0) binary_search_tree.insert(3) binary_search_tree.insert(2) binary_search_tree.insert(6) binary_search_tree.insert(4) binary_search_tree.insert(5) binary_search_tree.insert(7) node = binary_search_tree.search(3) binary_search_tree.delete(node) traversed_list = [] action = lambda node: traversed_list.append(node.value) binary_search_tree.traverse(action) new_node = binary_search_tree.search(4) new_left_child_node = binary_search_tree.search(2) new_right_child_node = binary_search_tree.search(6) new_left_of_right_child_node = binary_search_tree.search(5) assert traversed_list == [0, 1, 2, 4, 5, 6, 7] assert new_node.parent is binary_search_tree.root assert binary_search_tree.root.right is new_node assert new_node.left is new_left_child_node assert new_left_child_node.parent is new_node assert new_node.right is new_right_child_node assert new_right_child_node.parent is new_node assert new_right_child_node.left is new_left_of_right_child_node assert new_left_of_right_child_node.parent is new_right_child_node
def test_search_fail(): binary_search_tree = BinarySearchTree() N = 10 seq = list(range(N)) random.shuffle(seq) for i in seq: binary_search_tree.insert(i) with pytest.raises(FileNotFoundError): binary_search_tree.search(N+1)
def test_delete_lone_node_left_from_parent_root(): binary_search_tree = BinarySearchTree() binary_search_tree.insert(1) binary_search_tree.insert(0) binary_search_tree.insert(2) node = binary_search_tree.search(0) binary_search_tree.delete(node) traversed_list = [] action = lambda node: traversed_list.append(node.value) binary_search_tree.traverse(action) parent_node = binary_search_tree.search(1) assert traversed_list == [1, 2] assert parent_node.left is None
def test_delete_root_node_only_right_child(): binary_search_tree = BinarySearchTree() binary_search_tree.insert(0) binary_search_tree.insert(2) binary_search_tree.insert(1) binary_search_tree.insert(3) node = binary_search_tree.search(0) binary_search_tree.delete(node) traversed_list = [] action = lambda node: traversed_list.append(node.value) binary_search_tree.traverse(action) new_node = binary_search_tree.search(2) assert traversed_list == [1, 2, 3] assert new_node is binary_search_tree.root assert new_node.parent is None
def test_delete_only_right_child_right_from_parent(): binary_search_tree = BinarySearchTree() binary_search_tree.insert(0) binary_search_tree.insert(1) binary_search_tree.insert(3) binary_search_tree.insert(2) binary_search_tree.insert(4) node = binary_search_tree.search(1) binary_search_tree.delete(node) traversed_list = [] action = lambda node: traversed_list.append(node.value) binary_search_tree.traverse(action) new_node = binary_search_tree.search(3) new_node_parent = binary_search_tree.search(0) assert traversed_list == [0, 2, 3, 4] assert new_node.parent is new_node_parent assert new_node_parent.right is new_node
def test_delete_lone_root_node(): binary_search_tree = BinarySearchTree() binary_search_tree.insert(0) node = binary_search_tree.search(0) binary_search_tree.delete(node) traversed_list = [] action = lambda node: traversed_list.append(node.value) binary_search_tree.traverse(action) assert traversed_list == []
def test_search(): binary_search_tree = BinarySearchTree() N = 10 seq = list(range(N)) random.shuffle(seq) for i in seq: binary_search_tree.insert(i) for i in range(N): assert binary_search_tree.search(i).value == i
def test_delete_root_node_lone_leftmost_rightsubtree_child(): binary_search_tree = BinarySearchTree() binary_search_tree.insert(1) binary_search_tree.insert(0) binary_search_tree.insert(3) binary_search_tree.insert(2) binary_search_tree.insert(4) node = binary_search_tree.search(1) binary_search_tree.delete(node) traversed_list = [] action = lambda node: traversed_list.append(node.value) binary_search_tree.traverse(action) new_node = binary_search_tree.search(2) new_child_node = binary_search_tree.search(0) assert traversed_list == [0, 2, 3, 4] assert new_node is binary_search_tree.root assert new_node.parent is None assert new_node.left is new_child_node assert new_child_node.parent is new_node
class test_binarysearchtree(TestCase): def setUp(self): self.binarysearchtree = BinarySearchTree() self.binarysearchtree.insert(5) self.binarysearchtree.insert(4) self.binarysearchtree.insert(3) self.binarysearchtree.insert(2) self.binarysearchtree.insert(1) def test_search(self): self.assertTrue(self.binarysearchtree.search(3))