def test_deletion(basic_tree):
    """Test the deletion of an AVL tree."""
    tree = avl_tree.AVLTree()

    # 23, 4, 30, 11, 7, 34, 20, 24, 22, 15, 1
    for key, data in basic_tree:
        tree.insert(key=key, data=data)

    # No child
    tree.delete(15)
    assert [item for item in traversal.inorder_traverse(tree)] == [
        (1, "1"),
        (4, "4"),
        (7, "7"),
        (11, "11"),
        (20, "20"),
        (22, "22"),
        (23, "23"),
        (24, "24"),
        (30, "30"),
        (34, "34"),
    ]

    # One right child
    tree.delete(20)
    assert [item for item in traversal.inorder_traverse(tree)] == [
        (1, "1"),
        (4, "4"),
        (7, "7"),
        (11, "11"),
        (22, "22"),
        (23, "23"),
        (24, "24"),
        (30, "30"),
        (34, "34"),
    ]

    # One left child
    tree.insert(key=17, data="17")
    tree.delete(22)
    assert [item for item in traversal.inorder_traverse(tree)] == [
        (1, "1"),
        (4, "4"),
        (7, "7"),
        (11, "11"),
        (17, "17"),
        (23, "23"),
        (24, "24"),
        (30, "30"),
        (34, "34"),
    ]

    # FIXME
    """
def test_binary_search_tree_traversal_random():
    """Test binary search tree traversal with random sampling."""
    for _ in range(0, 10):

        insert_data = random.sample(range(1, 2000), 1000)

        tree = binary_search_tree.BinarySearchTree()
        for key in insert_data:
            tree.insert(key=key, data=str(key))

        preorder_recursive = [
            item for item in traversal.preorder_traverse(tree, True)
        ]
        preorder = [item for item in traversal.preorder_traverse(tree, False)]
        assert preorder_recursive == preorder

        inorder_recursive = [
            item for item in traversal.inorder_traverse(tree, True)
        ]
        inorder_nonrecursive = [
            item for item in traversal.inorder_traverse(tree, False)
        ]
        assert inorder_recursive == inorder_nonrecursive

        rinorder_recursive = [
            item for item in traversal.reverse_inorder_traverse(tree, True)
        ]
        rinorder_nonrecursive = [
            item for item in traversal.reverse_inorder_traverse(tree, False)
        ]
        assert rinorder_recursive == rinorder_nonrecursive

        postorder_recursive = [
            item for item in traversal.postorder_traverse(tree, True)
        ]
        postorder_nonrecursive = [
            item for item in traversal.postorder_traverse(tree, False)
        ]
        assert postorder_recursive == postorder_nonrecursive
Beispiel #3
0
def test_avl_tree_traversal(basic_tree):
    """Test AVL tree traversal."""
    tree = avl_tree.AVLTree()

    for key, data in basic_tree:
        tree.insert(key=key, data=data)

    assert [item for item in traversal.inorder_traverse(tree, False)] == [
        (1, "1"),
        (4, "4"),
        (7, "7"),
        (11, "11"),
        (15, "15"),
        (20, "20"),
        (22, "22"),
        (23, "23"),
        (24, "24"),
        (30, "30"),
        (34, "34"),
    ]

    assert [item for item in traversal.preorder_traverse(tree, False)] == [
        (23, "23"),
        (11, "11"),
        (4, "4"),
        (1, "1"),
        (7, "7"),
        (20, "20"),
        (15, "15"),
        (22, "22"),
        (30, "30"),
        (24, "24"),
        (34, "34"),
    ]

    assert [item for item in traversal.postorder_traverse(tree, False)] == [
        (1, "1"),
        (7, "7"),
        (4, "4"),
        (15, "15"),
        (22, "22"),
        (20, "20"),
        (11, "11"),
        (24, "24"),
        (34, "34"),
        (30, "30"),
        (23, "23"),
    ]
    def do_traverse(self, line):
        """Traverse the binary search tree.

        Options: pre, in, post, rev-in

        Example
        -------
        tree> traverse pre
        """
        try:
            arg = self._get_single_arg(line=line).lower()

            if isinstance(self._tree, red_black_tree.RBTree):
                if arg == "pre":
                    for item in self._tree.preorder_traverse():
                        print(item)
                elif arg == "in":
                    for item in self._tree.inorder_traverse():
                        print(item)
                elif arg == "post":
                    for item in self._tree.postorder_traverse():
                        print(item)
            else:
                if arg == "pre":
                    if isinstance(
                            self._tree, threaded_binary_tree.
                            RightThreadedBinaryTree) or isinstance(
                                self._tree,
                                threaded_binary_tree.DoubleThreadedBinaryTree):
                        for item in self._tree.preorder_traverse():
                            print(item)
                    else:
                        for item in traversal.preorder_traverse(
                                tree=self._tree):
                            print(item)
                elif arg == "in":
                    if isinstance(
                            self._tree, threaded_binary_tree.
                            RightThreadedBinaryTree) or isinstance(
                                self._tree,
                                threaded_binary_tree.DoubleThreadedBinaryTree):
                        for item in self._tree.inorder_traverse():
                            print(item)
                    else:
                        for item in traversal.inorder_traverse(
                                tree=self._tree):
                            print(item)
                elif arg == "post":
                    for item in traversal.postorder_traverse(tree=self._tree):
                        print(item)
                elif arg == "rev-in":
                    if isinstance(
                            self._tree, threaded_binary_tree.
                            LeftThreadedBinaryTree) or isinstance(
                                self._tree,
                                threaded_binary_tree.DoubleThreadedBinaryTree):
                        for item in self._tree.reverse_inorder_traverse():
                            print(item)
                    else:
                        for item in traversal.reverse_inorder_traverse(
                                tree=self._tree):
                            print(item)
                else:
                    print(f"{arg} is an invalid traversal type")
        except KeyError as error:
            print(error)
Beispiel #5
0
def test_binary_search_tree_traversal(basic_tree):
    """Test binary search tree traversal."""
    tree = binary_search_tree.BinarySearchTree()

    for key, data in basic_tree:
        tree.insert(key=key, data=data)

    assert [item for item in traversal.postorder_traverse(tree)] == [
        (1, "1"),
        (7, "7"),
        (15, "15"),
        (22, "22"),
        (20, "20"),
        (11, "11"),
        (4, "4"),
        (24, "24"),
        (34, "34"),
        (30, "30"),
        (23, "23"),
    ]

    assert [item for item in traversal.postorder_traverse(tree, False)] == [
        (1, "1"),
        (7, "7"),
        (15, "15"),
        (22, "22"),
        (20, "20"),
        (11, "11"),
        (4, "4"),
        (24, "24"),
        (34, "34"),
        (30, "30"),
        (23, "23"),
    ]

    assert [item for item in traversal.preorder_traverse(tree)] == [
        (23, "23"),
        (4, "4"),
        (1, "1"),
        (11, "11"),
        (7, "7"),
        (20, "20"),
        (15, "15"),
        (22, "22"),
        (30, "30"),
        (24, "24"),
        (34, "34"),
    ]

    assert [item for item in traversal.preorder_traverse(tree, False)] == [
        (23, "23"),
        (4, "4"),
        (1, "1"),
        (11, "11"),
        (7, "7"),
        (20, "20"),
        (15, "15"),
        (22, "22"),
        (30, "30"),
        (24, "24"),
        (34, "34"),
    ]

    assert [item for item in traversal.inorder_traverse(tree)] == [
        (1, "1"),
        (4, "4"),
        (7, "7"),
        (11, "11"),
        (15, "15"),
        (20, "20"),
        (22, "22"),
        (23, "23"),
        (24, "24"),
        (30, "30"),
        (34, "34"),
    ]

    assert [item for item in traversal.inorder_traverse(tree, False)] == [
        (1, "1"),
        (4, "4"),
        (7, "7"),
        (11, "11"),
        (15, "15"),
        (20, "20"),
        (22, "22"),
        (23, "23"),
        (24, "24"),
        (30, "30"),
        (34, "34"),
    ]

    assert [item for item in traversal.reverse_inorder_traverse(tree)] == [
        (34, "34"),
        (30, "30"),
        (24, "24"),
        (23, "23"),
        (22, "22"),
        (20, "20"),
        (15, "15"),
        (11, "11"),
        (7, "7"),
        (4, "4"),
        (1, "1"),
    ]

    assert [item
            for item in traversal.reverse_inorder_traverse(tree, False)] == [
                (34, "34"),
                (30, "30"),
                (24, "24"),
                (23, "23"),
                (22, "22"),
                (20, "20"),
                (15, "15"),
                (11, "11"),
                (7, "7"),
                (4, "4"),
                (1, "1"),
            ]