Ejemplo n.º 1
0
def test_rebalance_rotate_left():
    tree = BSTree(1)
    tree.right = BSTree(2, tree)
    tree.right.right = BSTree(3, tree.right)
    tree.rebalance()
    actual = [n.key for n in tree.breadth_first()]
    expected = [2, 1, 3]
    assert tree.depth() == 2
    assert tree.balance() == 0
    assert actual == expected
Ejemplo n.º 2
0
def traverse_ex():
    tree = BSTree(8)
    tree.left = BSTree(3, tree)
    tree.right = BSTree(10, tree)
    tree.left.left = BSTree(1, tree.left)
    tree.left.right = BSTree(6, tree.left)
    tree.right.right = BSTree(14, tree.right)
    tree.left.right.left = BSTree(4, tree.left.right)
    tree.left.right.right = BSTree(7, tree.left.right)
    tree.right.right.left = BSTree(13, tree.right.right)
    return tree
Ejemplo n.º 3
0
def tree_rebalance_right_right():
    tree = BSTree(2)
    tree.left = BSTree(1, tree)
    tree.right = BSTree(4, tree)
    tree.right.left = BSTree(3, tree.right)
    tree.right.right = BSTree(6, tree.right)
    tree.right.right.left = BSTree(5, tree.right.right)
    tree.right.right.right = BSTree(7, tree.right.right)
    tree.rebalance()
    actual = [n.key for n in tree.breadth_first()]
    expected = [4, 2, 6, 1, 3, 5, 7]
    assert tree.depth() == 3
    assert tree.balance() == 0
    assert actual == expected