Ejemplo n.º 1
0
class AddKeyValueToNotEmptyTreeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")
        self.tree.AddKeyValue(2, "value_2")
        self.tree.AddKeyValue(6, "value_6")

    def test_left(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertFalse(self.tree.FindNodeByKey(1).NodeHasKey)

        self.assertTrue(self.tree.AddKeyValue(1, "value_1"))
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(4, self.tree.Root.LeftChild.Parent.NodeKey)
        self.assertEqual(1, self.tree.Root.LeftChild.LeftChild.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.LeftChild.Parent.NodeKey)

    def test_right(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertFalse(self.tree.FindNodeByKey(7).NodeHasKey)

        self.assertTrue(self.tree.AddKeyValue(7, "value_7"))
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertEqual(4, self.tree.Root.RightChild.Parent.NodeKey)
        self.assertEqual(7, self.tree.Root.RightChild.RightChild.NodeKey)
        self.assertEqual(6,
                         self.tree.Root.RightChild.RightChild.Parent.NodeKey)
Ejemplo n.º 2
0
class RemoveFromTreeWithSeveralNodes(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")

    def test_left(self):
        self.tree.AddKeyValue(2, "value_2")

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(4, self.tree.Root.LeftChild.Parent.NodeKey)

        self.tree.DeleteNodeByKey(2)

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertIsNone(self.tree.Root.LeftChild)
        self.assertIsNone(self.tree.Root.RightChild)

    def test_right(self):
        self.tree.AddKeyValue(6, "value_2")

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertEqual(4, self.tree.Root.RightChild.Parent.NodeKey)

        self.tree.DeleteNodeByKey(6)

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertIsNone(self.tree.Root.LeftChild)
        self.assertIsNone(self.tree.Root.RightChild)
Ejemplo n.º 3
0
class RemoveRightLeafNodeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")
        self.tree.AddKeyValue(2, "value_2")
        self.tree.AddKeyValue(6, "value_6")
        self.tree.AddKeyValue(5, "value_5")
        self.tree.AddKeyValue(7, "value_7")

    def test_left(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertEqual(5, self.tree.Root.RightChild.LeftChild.NodeKey)
        self.assertEqual(7, self.tree.Root.RightChild.RightChild.NodeKey)

        self.tree.DeleteNodeByKey(5)

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertIsNone(self.tree.Root.RightChild.LeftChild)
        self.assertEqual(7, self.tree.Root.RightChild.RightChild.NodeKey)

    def test_right(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertEqual(5, self.tree.Root.RightChild.LeftChild.NodeKey)
        self.assertEqual(7, self.tree.Root.RightChild.RightChild.NodeKey)

        self.tree.DeleteNodeByKey(7)

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)
        self.assertIsNone(self.tree.Root.RightChild.RightChild)
        self.assertEqual(5, self.tree.Root.RightChild.LeftChild.NodeKey)
Ejemplo n.º 4
0
class RemoveLeftLeafNodeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")
        self.tree.AddKeyValue(2, "value_2")
        self.tree.AddKeyValue(6, "value_6")
        self.tree.AddKeyValue(1, "value_1")
        self.tree.AddKeyValue(3, "value_3")

    def test_left(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(1, self.tree.Root.LeftChild.LeftChild.NodeKey)
        self.assertEqual(3, self.tree.Root.LeftChild.RightChild.NodeKey)

        self.tree.DeleteNodeByKey(1)

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertIsNone(self.tree.Root.LeftChild.LeftChild)
        self.assertEqual(3, self.tree.Root.LeftChild.RightChild.NodeKey)

    def test_right(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(1, self.tree.Root.LeftChild.LeftChild.NodeKey)
        self.assertEqual(3, self.tree.Root.LeftChild.RightChild.NodeKey)

        self.tree.DeleteNodeByKey(3)

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertIsNone(self.tree.Root.LeftChild.RightChild)
        self.assertEqual(1, self.tree.Root.LeftChild.LeftChild.NodeKey)
Ejemplo n.º 5
0
class BaseBinarySearchTreeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")
        self.tree.AddKeyValue(2, "value_2")
        self.tree.AddKeyValue(1, "value_1")
        self.tree.AddKeyValue(3, "value_3")
        self.tree.AddKeyValue(6, "value_6")
        self.tree.AddKeyValue(5, "value_5")
        self.tree.AddKeyValue(7, "value_7")
Ejemplo n.º 6
0
class AddExistingKeyValueToNotEmptyTreeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")
        self.tree.AddKeyValue(2, "value_2")
        self.tree.AddKeyValue(6, "value_6")

    def test(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(6, self.tree.Root.RightChild.NodeKey)

        self.assertTrue(self.tree.FindNodeByKey(6).NodeHasKey)
        self.assertTrue(self.tree.FindNodeByKey(2).NodeHasKey)

        self.assertFalse(self.tree.AddKeyValue(6, "value_6"))
        self.assertFalse(self.tree.AddKeyValue(2, "value_2"))
Ejemplo n.º 7
0
class CountTreeWithSingleNodeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")

    def test(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(1, self.tree.Count())
Ejemplo n.º 8
0
class RemoveFromTreeWithSingleNodeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")

    def test(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertFalse(self.tree.DeleteNodeByKey(10))
        self.assertTrue(self.tree.DeleteNodeByKey(4))
        self.assertEqual(0, self.tree.Count())
Ejemplo n.º 9
0
class RemoveLeftNotLeafNodeWithRightChildTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")
        self.tree.AddKeyValue(2, "value_2")
        self.tree.AddKeyValue(6, "value_6")
        self.tree.AddKeyValue(3, "value_3")

    def test(self):
        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(3, self.tree.Root.LeftChild.RightChild.NodeKey)

        self.tree.DeleteNodeByKey(2)
        self.assertEqual(4, self.tree.Root.LeftChild.Parent.NodeKey)
        self.assertEqual(3, self.tree.Root.LeftChild.NodeKey)
        self.assertIsNone(self.tree.Root.LeftChild.RightChild)
        self.assertIsNone(self.tree.Root.LeftChild.LeftChild)
        self.assertFalse(self.tree.FindNodeByKey(2).NodeHasKey)
Ejemplo n.º 10
0
class DeepAllNodesPreOrderInTreeWithSingleNodeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(4, "value_4")

    def test(self):
        nodes = self.tree.DeepAllNodes(2)
        nodes_values = []

        for node in nodes:
            nodes_values.append(node.NodeKey)

        self.assertListEqual(nodes_values, [4])
Ejemplo n.º 11
0
class AddKeyValueToEmptyTreeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)

    def test(self):
        self.assertIsNone(self.tree.Root)
        self.assertFalse(self.tree.FindNodeByKey(4).NodeHasKey)
        self.assertTrue(self.tree.AddKeyValue(4, "value_4"))

        self.assertEqual(4, self.tree.Root.NodeKey)
        self.assertEqual("value_4", self.tree.Root.NodeValue)
        self.assertIsNone(self.tree.Root.Parent)
        self.assertIsNone(self.tree.Root.LeftChild)
        self.assertIsNone(self.tree.Root.RightChild)
class TestBST(unittest.TestCase):
    def setUp(self):
        # Создание дерева из 15 злементов начиная от корня (8)
        self.tree = BST(BSTNode(8, 80, None))
        self.tree.AddKeyValue(4, 40)
        self.tree.AddKeyValue(12, 120)
        self.tree.AddKeyValue(2, 20)
        self.tree.AddKeyValue(6, 60)
        self.tree.AddKeyValue(10, 100)
        self.tree.AddKeyValue(14, 140)
        self.tree.AddKeyValue(1, 10)
        self.tree.AddKeyValue(3, 30)
        self.tree.AddKeyValue(5, 50)
        self.tree.AddKeyValue(7, 70)
        self.tree.AddKeyValue(9, 90)
        self.tree.AddKeyValue(11, 110)
        self.tree.AddKeyValue(13, 130)
        self.tree.AddKeyValue(15, 150)

    def test_AddKeyValue(self):
        # проверка добавления элементов и создания дерево из 15 узлов
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])
        self.tree.AddKeyValue(12, 120)
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])

    def test_FindNodeByKey(self):
        # Проверка поиска элемента по ключу
        self.assertEqual(self.tree.FindNodeByKey(12).NodeHasKey, True)
        self.assertEqual(self.tree.FindNodeByKey(10).NodeHasKey, True)
        self.assertEqual(self.tree.FindNodeByKey(25).NodeHasKey, False)
        self.assertEqual(self.tree.FindNodeByKey(0).NodeHasKey, False)

    def test_FinMinMax(self):
        # Проверка нахождения максимального или минимального значения
        self.assertEqual(self.tree.FinMinMax(self.tree.Root, True).NodeKey, 15)
        self.assertEqual(self.tree.FinMinMax(self.tree.Root, False).NodeKey, 1)
        self.assertEqual(
            self.tree.FinMinMax(self.tree.Root.RightChild, True).NodeKey, 15)
        self.assertEqual(
            self.tree.FinMinMax(self.tree.Root.RightChild.LeftChild,
                                False).NodeKey, 9)
        #self.assertEqual(self.tree.FinMinMax(9).key, 9)

        # Проверка удаления элемента:

    def test_DeleteNodeByKey_1(self):
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])
        self.tree.DeleteNodeByKey(12)
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 13, 10, 9, 11, 14, 15])

    def test_DeleteNodeByKey_2(self):
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])
        self.tree.DeleteNodeByKey(14)
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 15, 13])

    def test_DeleteNodeByKey_3(self):
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])
        self.tree.DeleteNodeByKey(2)
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 3, 1, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])

    def test_DeleteNodeByKey_4(self):
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])
        self.tree.DeleteNodeByKey(1)
        self.assertEqual([node.NodeKey for node in self.tree.GetNodeList()],
                         [8, 4, 2, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])

    def test_Count(self):
        # проверка подсчёта количества узлов дерева
        self.assertEqual(self.tree.Count(), 15)
        self.tree = BST(BSTNode(8, 80, None))
        self.assertEqual(self.tree.Count(), 1)

    # проверка обходов дерева в глубину ------------------------------------------

    def test_DeepAllNodes(self):
        # проверка in-order
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.DeepAllNodes(0))],
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
        self.tree = BST(BSTNode(8, 80, None))
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.DeepAllNodes(0))], [8])

    def test_DeepAllNodes(self):
        # проверка post-order
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.DeepAllNodes(1))],
            [1, 3, 2, 5, 7, 6, 4, 9, 11, 10, 13, 15, 14, 12, 8])
        self.tree = BST(BSTNode(8, 80, None))
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.DeepAllNodes(0))], [8])

    def test_DeepAllNodes(self):
        # проверка pre-order
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.DeepAllNodes(2))],
            [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15])
        self.tree = BST(BSTNode(8, 80, None))
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.DeepAllNodes(0))], [8])

    # проверка обхода дерева в ширину --------------------------------------------

    def test_WideAllNodes(self):
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.WideAllNodes())],
            [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15])
        self.tree = BST(BSTNode(8, 80, None))
        self.assertEqual(
            [node.NodeKey for node in list(self.tree.DeepAllNodes(0))], [8])
Ejemplo n.º 13
0
class RemoveFromLargeTreeTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.tree = BST(None)
        self.tree.AddKeyValue(8, "")
        self.tree.AddKeyValue(4, "")
        self.tree.AddKeyValue(12, "")
        self.tree.AddKeyValue(2, "")
        self.tree.AddKeyValue(6, "")
        self.tree.AddKeyValue(1, "")
        self.tree.AddKeyValue(5, "")
        self.tree.AddKeyValue(7, "")
        self.tree.AddKeyValue(10, "")
        self.tree.AddKeyValue(14, "")
        self.tree.AddKeyValue(11, "")
        self.tree.AddKeyValue(13, "")
        self.tree.AddKeyValue(15, "")
        self.tree.AddKeyValue(16, "")

        self.assertEqual(8, self.tree.Root.NodeKey)
        self.assertEqual(4, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.LeftChild.NodeKey)
        self.assertEqual(1,
                         self.tree.Root.LeftChild.LeftChild.LeftChild.NodeKey)
        self.assertEqual(6, self.tree.Root.LeftChild.RightChild.NodeKey)
        self.assertEqual(
            7, self.tree.Root.LeftChild.RightChild.RightChild.NodeKey)
        self.assertEqual(5,
                         self.tree.Root.LeftChild.RightChild.LeftChild.NodeKey)

        self.assertEqual(12, self.tree.Root.RightChild.NodeKey)
        self.assertEqual(10, self.tree.Root.RightChild.LeftChild.NodeKey)
        self.assertEqual(
            11, self.tree.Root.RightChild.LeftChild.RightChild.NodeKey)
        self.assertEqual(14, self.tree.Root.RightChild.RightChild.NodeKey)
        self.assertEqual(
            13, self.tree.Root.RightChild.RightChild.LeftChild.NodeKey)
        self.assertEqual(
            15, self.tree.Root.RightChild.RightChild.RightChild.NodeKey)
        self.assertEqual(
            16,
            self.tree.Root.RightChild.RightChild.RightChild.RightChild.NodeKey)

    def test_1(self):
        self.tree.DeleteNodeByKey(1)
        self.assertEqual(2, self.tree.Root.LeftChild.LeftChild.NodeKey)
        self.assertIsNone(self.tree.Root.LeftChild.LeftChild.LeftChild)

    def test_2(self):
        self.tree.DeleteNodeByKey(2)
        self.assertEqual(1, self.tree.Root.LeftChild.LeftChild.NodeKey)

    def test_3(self):
        self.tree.DeleteNodeByKey(4)
        self.assertEqual(5, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(2, self.tree.Root.LeftChild.LeftChild.NodeKey)
        self.assertEqual(6, self.tree.Root.LeftChild.RightChild.NodeKey)
        self.assertEqual(
            7, self.tree.Root.LeftChild.RightChild.RightChild.NodeKey)
        self.assertIsNone(self.tree.Root.LeftChild.RightChild.LeftChild)

    def test_4(self):
        self.tree.DeleteNodeByKey(6)
        self.assertEqual(4, self.tree.Root.LeftChild.NodeKey)
        self.assertEqual(7, self.tree.Root.LeftChild.RightChild.NodeKey)
        self.assertEqual(5,
                         self.tree.Root.LeftChild.RightChild.LeftChild.NodeKey)

    def test_5(self):
        self.tree.DeleteNodeByKey(11)
        self.assertEqual(10, self.tree.Root.RightChild.LeftChild.NodeKey)
        self.assertIsNone(self.tree.Root.RightChild.LeftChild.RightChild)

    def test_6(self):
        self.tree.DeleteNodeByKey(10)
        self.assertEqual(11, self.tree.Root.RightChild.LeftChild.NodeKey)

    def test_7(self):
        self.tree.DeleteNodeByKey(14)
        self.assertEqual(15, self.tree.Root.RightChild.RightChild.NodeKey)
        self.assertEqual(
            16, self.tree.Root.RightChild.RightChild.RightChild.NodeKey)
        self.assertEqual(
            13, self.tree.Root.RightChild.RightChild.LeftChild.NodeKey)