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


Python BST.delete方法代码示例

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


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

示例1: test_delete_head_only

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import delete [as 别名]
def test_delete_head_only():
    """Test deleteing a one node head."""
    from bst import BST
    b = BST([20])
    assert b.contains(20) is True
    b.delete(20)
    assert b.contains(20) is False
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:9,代码来源:test_bst.py

示例2: test_delete_one_item

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import delete [as 别名]
def test_delete_one_item():
    from bst import BST
    bst = BST()
    bst.insert(42)
    bst.delete(42)
    assert not bst._search(42)
    assert not bst.contains(42)
开发者ID:KyleRichVA,项目名称:data-structures,代码行数:9,代码来源:test_bst.py

示例3: test_delete_no_children

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

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import delete [as 别名]
def test_delete_head_one_right_child():
    """Test deleting a head with one right child."""
    from bst import BST
    b = BST([20])
    b.insert(22)
    assert b.contains(20) is True
    b.delete(20)
    assert b.contains(20) is False
    assert b.top.value == 22
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:11,代码来源:test_bst.py

示例5: test_delete_middle_of_tree

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import delete [as 别名]
def test_delete_middle_of_tree():
    t = BST()
    t.insert(3)
    t.insert(2)
    t.insert(5)
    t.insert(1)
    t.insert(4)
    t.insert(6)
    t.insert(7)
    t.delete(5)
    assert list(t.pre_order()) == [3, 2, 1, 6, 4, 7]
开发者ID:kaka0525,项目名称:data-strutctures-partII,代码行数:13,代码来源:test_bst.py

示例6: test_delete_head_two_children

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import delete [as 别名]
def test_delete_head_two_children():
    """Test deleting a head with two children."""
    from bst import BST
    b = BST([20])
    b.insert(18)
    b.insert(22)
    assert b.contains(20) is True
    b.delete(20)
    assert b.contains(20) is False
    assert b.top.value == 22
    assert b.top.left.value == 18
    assert b.top.right is None
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:14,代码来源:test_bst.py

示例7: test_delete_one_child

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

示例8: test_delete_two_children

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

示例9: test_delete

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


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