本文整理汇总了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
示例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)
示例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
示例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
示例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]
示例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
示例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
示例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
示例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)