当前位置: 首页>>代码示例>>Python>>正文


Python BST.balance方法代码示例

本文整理汇总了Python中bst.BST.balance方法的典型用法代码示例。如果您正苦于以下问题:Python BST.balance方法的具体用法?Python BST.balance怎么用?Python BST.balance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bst.BST的用法示例。


在下文中一共展示了BST.balance方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_rebalance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_rebalance():
    """Tests my rebalancing algorithm"""
    bintree = BST()

    for i in xrange(100):
        bintree.insert(random.randint(0, 1e6))
    assert bintree.size() == 100

    original_depth = bintree.depth()

    bintree.rebalance()
    assert abs(bintree.balance()) < 2
    assert original_depth > bintree.depth()

    bintree = BST()

    for i in xrange(100):
        bintree.insert(i)  # Horribly unbalanced
    assert bintree.size() == 100
    assert bintree.depth() == 100
    assert bintree.balance() == -99

    bintree.rebalance()
    assert abs(bintree.balance()) < 2
    assert bintree.depth() < 10  # Much better, anyway
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:27,代码来源:test_bst.py

示例2: test_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_balance():
    aBST = BST()
    assert aBST.balance() == 0
    for num, bal in zip([4, 2, 6, 3, 7, 8, 9, 10, 11],
                        [0, 1, 0, 1, 0, -1, -2, -3]):
        aBST.insert(num)
        assert aBST.balance() == bal
开发者ID:tpeek,项目名称:data-structures-1,代码行数:9,代码来源:test_bst.py

示例3: test_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_balance():
    tree = BST()
    assert tree.balance() == 0
    items = [0, -2, -1, -4, -3]
    for item in items:
        tree.insert(item)
    assert tree.balance() == 3
    items = [1, 2, 3, 4]
    for item in items:
        tree.insert(item)
    assert tree.balance() == -1
开发者ID:charlieRode,项目名称:data-structures,代码行数:13,代码来源:test_bst.py

示例4: test_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_balance():
    """balance(self) should return a positive or negative integer that
    represents how well balanced the tree is. Trees deeper on the left return a
    positive value, trees deeper on the right return a negative value.
    An ideally-balanced tree should return 0."""
    bintree = BST()
    with pytest.raises(TypeError):
        bintree.balance(1)  # No values allowed

    assert bintree.balance() == 0  # Empty tree
    bintree.insert(5)
    #   5
    assert bintree.balance() == 0  # no children, it's balanced
    bintree.insert(3)
    #   5
    #  /
    # 3
    assert bintree.balance() == 1  # Deeper on the left
    bintree.insert(7)
    #   5
    #  / \
    # 3   7
    assert bintree.balance() == 0
    bintree.insert(2)
    bintree.insert(4)
    #     5
    #    / \
    #   3   7
    #  /\
    # 2  4
    assert bintree.balance() == 1  # Deeper on the left
    bintree.insert(1)
    #     5
    #    / \
    #   3   7
    #  /\
    # 2  4
    # |
    # 1
    assert bintree.balance() == 2
    bintree.insert(9)
    bintree.insert(10)
    #     5
    #    / \
    #   3   7
    #  /\    \
    # 2  4    9
    # |       |
    # 1       10
    assert bintree.balance() == 0
    bintree.insert(12)
    assert bintree.balance() == -1
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:54,代码来源:test_bst.py

示例5: test_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_balance():
    b = BST()
    b.insert(10)
    b.insert(5)
    b.insert(15)
    b.insert(3)
    b.insert(2)
    b.insert(1)
    assert b._depth == 5
    assert b._size == 6
    assert b.balance(b._root) == -3
    assert b.balance(b._root._left) == -3
    assert b.balance(b._root._right) == -0
开发者ID:corinnelhh,项目名称:data-structures,代码行数:15,代码来源:test_bst.py

示例6: test_right_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_right_balance():
    bst = BST()
    bst.insert(5)
    bst.insert(6)
    bst.insert(7)
    bst.insert(3)
    assert bst.balance() == -1
开发者ID:EyuelAbebe,项目名称:data-structures1,代码行数:9,代码来源:test_bst.py

示例7: test_left_balance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_left_balance():
    bst = BST()
    bst.insert(5)
    bst.insert(4)
    bst.insert(3)
    bst.insert(6)
    assert bst.balance() == 1
开发者ID:EyuelAbebe,项目名称:data-structures1,代码行数:9,代码来源:test_bst.py

示例8: test_make_balanced_randomly

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_make_balanced_randomly():
    aBST = BST()
    for y in range(300):
        for x in range(1000):
            aBST.insert(randint(0, 1000))
        aBST.make_balanced()
        b = aBST.balance()
        assert b in [1, 0, -1]
开发者ID:tpeek,项目名称:data-structures-1,代码行数:10,代码来源:test_bst.py

示例9: test_delete_one_child

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_delete_one_child():
    """delete(self, val) should remove val from the tree if present,
    if not present it should do nothing. Return None in all cases"""
    bintree = BST()
    bintree.insert(5)
    bintree.insert(3)
    bintree.insert(7)
    bintree.insert(2)
    bintree.insert(4)
    bintree.insert(1)
    bintree.insert(9)
    bintree.insert(10)
    #     5
    #    / \
    #   3   7
    #  /\    \
    # 2  4    9
    # |       |
    # 1       10
    assert bintree.size() == 8
    assert bintree.delete(7) is None
    #     5
    #    / \
    #   3   9
    #  /\    \
    # 2  4    10
    # |
    # 1
    assert bintree.size() == 7
    assert not bintree.contains(7)
    assert bintree.contains(9)
    assert bintree.contains(10)
    assert bintree.balance() == 1
    assert bintree.delete(2) is None
    #     5
    #    / \
    #   3   9
    #  /\    \
    # 1  4    10
    assert bintree.size() == 6
    assert not bintree.contains(2)
    assert bintree.contains(1)
    assert bintree.leftchild.leftchild.value == 1
    assert bintree.balance() == 0
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:46,代码来源:test_bst.py

示例10: test_delete_two_children

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_delete_two_children():
    bintree = BST()
    bintree.insert(5)
    bintree.insert(3)
    bintree.insert(7)
    bintree.insert(6)
    bintree.insert(2)
    bintree.insert(4)
    bintree.insert(1)
    bintree.insert(9)
    bintree.insert(10)
    #      5
    #    /   \
    #   3     7
    #  /\    / \
    # 2  4  6   9
    # |         |
    # 1         10
    assert bintree.size() == 9
    assert bintree.leftchild.balance() == 1  # The subtree at 3 is left-heavy
    assert bintree.delete(3) is None  # So this will delete left-style
    #      5
    #    /   \
    #   2     7
    #  /\    / \
    # 1  4  6   9
    #           |
    #          10
    assert bintree.size() == 8
    assert bintree.leftchild.value == 2
    assert bintree.balance() == -1  # The whole tree is now right-heavy
    assert bintree.delete(5) is None  # So deleting the top will go right-style
    #      6
    #    /   \
    #   2     7
    #  /\      \
    # 1  4      9
    #           |
    #          10
    assert bintree.value == 6
    assert bintree.size() == 7
    assert bintree.rightchild.value == 7
    assert bintree.balance() == -1
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:45,代码来源:test_bst.py

示例11: test_balance_1_node

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_balance_1_node():
    """Test balance of tree of only head."""
    from bst import BST
    new_bst = BST()
    new_bst.insert(10)
    assert new_bst.balance() == 0
开发者ID:palindromed,项目名称:data-structures2,代码行数:8,代码来源:test_bst.py

示例12: test_balance_empty_tree

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import balance [as 别名]
def test_balance_empty_tree():
    """Test balance of empty tree."""
    from bst import BST
    new_bst = BST()
    assert new_bst.balance() == 0
开发者ID:palindromed,项目名称:data-structures2,代码行数:7,代码来源:test_bst.py


注:本文中的bst.BST.balance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。