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


Python BinarySearchTree.search方法代码示例

本文整理汇总了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)
开发者ID:Aravind-Shankar,项目名称:Algorithm-Implementations,代码行数:21,代码来源:bst_test.py

示例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))
开发者ID:jflowaa,项目名称:datastructures,代码行数:31,代码来源:bst_tester.py


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