本文整理汇总了Python中bst.BinarySearchTree.search方法的典型用法代码示例。如果您正苦于以下问题:Python BinarySearchTree.search方法的具体用法?Python BinarySearchTree.search怎么用?Python BinarySearchTree.search使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.search方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_search
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import search [as 别名]
def test_search(self):
# Emptry Tree
self.assertEquals(BinarySearchTree().search(8), None)
tree = BinarySearchTree()
tree.insert(8)
tree.insert(3)
tree.insert(10)
tree.insert(1)
tree.insert(6)
tree.insert(4)
tree.insert(7)
tree.insert(14)
tree.insert(13)
self.assertEquals(tree.search(6).value, 6)
self.assertEquals(tree.search(2), None)
self.assertEquals(tree.search(15), None)
self.assertEquals(tree.search(13).value, 13)
self.assertEquals(tree.search(1).value, 1)
示例2: replace
# 需要导入模块: from bst import BinarySearchTree [as 别名]
# 或者: from bst.BinarySearchTree import search [as 别名]
if __name__ == "__main__":
def replace(string):
"""
Search uses regex for searching with wildcards.
The . (dot) is any char.
"""
return string.replace('*', '.', 25)
parser = argparse.ArgumentParser(description="Binary Search Tree. Builds\
a tree from a file containing a list of\
words. Then searches that tree for the\
inputed word. Wildcards(*) are allowed.")
parser.add_argument('file', type=str, help="File location")
parser.add_argument('word', type=str, help="Word to search")
args = parser.parse_args()
search_value = replace(args.word)
with open(args.file, 'r') as reader:
print("Building binary search tree...")
bst = BinarySearchTree()
for line in reader:
bst.insert(line.rstrip())
words = bst.search(search_value)
"""
There's a issue in the bst search function that returns None.
Lazy fix is list comprehension to remove all Nones.
"""
words = [word for word in words if word is not None]
print("Following words were matched: \n" + str(words))