def test_delete_node_is_none(self): bst = BinarySearchTree(data=5) bst.insert(3) bst.insert(7) bst.insert(1) bst.insert(2) bst.insert(9) bst.insert(6) self.assertEqual(bst.delete(3, None), None)
def test_delete_successful_left_subtree(self): bst = BinarySearchTree(data=5) bst.insert(3) bst.insert(7) bst.insert(1) bst.insert(4) bst.insert(2) bst.insert(9) bst.insert(6) self.assertIsNotNone(bst.delete(3, bst.root)) self.assertIsNone(bst.root.left.right) self.assertEqual(bst.root.left.data, 4) self.assertIsNotNone(bst.delete(4, bst.root)) self.assertEqual(bst.root.left.data, 1) self.assertIsNotNone(bst.delete(1, bst.root)) self.assertIsNotNone(bst.delete(2, bst.root))
def test_delete_successful_right_subtree(self): bst = BinarySearchTree(data=5) bst.insert(3) bst.insert(7) bst.insert(1) bst.insert(2) bst.insert(9) bst.insert(6) self.assertIsNotNone(bst.delete(5, bst.root)) self.assertEqual(bst.root.data, 6) self.assertEqual(bst.root.right.data, 7) self.assertEqual(bst.root.right.right.data, 9) self.assertIsNone(bst.root.right.left) self.assertIsNone(bst.root.right.right.left) self.assertIsNotNone(bst.delete(9, bst.root)) self.assertEqual(bst.root.data, 6) self.assertEqual(bst.root.right.data, 7) self.assertIsNone(bst.root.right.left) self.assertIsNone(bst.root.right.right)