本文整理汇总了Python中bst.BST.contains方法的典型用法代码示例。如果您正苦于以下问题:Python BST.contains方法的具体用法?Python BST.contains怎么用?Python BST.contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BST
的用法示例。
在下文中一共展示了BST.contains方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_contains
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [as 别名]
def test_contains():
bst = BST()
for i in range(1,11):
bst.insert(i)
assert bst.contains(1)
assert bst.contains(5)
assert not bst.contains(15)
示例2: test_delete_head_only
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [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
示例3: test_delete_head_one_right_child
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [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
示例4: test_contains
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [as 别名]
def test_contains():
tree = BST()
tree.insert(0)
tree.insert(1)
tree.insert(-1)
tree.insert(2)
tree.insert(-2)
assert tree.contains(-1)
assert tree.contains(0)
assert tree.contains(4) is False
示例5: test_contains
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [as 别名]
def test_contains():
btree = BST()
btree.insert(3)
btree.insert(2)
btree.insert(5)
btree.insert(4)
btree.insert(1)
assert btree.contains(5) is True
assert btree.contains(1) is True
assert btree.contains(10) is False
示例6: BSTTestCase
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [as 别名]
class BSTTestCase(unittest.TestCase):
def setUp(self):
self.tree = BST()
self.nodes = {5: 'five', 7: 'seven', 2: 'two', 3: 'three', 1: 'one', 8: 'eight'}
for k, v in self.nodes.items():
self.tree.put(k,v)
def tearDown(self):
del self.tree
del self.nodes
def testPutGet(self):
for k, v in self.nodes.items():
self.assertEqual(v, self.tree.get(k))
for k in [4,6,9]:
self.assertIsNone(self.tree.get(k))
def testLen(self):
self.assertEqual(6, len(self.tree))
def testMinKey(self):
self.assertEqual(1, self.tree.minKey())
def testMin(self):
self.assertTupleEqual((1, 'one'), self.tree.min())
def testMaxKey(self):
self.assertEqual(8, self.tree.maxKey())
def testMax(self):
self.assertTupleEqual((8, 'eight'), self.tree.max())
def testIsEmpty(self):
self.assertFalse(self.tree.isEmpty())
self.assertTrue(BST().isEmpty())
def testItems(self):
self.assertSequenceEqual(sorted(self.nodes.items()), list(self.tree.items()))
def testKeys(self):
self.assertSequenceEqual(sorted(self.nodes.keys()), list(self.tree.keys()))
def testContains(self):
self.assertTrue(self.tree.contains(8))
self.assertFalse(self.tree.contains(13))
def testKeysBetween(self):
self.assertSequenceEqual([2,3,5], list(self.tree.keys_between(2, 6)))
def testNumKeysBetween(self):
# self.nodes = {5: 'five', 7: 'seven', 2: 'two', 3: 'three', 1: 'one', 8: 'eight'}
self.assertEqual(4, self.tree.num_between(3, 8))
def testRank(self):
self.assertEqual(2, self.tree.rank(3))
示例7: test_delete_head_two_children
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [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
示例8: test_delete_one_item
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [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)
示例9: test_contains
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [as 别名]
def test_contains():
"""contains(val) should return True if val is in the BST, False if not."""
bintree = BST()
with pytest.raises(TypeError):
bintree.insert()
with pytest.raises(TypeError):
bintree.insert(1, 2)
# Will accept any value including None; behavior is on the user
assert bintree.contains(None) is False
assert bintree.contains(1) is False
bintree.insert(1)
assert bintree.contains(1) is True
for i in xrange(-5, 5):
bintree.insert(i)
for i in xrange(-5, 5):
assert bintree.contains(i) is True
assert bintree.contains(None) is False
示例10: test_delete_one_child
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [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
示例11: test_delete
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import contains [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)