Beispiel #1
0
 def test_insert_valid(self):
     bst = BinarySearchTree(data=5)
     bst.insert(3)
     self.assertEqual(bst.root.left.data, 3)
     bst.insert(7)
     self.assertEqual(bst.root.right.data, 7)
     bst.insert(1)
     self.assertEqual(bst.root.left.left.data, 1)
     bst.insert(2)
     self.assertEqual(bst.root.left.left.right.data, 2)
     bst.insert(9)
     self.assertEqual(bst.root.right.right.data, 9)
Beispiel #2
0
 def test_get_max(self):
     bst = BinarySearchTree(data=5)
     bst.insert(3)
     bst.insert(7)
     bst.insert(1)
     bst.insert(2)
     bst.insert(9)
     self.assertEqual(bst.get_max(bst.root), 9)
Beispiel #3
0
 def test_get_min_different_node(self):
     bst = BinarySearchTree(data=5)
     bst.insert(3)
     bst.insert(7)
     bst.insert(1)
     bst.insert(2)
     bst.insert(9)
     self.assertEqual(bst.get_min(bst.root.right), 7)
Beispiel #4
0
 def test_find_successful(self):
     bst = BinarySearchTree(data=5)
     bst.insert(3)
     bst.insert(7)
     bst.insert(1)
     bst.insert(2)
     bst.insert(9)
     self.assertEqual(bst.find(5, bst.root), bst.root)
     self.assertEqual(bst.find(1, bst.root), bst.root.left.left)
Beispiel #5
0
 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)
Beispiel #6
0
 def test_find_successful_different_node(self):
     bst = BinarySearchTree(data=5)
     bst.insert(3)
     bst.insert(7)
     bst.insert(1)
     bst.insert(2)
     bst.insert(9)
     self.assertEqual(bst.find(1, bst.root.left), bst.root.left.left)
     self.assertEqual(bst.find(9, bst.root.right), bst.root.right.right)
     self.assertIsNone(bst.find(9, bst.root.left))
Beispiel #7
0
    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))
Beispiel #8
0
 def test_find_inorder_successor(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.findInorderSuccessor(bst.root.right).data, 6)
     self.assertEqual(bst.findInorderSuccessor(bst.root).data, 1)
     self.assertEqual(bst.findInorderSuccessor(bst.root.left).data, 1)
     self.assertEqual(bst.findInorderSuccessor(bst.root.left.left).data, 1)
Beispiel #9
0
    def test_postorder(self):
        bst = BinarySearchTree(data=5)
        bst.insert(3)
        bst.insert(7)
        bst.insert(1)
        bst.insert(2)
        bst.insert(9)
        bst.insert(6)

        stdout_org = sys.stdout
        stdout_mock = StdOutMock()
        try:
            sys.stdout = stdout_mock
            bst.postorder(bst.root)
        finally:
            sys.stdout = stdout_org

        self.assertEqual(str(stdout_mock), '2136975')
Beispiel #10
0
    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)
Beispiel #11
0
class TestBinarySearchTree(TestCase):
    tree: BinarySearchTree

    def setUp(self):
        random_node = Node(random.randint(0, 100))
        self.tree = BinarySearchTree(random_node)

    def test_insert_root(self):
        random_node = Node(random.randint(0, 100))
        tree = BinarySearchTree(random_node)

        self.assertEqual(
            tree.root.data,
            random_node.data,
            f'Root node data {tree.root.data} '
            f'does not equal {random_node.data}'
        )

    def test_insert_multiple(self):
        for n in range(0, random.randint(5, 10)):
            self.tree.insert(n)

        self._check_binary_search_tree(self.tree.root)

    def test_find_root(self):
        node_found = self.tree.find(self.tree.root.data)

        self.assertEqual(
            self.tree.root.data,
            node_found.data,
            f'Root data {self.tree.root.data} '
            f'is not equal to {node_found.data}'
        )

    def test_find(self):
        num_nodes = random.randint(5, 10)
        for n in range(0, num_nodes):
            self.tree.insert(n)

        random_node_data = random.randint(0, num_nodes - 1)

        found_node = self.tree.find(random_node_data)

        self.assertEqual(
            found_node.data,
            random_node_data,
            f'Found node data {found_node.data} '
            f'does not equal {random_node_data}'
        )

    def test_find_non_existent(self):
        num_nodes = random.randint(5, 10)
        for n in range(0, num_nodes):
            self.tree.insert(n)

        random_node_data = random.randint(num_nodes, 100)

        res_should_be_none = self.tree.find(random_node_data)

        self.assertIsNone(
            res_should_be_none,
            f'{res_should_be_none} should be None.'
        )

    def _check_binary_search_tree(self, node: Node):
        if node.left is not None:
            self.assertLess(
                node.left.data,
                node.data,
                f'Left node data {node.left.data} '
                f'is greater than or equal to parent node data {node.data}'
            )
            self._check_binary_search_tree(node.left)

        if node.right is not None:
            self.assertGreater(
                node.right.data,
                node.data,
                f'Right node data {node.right.data} is equal or less than '
                f'parent node data {node.data}'
            )
            self._check_binary_search_tree(node.right)
Beispiel #12
0
 def test_find_unsuccessful(self):
     bst = BinarySearchTree(data=5)
     bst.insert(3)
     bst.insert(7)
     self.assertEqual(bst.find(-1, bst.root), None)
Beispiel #13
0
 def test_insert_invalid(self):
     bst = BinarySearchTree(data=5)
     with self.assertRaises(ValueError) as error:
         bst.insert(data=None)