我有一个Tree类,它允许我在该树函数中输入句子。如果该句子有多个实例,那么它将添加到树中该句子的计数中。
我希望接下来要做的是将前10个值的句子放入数组中。但是我不知道该怎么做。如果有人能帮助我做到这一点,那就太棒了。
以下是树的完整代码:
class Branch():
def __init__(self, value):
self.left = None
self.right = None
self.value = value
self.frequency = 1
def incFreq(self):
self.frequency = self.frequency + 1
def freq(self):
return self.frequency
class Tree():
highest = []
def __init__(self):
self.root = None
self.found = False
def findHighest(self):
self.highest = [
["", 0],["", 0],["", 0],["", 0],["", 0],
["", 0],["", 0],["", 0],["", 0],["", 0] ]
self.inorder(self.root)
print(self.highest)
#lessThan function needed to compare strings
def lessThan(self, a, b):
if len(a) < len(b):
loopCount = len(a)
else:
loopCount = len(b)
for pos in range(0, loopCount):
if a[pos] > b[pos]:
return False
return True
def outputTree(self):
self.inorder(self.root)
def insert(self, value):
#increment freq if already exists, else insert
if not self.exists(value):
self.root = self.insertAtBranch(self.root, value)
def exists(self, value):
#set the class variable found to False to assume it is not there
self.found = False
self.findAtBranch(self.root, value)
return self.found
#Used to fine a value in a tree
def findAtBranch(self, branch, value):
if branch == None:
pass
else:
#print ("[" + branch.value + "][" + value + "]") # Error checking
if branch.value == value:
self.found = True
#print("found " + value)
branch.incFreq()
#print(branch.freq())
else:
self.findAtBranch(branch.left, value)
self.findAtBranch(branch.right, value)
def insertAtBranch(self, branch, value):
if branch == None:
return Branch(value)
else:
if self.lessThan(branch.value, value):
branch.right = self.insertAtBranch(branch.right, value)
else:
branch.left = self.insertAtBranch(branch.left, value)
return branch
def inorder(self, branch):
if branch == None: return
if branch.freq() > self.findHighest():
self.highest = [branch.value, branch.freq()]
#print (branch.value)
#print (branch.freq())
self.inorder(branch.left)
self.inorder(branch.right)
------------------------------Main-------------------------------------
phrasesTree = Tree()
phrasesTree.insert("Hello world")
phrasesTree.insert("Where do you live?")
phrasesTree.insert("Hello world")
phrasesTree.insert("Hello world")
phrasesTree.insert("Hello world")
phrasesTree.insert("Hello world")
phrasesTree.insert("Have you met my friend Jim?")
phrasesTree.insert("Have you met my friend Jim?")
phrasesTree.insert("Have you met my friend Jim?")
phrasesTree.insert("Where do you live?")
phrasesTree.insert("Hello this is it")
phrasesTree.insert("What is your name?")
phrasesTree.insert("These are not my shoes")
phrasesTree.insert("These are not my shoes")
phrasesTree.insert("These are not my shoes")
phrasesTree.insert("These are not my shoes")
phrasesTree.insert("These are not my shoes")
phrasesTree.insert("Where is my hat?")
phrasesTree.insert("Where is my hat?")
phrasesTree.insert("Where is my hat?")
phrasesTree.insert("How old are you?")
phrasesTree.insert("How old are you?")
phrasesTree.insert("How old are you?")
phrasesTree.insert("How old are you?")
phrasesTree.insert("Where do you live?")
phrasesTree.insert("Where do you live?")
phrasesTree.insert("Have you met my friend Jim?")
phrasesTree.insert("What time is dinner?")
phrasesTree.insert("What time is dinner?")
phrasesTree.insert("What time is dinner?")
phrasesTree.insert("What time is dinner?")
phrasesTree.insert("I have done my homework.")
phrasesTree.insert("I have done my homework.")
phrasesTree.insert("I have done my homework.")
phrasesTree.findHighest()
另一件事是,当我运行此代码时,我收到一条错误消息:
File "H:\My Documents\Year 13\COMP4\trial.3.1.3.py", line 41, in findHighest
self.inorder(self.root)
File "H:\My Documents\Year 13\COMP4\trial.3.1.3.py", line 96, in inorder
if branch.freq() > self.findHighest():
File "H:\My Documents\Year 13\COMP4\trial.3.1.3.py", line 41, in findHighest
self.inorder(self.root)
RuntimeError: maximum recursion depth exceeded
这是数组的位置:
def findHighest(self):
self.highest = [
["", 0],["", 0],["", 0],["", 0],["", 0],
["", 0],["", 0],["", 0],["", 0],["", 0] ]
self.inorder(self.root)
这是树的递归区域,我尝试将其排序到数组中:
def inorder(self, branch):
if branch == None: return
if branch.freq() > self.findHighest():
self.highest = [ branch.value, branch.freq() ]
#print (branch.value)
#print (branch.freq())
self.inorder(branch.left)
self.inorder(branch.right)
print(self.highest)
是否有人能够帮助我将具有最高值的前10个句子存储到我的阵列中?如果是这样我怎么能这样做?