def test_rebalance_rotate_right(): tree = BSTree(3) tree.left = BSTree(2, tree) tree.left.left = BSTree(1, tree.left) 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
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