Пример #1
0
def test_add_right():
    tree = BinarySearchTree()
    tree.add(10)
    tree.add(15)
    actual = tree.root.right.value
    expected = 15
    assert actual == expected
Пример #2
0
def test_add_left():
    tree = BinarySearchTree()
    tree.add(10)
    tree.add(5)
    actual = tree.root.left.value
    expected = 5
    assert actual == expected
Пример #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)
Пример #4
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)
Пример #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)
Пример #6
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)
Пример #7
0
    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}'
        )
Пример #8
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))
def test_removal():
    bst = BinarySearchTree()

    bst.add(2)
    bst.add(4)
    bst.add(55)
    assert bst.size == 3
    bst.remove(4)
    assert bst.size == 2
    assert bst.root.right.data == 55
Пример #10
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)
Пример #11
0
    def test_postorder_single_node(self):
        bst = BinarySearchTree(data=5)

        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), '5')
Пример #12
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)
def test_insertion():
    bst = BinarySearchTree()
    bst.add(22)
    assert bst.size == 1
    assert bst.root.data == 22
    assert bst.root.left is None
    assert bst.root.right is None
    bst.add(56)
    bst.add(13)
    assert bst.root.right.data == 56
    assert bst.root.left.data == 13
    assert bst.size == 3
    def test_add(self):
        binary_search_tree = BinarySearchTree()
        binary_search_tree.add(50)
        binary_search_tree.add(100)
        binary_search_tree.add(0)
        binary_search_tree.add(75)
        binary_search_tree.add(25)
        binary_search_tree.add(88)
        binary_search_tree.add(33)
        binary_search_tree.add(66)
        binary_search_tree.add(11)

        assert (binary_search_tree.size == 9)
Пример #15
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')
Пример #16
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))
Пример #17
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)
def main():
    # creating a binary search tree
    data = [17, 13, 10, 15, 4, 11, 16, 21, 24, 23, 27, 25, 26]
    bst = BinarySearchTree(data)
    print(bst.breadth_first_list())

    # Removing a few items...
    print('\nDeleting a few items...')
    bst.remove(4)
    bst.remove(10)
    bst.remove(27)
    bst.remove(13)
    print(bst.breadth_first_list())

    print("\nadding 45 to BST...")
    bst.add(45)
    print(bst.breadth_first_list())

    print('\nTHe height of the tree :', bst.height)

    print('\nBST contains 45:', bst.contains(45))
    def test_contains(self):
        binary_search_tree = BinarySearchTree()
        binary_search_tree.add(50)
        binary_search_tree.add(100)
        binary_search_tree.add(0)
        binary_search_tree.add(75)
        binary_search_tree.add(25)
        binary_search_tree.add(88)
        binary_search_tree.add(33)
        binary_search_tree.add(66)
        binary_search_tree.add(11)

        assert (binary_search_tree.contains(50) is True)
        assert (binary_search_tree.contains(100) is True)
        assert (binary_search_tree.contains(0) is True)
        assert (binary_search_tree.contains(75) is True)
        assert (binary_search_tree.contains(25) is True)
        assert (binary_search_tree.contains(88) is True)
        assert (binary_search_tree.contains(33) is True)
        assert (binary_search_tree.contains(66) is True)
        assert (binary_search_tree.contains(11) is True)

        assert (binary_search_tree.contains(99) is False)
Пример #20
0
def test_instantiate_empty():
    tree = BinarySearchTree()
    actual = tree.root
    expected = None
    assert actual == expected
Пример #21
0
 def test_find_unsuccessful(self):
     bst = BinarySearchTree(data=5)
     bst.insert(3)
     bst.insert(7)
     self.assertEqual(bst.find(-1, bst.root), None)
def test_contains():
    bst = BinarySearchTree([5, 7, 3])
    assert bst.contains(5) == True
    assert bst.contains(99) == False
def test_empty():
    bst = BinarySearchTree()
    assert bst.size == 0
    assert bst.root is None
Пример #24
0
 def test_insert_invalid(self):
     bst = BinarySearchTree(data=5)
     with self.assertRaises(ValueError) as error:
         bst.insert(data=None)
    def test_construction(self):
        binary_search_tree = BinarySearchTree()

        assert (binary_search_tree.size == 0)
        assert (binary_search_tree.root is None)
Пример #26
0
 def test_get_min_empty_bst(self):
     bst = BinarySearchTree(data=5)
     self.assertIsNone(bst.get_min(bst.root.right))
Пример #27
0
def tree():
    tree = BinarySearchTree()
    tree.add(10)
    tree.add(5)
    tree.add(15)
    return tree
Пример #28
0
 def test_initiation(self):
     bst = BinarySearchTree(data=5)
     self.assertEqual(bst.root.data, 5)
Пример #29
0
 def setUp(self):
     random_node = Node(random.randint(0, 100))
     self.tree = BinarySearchTree(random_node)
Пример #30
0
 def test_initiation_invalid(self):
     with self.assertRaises(ValueError) as error:
         bst = BinarySearchTree(data=None)