本文整理汇总了Python中bst.BST.add方法的典型用法代码示例。如果您正苦于以下问题:Python BST.add方法的具体用法?Python BST.add怎么用?Python BST.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BST
的用法示例。
在下文中一共展示了BST.add方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_inorder
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import add [as 别名]
def test_inorder():
nums = range(1, 200)
random.shuffle(nums)
bst = BST()
for n in nums:
bst.add(n)
nums.sort()
assert bst.inorder() == sorted(nums)
示例2: grade
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import add [as 别名]
def grade(self, array):
bst = BST()
for i in array:
bst.add(i)
array = sorted(array)
ptr = 0
for i in bst:
self.assertEqual(i, array[ptr])
ptr += 1
示例3: bstsort
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import add [as 别名]
def bstsort(l):
arbol = BST()
# Generamos nuestra estructura
# de datos auxiliar
while l:
# Vaciamos la lista en el arbol
item = l.pop()
arbol.add(item)
l.extend(arbol.inorder())
示例4: TreeSet
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import add [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)
示例5: BSTDict
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import add [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)
示例6: createConcordance
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import add [as 别名]
def createConcordance(inputFile, stopWords, outputFile):
#declare data structures
stopSet = HashSet(3500)
dictionary = HashDictionary(10000)
bst = BST()
#declare regular expressions
newLine = re.compile(r'\n')
exprSpaces = re.compile(r'\s')
dhyp = re.compile('--')
notChars = re.compile('\W|-')
#populate hashset with stop words
stopWords = open(stopWords, 'r')
for stop in stopWords:
x = newLine.search(stop)
stop = stop[:x.start()]
if stop == "":
break
stopSet.add(stop)
stopWords.close()
#open the input and process into words
f = open(inputFile, 'r')
lineNum = 0
while True:
line = f.readline()
lineNum += 1
if line == "":
break
#split lines
m = dhyp.split(line)
alist = []
for i in range(len(m)):
g = exprSpaces.split(m[i])
alist = alist + g
#strip down to words
print alist
for word in alist:
if word == None:
pass
else:
word = string.lower(word)
while True:
n = notChars.search(word)
if len(word) <= 0:
break
elif n != None:
if n.start() == 0:
word = word[n.end():]
else:
word = word[:n.start()]
else:
break
#check if word is stop word
if not stopSet.contains(word) and len(word) > 0:
#if word isn't already in dictionary
if dictionary.search(word) == None:
linkedValue = LinkedIndexedList()
dictionary.store(word, linkedValue)
dictionary.search(word).append(lineNum)
bst.add(word)
#if the word is in the dictionary
else:
dictionary.search(word).append(lineNum)
f.close()
#open output and use BST to print out words
# in alphabetical order
output = open(outputFile, 'w')
lyst = bst.inorder()
temp = None
for item in lyst:
temp = dictionary.search(item)
output.write(item + " - " + str(temp)+"\n")
output.close()