本文整理汇总了Python中bst.BST.find方法的典型用法代码示例。如果您正苦于以下问题:Python BST.find方法的具体用法?Python BST.find怎么用?Python BST.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BST
的用法示例。
在下文中一共展示了BST.find方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BSTDict
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import find [as 别名]
class BSTDict(BST):
def __init__(self):
self._table = BST()
def __getitem__(self, key):
"""Returns the value associated with key or
returns None if key does not exist."""
entry = Entry(key, None)
result = self._table.find(entry)
if result == None:
return None
else:
return result.getValue()
def __setitem__(self, key, value):
"""Inserts an entry with key/value if key
does not exist or replaces the existing value
with value if key exists."""
entry = Entry(key, value)
result = self._table.find(entry)
if result == None:
self._table.add(entry)
else:
result.setValue(value)
def __contains__(self, key):
"""Returns True if key in BSTDict otherwise returns False."""
entry = Entry(key, None)
result = self._table.find(entry)
if result == None:
return False
else:
return True
def __len__(self):
"""Returns the length of the BST Dictionary"""
return len(self._table)
def __iter__(self):
"""Iterates through the key/values in the BST"""
return iter(self._table)
def __str__(self):
"""Returns unordered string of the dictionary and associated values in tree format."""
return str(self._table)
示例2: bst_search_run
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import find [as 别名]
def bst_search_run(data):
# Create a BST
bst = BST()
# Insert elements
for i in xrange(size):
bst.insert(i)
# Now, get time
start = time.clock()
# Find elements
for i in data:
bst.find(i)
# Get final time, and store
end = time.clock()
return {
'structure': 'bst',
'size': size,
'time': end - start,
'comparisons': bst.comparisons
}
示例3: TreeSet
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import find [as 别名]
class TreeSet(object):
"""A tree-based implementation of a sorted set."""
def __init__(self):
self._items = BST()
def __contains__(self, item):
"""Returns True if item is in the set or
False otherwise."""
return self._items.find(item) != None
def add(self, item):
"""Adds item to the set if it is not in the set."""
if not item in self:
self._items.add(item)