现在被这个问题困扰了很长时间,是时候寻求帮助了。几乎一切正常。它读取文件并输入数据等等..程序没有给我任何输出,最后一个if语句有问题..最后三行是
root1.put(word1)
if root.exists(word1):
print(word1, end = " ")
它将值放入树中。然后它将检查第一棵树,如果值“word1”已经在该树中,如果这是真的,它将打印出这个词,如果没有继续。这个程序只是给我一个空输出。有人可以看到问题吗?另一个类文件看起来几乎相同,只是所有变量/参数都没有 1。
类文件 (root1)
class BintreeEN:
def __init__(self, data1):
self.left1 = None
self.right1 = None
self.data1 = data1
def put(self, data1):
if data1 < self.data1:
if self.left1 is None:
self.left1 = BintreeEN(data1)
else:
self.left1.put(data1)
else:
if self.right1 is None:
self.right1 = BintreeEN(data1)
else:
self.right1.put(data1)
def write(self):
if self.left1:
self.left1.write()
print(self.data1)
if self.right1:
self.right1.write()
def exists(self, data1):
if data1 < self.data1:
if self.left1 is None:
return None, None
return self.left1.exists(data1)
elif data1 > self.data1:
if self.right1 is None:
return None, None
return self.right1.exists(data1)
else:
return self.data1
程序文件
#first tree
root = Bintree("root")
with open("word3.txt", "r", encoding = "utf-8") as file:
for row in file:
word = row.strip()
checklist = root.exists(word)
if checklist == word:
pass
else:
root.put(word)
#second tree
root1 = BintreeEN("root1")
with open('engelska.txt','r', encoding = "utf-8") as f:
for row in f:
onerow = row.split()
for rowz in onerow:
word1 = rowz.strip()
#HERE IT something thats wrong...
if root1.exists(word1):
pass
else:
root1.put(word1)
if root.exists(word1): #Check if value is in the first tree
print(word1, end = " ")