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


Python BST.size方法代码示例

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


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

示例1: test_rebalance

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [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_size

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

示例3: test_delete_no_children

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_delete_no_children():
    """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()
    with pytest.raises(TypeError):
        bintree.delete()  # 1 value required

    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(-1) is None
    assert bintree.size() == 8
    assert bintree.delete(4) is None
    assert bintree.size() == 7
    assert not bintree.leftchild.rightchild  # 4 is gone
    assert bintree.delete(10) is None
    assert bintree.size() == 6
    assert not bintree.rightchild.rightchild.leftchild  # 9 is a leaf
    assert not bintree.rightchild.rightchild.rightchild
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:34,代码来源:test_bst.py

示例4: test_size

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

示例5: test_delete_one_child

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [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

示例6: test_delete_two_children

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [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

示例7: test_delete

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_delete():
    tree = BST()
    for n in [0, 2, -2, 1, -1, 3, -3]:
        tree.insert(n)
    for n in [-3, -2, -1, 0, 1, 2, 3]:
        assert tree.contains(n)
    tree.delete(-3)
    assert tree.contains(-3) is False
    tree.delete(-2)
    assert tree.contains(-2) is False
    for n in [-1, 0, 1, 2, 3]:
        assert tree.contains(n)
    assert tree.size() == 5
    tree.insert(5)
    tree.insert(4)
    tree.insert(6)
    tree.delete(3)
    assert tree.size() == 7
    for i in [-1, 0, 1, 2, 4, 5, 6]:
        assert tree.contains(i)
开发者ID:charlieRode,项目名称:data-structures,代码行数:22,代码来源:test_bst.py

示例8: test_find_minimum_and_delete

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_find_minimum_and_delete():
    """Should return the minimum value in a (sub)tree and delete that leaf"""
    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._find_minimum_and_delete() == 1
    #     5
    #    / \
    #   3   7
    #  /\    \
    # 2  4    9
    #         |
    #         10
    assert bintree.size() == 7
    bintree.rightchild._find_minimum_and_delete() == 7
    # The "head" of the right subtree is deleted in this case
    #     5
    #    / \
    #   3   9
    #  /\    \
    # 2  4    10
    assert bintree.size() == 6
    assert bintree.rightchild.value == 9
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:39,代码来源:test_bst.py

示例9: test_size

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_size():
    """size(self) should return the integer size of the BST (total number of
    values stored in the tree), 0 if the tree is empty."""
    bintree = BST()
    with pytest.raises(TypeError):
        bintree.size(1)  # No values allowed

    assert bintree.size() == 0
    bintree.insert(5)
    assert bintree.size() == 1
    for i in xrange(10):
        bintree.insert(i)
    # 5 will be discarded (This is the first time we can really test this)
    assert bintree.size() == 10
    bintree.insert(10)
    assert bintree.size() == 11
    bintree.insert(-1)
    assert bintree.size() == 12
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:20,代码来源:test_bst.py

示例10: test_in_order_traversal

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_in_order_traversal():
    """Returns a generator of values in the tree using in-order traversal"""
    import types
    bintree = BST()
    with pytest.raises(TypeError):
        bintree.in_order(1)  # No args/kwargs
    with pytest.raises(TypeError):
        bintree.in_order(None)

    assert isinstance(bintree.in_order(), types.GeneratorType)

    testlist = []
    for i in xrange(30):
        testval = random.random()
        bintree.insert(testval)
        testlist.append(testval)

    assert bintree.size() == 30
    ordered_generator = bintree.in_order()
    testlist.sort()

    for number in ordered_generator:
        assert number == testlist.pop(0)
开发者ID:jbbrokaw,项目名称:data-structures,代码行数:25,代码来源:test_bst.py

示例11: test_size_empty

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

示例12: test_size_empty_tree

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_size_empty_tree():
    """Assert an empty tree has a size of zero."""
    from bst import BST
    b = BST([])
    assert b.size() == 0
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:7,代码来源:test_bst.py

示例13: test_size_one

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_size_one():
    """Test size function works on a single Node Tree."""
    from bst import BST
    d = BST()
    d.insert(20)
    assert d.size() == 1
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:8,代码来源:test_bst.py

示例14: test_size_empty

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import size [as 别名]
def test_size_empty():
    """Test size function works on an empty tree."""
    from bst import BST
    c = BST()
    assert c.size() == 0
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:7,代码来源:test_bst.py


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