我正在使用python3.2.3中的一个二进制搜索树,下面是我创建二叉搜索树的两个文件(老师给的)。在
但首先我要解释我遇到的问题。首先,我在文件中阅读,并将新单词附加到BST,并将电影标题作为它们的值。然而,我在如何处理else语句上遇到了问题。我想我需要把已经在二叉搜索树中的那个词附加到一个电影标题的另一个值上,比如说“A”,因为它出现的太多了,所以一个键附加到多个值上。在
我不知道该怎么做,这就是else语句在Read函数中的位置。任何帮助都将不胜感激。在
谢谢你
树节点:
class TreeNode:
def __init__(self,key,val,left=None,right=None,
parent=None):
self.key = key
self.payload = val
self.leftChild = left
self.rightChild = right
self.parent = parent
def hasLeftChild(self):
return self.leftChild
def hasRightChild(self):
return self.rightChild
def isLeftChild(self):
return self.parent and \
self.parent.leftChild == self
def isRightChild(self):
return self.parent and \
self.parent.rightChild == self
def isRoot(self):
return not self.parent
def isLeaf(self):
return not (self.rightChild or self.leftChild)
def hasAnyChildren(self):
return self.rightChild or self.leftChild
def hasBothChildren(self):
return self.rightChild and self.leftChild
def replaceNodeData(self,key,value,lc,rc):
self.key = key
self.payload = value
self.leftChild = lc
self.rightChild = rc
if self.hasLeftChild():
self.leftChild.parent = self
if self.hasRightChild():
self.rightChild.parent = self
def __iter__(self):
if self:
if self.hasLeftChild():
for elem in self.leftChild:
yield elem
yield self.key
if self.hasRightChild():
for elem in self.rightChild:
yield elem
def findSuccessor(self):
succ = None
if self.hasRightChild():
succ = self.rightChild.findMin()
else:
if self.parent:
if self.isLeftChild():
succ = self.parent
else:
self.parent.rightChild = None
succ = self.parent.findSuccessor()
self.parent.rightChild = self
return succ
def findMin(self):
current = self
while current.hasLeftChild():
current = current.leftChild
return current
def spliceOut(self):
if self.isLeaf():
if self.isLeftChild():
self.parent.leftChild = None
else:
self.parent.rightChild = None
elif self.hasAnyChildren():
if self.hasLeftChild():
if self.isLeftChild():
self.parent.leftChild = self.leftChild
else:
self.parent.rightChild = self.leftChild
self.leftChild.parent = self.parent
else:
if self.isLeftChild():
self.parent.leftChild = self.rightChild
else:
self.parent.rightChild = self.rightChild
self.rightChild.parent = self.parent
二进制搜索树:
^{pr2}$我需要帮助的代码:
from binary_search_tree import BinarySearchTree
from tree_node import TreeNode
MovieTree = BinarySearchTree()
MovieList = []
def Read(filename):
file = open('movieData.txt')
for line in file:
line = line.strip()
words = line.lower().split(' ')
for word in words:
if word not in MovieTree:
MovieTree.put(word, [line])
else:
pass
#attach word that is already in BST to another value
# which is this case is a movie title.
def Main():
Read('movieData.txt')
#print(MovieTree)
#print (MovieList)
Main()
如果需要的话,这里有一个我正在阅读的小样本:
A Bad Day <2006>
A Baleia Branca - Uma Ideia de Deus <2007>
A Batalha das Flores no Campo Grande <1907>
A Bear Named Winnie <2004>
A Beautiful Daze <2008>
A Beautiful Mind <2001>
A Bell for Adano <1945>
A Better Place <1997>
A Big Hand for Sooty <1998>
A Big Hand for the Little Lady <1966>
A Big Mistake! <2009>
A Bill of Divorcement <1932> <1940>
A Bird in a Bonnet <1958>
A Bird in a Guilty Cage <1952>
A Bird in the Head <1946>
A Bit of Scarlet <1997>
A Black Widow <2009>
A Blind Bargain <1922>
A Blue Collapse <2008>
A Blue Note <2009>
如果我正确地理解了这个要求,我想您应该
MovieTree.get(word).append(line)
get(word)部分返回与该单词相关联的标题列表(即二进制搜索树中匹配单词的有效负载),并且.append(line)将line添加到列表的末尾。
相关问题 更多 >
编程相关推荐