Example #1
0
def test_fast_merge_separated_bigger_right():
    """Test fast merge separated tree bigger on the right."""
    # Populate trees
    for i in range(tree_ops):
        # Create tree to be merge
        ltree = AVLBinarySearchTree()
        keys = set()
        for i in range(tree_inserts):
            key = random.randint(min_key, (max_key - min_key) // 2 + min_key)
            keys.add(key)
            ltree.insert(AVLBinarySearchTreeNode(key))
        rtree = AVLBinarySearchTree()
        for i in range(tree_inserts):
            key = random.randint((max_key - min_key) // 2 + min_key + 1,
                                 max_key)
            keys.add(key)
            rtree.insert(AVLBinarySearchTreeNode(key))
        print(ltree)
        print()
        print(rtree)
        ltree.merge(rtree)
        print()
        print(ltree)
        assert_search_binary_tree_invariance(keys, ltree)
        assert_avl_invariance(keys, ltree)
Example #2
0
def test_insert():
    """Test insert operation."""
    # Create the tree
    tree = AVLBinarySearchTree()
    # Populate tree with insert
    keys = set()
    for i in range(tree_inserts):
        key = random.randint(min_key, max_key)
        keys.add(key)
        tree.insert(AVLBinarySearchTreeNode(key))
    # Assert basic tree conditions
    assert_search_binary_tree_invariance(keys, tree)
    assert_avl_invariance(keys, tree)
Example #3
0
def main():
    """Main example."""
    # Create the trees
    tree = AVLBinarySearchTree()

    # Words
    words = """
        this was the biggest disappointment of our trip. the restaurant had received some very good reviews, so our expectations were high.
        the service was slow even though the restaurant was not very full.
    """

    # Mercle tree
    for word in words.split():
        tree.insert(Node(word.strip()))

    # Print original tree
    print('Initial mercle tree:')
    print(tree)
    print()
Example #4
0
def test_slow_merge_overlapped_trees():
    """Test slow merge for overlapping trees."""
    # Populate trees
    for i in range(tree_ops):
        # Create tree to be merge
        ltree = AVLBinarySearchTree()
        keys = set()
        for i in range(tree_inserts):
            key = random.randint(min_key, max_key)
            keys.add(key)
            ltree.insert(AVLBinarySearchTreeNode(key))
        rtree = AVLBinarySearchTree()
        for i in range(tree_inserts):
            key = random.randint(min_key, max_key)
            keys.add(key)
            rtree.insert(AVLBinarySearchTreeNode(key))
        ltree.merge(rtree)
        assert_search_binary_tree_invariance(keys, ltree)
        assert_avl_invariance(keys, ltree)
Example #5
0
def test_delete():
    """Test delete operation."""
    # Create the tree
    tree = AVLBinarySearchTree()
    # Populate tree with insert
    keys = set()
    for i in range(tree_inserts):
        key = random.randint(min_key, max_key)
        keys.add(key)
        tree.insert(AVLBinarySearchTreeNode(key))
    # Assert tree invariances after multiple deletions
    for n in range(tree_inserts - 1, tree_deletes - 1, -1):
        # Delete one node
        key = random.sample(keys, k=1)[0]
        keys -= set([key])
        node = tree.delete(key)
        assert key == node.key
        assert_deleted_node(node)
        assert_search_binary_tree_invariance(keys, tree)
        assert_avl_invariance(keys, tree)
Example #6
0
def main():
    """Main example."""
    # Create the trees
    ltree = AVLBinarySearchTree()
    rtree = AVLBinarySearchTree()

    # Populate original tree
    for i in range(1, 41):
        ltree.insert(Node(i))

    # Print original tree
    print('Original tree:')
    print(ltree)
    print()

    print('Split original tree into left and right trees')
    rtree = ltree.split(36)
    print()

    print('Left tree:')
    print(ltree)
    print()
    print('Right tree:')
    print(rtree)
    print()

    # Merge the trees
    print('Merge rtree into ltree')
    ltree.merge(rtree)
    print()

    # Print output
    print('Left tree:')
    print(ltree)
    print()

    print('Right tree:')
    print(rtree)
    print()
Example #7
0
def main():
    """Main example."""
    # Create the trees
    ltree = AVLBinarySearchTree()
    rtree = AVLBinarySearchTree()

    # Populate original tree
    for i in range(1, 41):
        ltree.insert(Node(i))

    # Print original tree
    print('Original tree:')
    print(ltree)
    print()

    print('Split original tree for key = 36')
    rtree = ltree.split(36)
    print()

    print('Left tree:')
    print(ltree)
    print()

    print('The root of the left tree should be (36+37)/2 = 666')
    print()

    # Merge the trees
    print('Split original left tree for key = 33')
    rtree = ltree.split(33)
    print()

    # Print output
    print('Right tree:')
    print(rtree)
    print()

    print('The root of the right tree should be 34+35+36 = 105')
    print()
Example #8
0
def test_split_tree():
    """Test tree splitting."""
    for i in range(tree_ops):
        # Create the tree
        ltree = AVLBinarySearchTree()
        # Populate tree with insert
        keys = set()
        akeys = []
        for i in range(tree_inserts):
            key = random.randint(min_key, max_key)
            keys.add(key)
            akeys.append(key)
            ltree.insert(AVLBinarySearchTreeNode(key))
        # Select separation key
        skey = random.sample(keys, k=1)[0]
        # Split the tree
        rtree = ltree.split(skey)
        # Split the set of keys
        lkeys = set()
        rkeys = set()
        for key in keys:
            if key <= skey:
                lkeys.add(key)
            else:
                rkeys.add(key)
        # Check invariances for ltree
        assert_search_binary_tree_invariance(lkeys, ltree)
        assert_avl_invariance(lkeys, ltree)
        # Check invariances for rtree
        assert_search_binary_tree_invariance(rkeys, rtree)
        assert_avl_invariance(rkeys, rtree)
        # Merge the tree
        ltree.merge(rtree)
        # Check invariance of merged tree
        assert_search_binary_tree_invariance(lkeys | rkeys, ltree)
        assert_avl_invariance(lkeys | rkeys, ltree)