本文整理汇总了Python中bst.BinarySearchTree.get方法的典型用法代码示例。如果您正苦于以下问题:Python BinarySearchTree.get方法的具体用法?Python BinarySearchTree.get怎么用?Python BinarySearchTree.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.get方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_delete_without_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import get [as 别名]
def test_delete_without_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.delete(6)
assert(tree.get(3).childcount() == 0)
示例2: test_parent_pointer_after_delete
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import get [as 别名]
def test_parent_pointer_after_delete():
tree = BinarySearchTree()
tree.add(1)
tree.add(3)
tree.add(6)
tree.delete(3)
assert(tree.get(6).parent.value == 1)
示例3: test_delete_successor_with_children
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import get [as 别名]
def test_delete_successor_with_children():
tree = BinarySearchTree()
tree.add(1)
tree.add(5)
tree.add(4)
tree.add(10)
tree.add(9)
tree.add(7)
tree.add(8)
tree.delete(5)
assert(tree.get(9).get_only_child().value == 8)
示例4: main
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import get [as 别名]
def main():
name = sys.argv[1];
for i in xrange(1, 11):
fname = '../data/%d_catalog.txt' % i
#read the catalog and load to a list
phonebook = []
append = phonebook.append
with open(fname, 'r') as f:
for l in f:
if l:
append(Entry.fromline(l))
#print 'phonebook contains %d entries' % len(phonebook)
name_idx = BinarySearchTree()
for i, e in enumerate(phonebook):
name_idx.add(e.name, i)
start = time.time()
idxs = name_idx.get(name)
bstelapsed = (time.time() - start) * 1000
# let's sort the entries by name
phonebook.sort(key=lambda e: e.name)
# let' search for a non existing name using naive search
start = time.time()
idx = search.naive(phonebook, name)
nelapsed = (time.time() - start) * 1000
# the same using binary search
bstart = time.time()
idx = search.binary_search(phonebook, name)
bselapsed = (time.time() - bstart) * 1000
print '%d - naive: %g - binary: %g - bst: %g'\
% (len(phonebook), nelapsed, bselapsed, bstelapsed)
示例5: test_element_get_simple
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import get [as 别名]
def test_element_get_simple(self):
""" Get simple element."""
a = BinarySearchTree()
a[7] = 'Harry'
self.assertTrue(a.get(7) == 'Harry')
示例6: test_element_get_empty
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import get [as 别名]
def test_element_get_empty(self):
""" Get simple element."""
a = BinarySearchTree()
self.assertTrue(a.get(7) == None)
示例7: BinarySearchTree
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import get [as 别名]
from bst import BinarySearchTree
bst = BinarySearchTree()
bst.put('3', 'dog')
bst.put('8', 'pig')
bst.put('4', 'goat')
bst.put('2', 'lion')
bst.put('1', 'cat')
bst.put('5', "horse")
for item in bst:
print(item, bst[item])
print(bst.get('2'))
bst['2'] = 'rabbit'
print(bst['2'])
bst.delete('2')
try:
del bst['2']
except KeyError:
print("Not in the BST!")
#for item in bst:
# print(item)