Ejemplo n.º 1
0
 def test_delete_with_linked_list_trees(self):
     # Create a complete binary search tree of 7 items in level-order
     items = [1, 2, 3, 4, 5, 6]
     tree = BinarySearchTree(items)
     tree.delete(2)
     assert tree.root.data == 1
     assert tree.root.left is None
     assert tree.root.right.data == 3
     assert tree.root.right.right.data == 4
     assert tree.root.right.left is None
Ejemplo n.º 2
0
 def test_delete_with_unbalanced_trees(self):
     # Create a complete binary search tree of 7 items in level-order
     items = [3, 6, 1, 2, 5, 7]
     tree = BinarySearchTree(items)
     tree.delete(2)
     assert tree.root.data == 3
     assert tree.root.left.data == 1
     assert tree.root.right.data == 6
     assert tree.root.left.left is None
     assert tree.root.left.right is None
    def test_deletion_leaf(self):
        bt = BinarySearchTree()
        bt.insert_multiple_values([10, 20, 22, 3, 7, 4, 32, 900])
        node, depth = bt.search(900)

        self.assertEqual(node.value, 900)
        self.assertEqual(depth, 4)

        bt.delete(900)

        node, _ = bt.search(900)

        self.assertIsNone(node)
        self.assertEqual(bt.in_order(), [3, 4, 7, 10, 20, 22, 32])
    def test_deletion_first_use_case(self):
        bt = BinarySearchTree()
        bt.insert_multiple_values([10, 20, 22, 3, 7, 4, 32, 900])
        node, depth = bt.search(4)

        self.assertEqual(node.value, 4)
        self.assertEqual(depth, 3)

        bt.delete(4)

        node, depth = bt.search(4)

        self.assertIsNone(node)
        self.assertEqual(bt.in_order(), [3, 7, 10, 20, 22, 32, 900])
Ejemplo n.º 5
0
 def test_delete_with_3_items(self):
     # Create a complete binary search tree of 3 items in level-order
     items = [2, 1, 3]
     tree = BinarySearchTree(items)
     assert tree.root.data == 2
     assert tree.root.left.data == 1
     assert tree.root.right.data == 3
     tree.delete(2)
     assert tree.root.data == 1
     assert tree.root.left is None
     assert tree.root.right.data is 3
     tree.delete(1)
     assert tree.root.data == 3
     assert tree.root.left is None
     assert tree.root.right is None
     tree.delete(3)
     assert tree.root is None
Ejemplo n.º 6
0
 def test_delete_with_7_items(self):
     # Create a complete binary search tree of 7 items in level-order
     items = [4, 2, 6, 1, 3, 5, 7]
     tree = BinarySearchTree(items)
     tree.delete(4)
     assert tree.root.data == 3
     assert tree.root.left.data == 2
     assert tree.root.right.data == 6
     assert tree.root.left.left.data == 1
     assert tree.root.left.right is None
     tree.delete(6)
     assert tree.root.data == 3
     assert tree.root.left.data == 2
     assert tree.root.right.data == 5
     assert tree.root.right.left is None
     assert tree.root.right.right.data == 7
     tree.delete(5)
     assert tree.root.data == 3
     assert tree.root.left.data == 2
     assert tree.root.right.data == 7
     assert tree.root.right.left == None
     assert tree.root.right.right == None