我正在编写一个简单的应用程序,其中包含代表英语的树。我在 C++ 中做过类似的事情,但这是我第一次尝试在 Python 中构建树。
englishWords = []
englishFile = open("english.txt")
for line in englishFile:
englishWords.append(line.rstrip())
class Node:
def __init__(self, value):
self.Value = value
self.checked = False
self.Pointers = []
self.numPointers = 0
def addNode(self, value):
x = Node(value)
self.Pointers.append(x)
return x
headString = "HEAD"
Head = Node(headString)
def buildPointers(parent, info, nodeList):
x = 0
y = len(nodeList)
while x < y :
if parent.numPointers == 0:
newNode = parent.addNode(info)
parent.numPointers = parent.numPointers + 1
buildPointers(newNode, nodeList[x+1], nodeList)
break
else:
for i in parent.Pointers:
if info == i.Value:
buildPointers(i, nodeList[x+1], nodeList)
continue
else:
newNode = parent.addNode(info)
parent.numPointers = parent.numPointers + 1
buildPointers(newNode, nodeList[x+1], nodeList)
continue
def treeBuild(lyst):
for i in lyst:
iList = list(i)
buildPointers(Head, iList[0], iList)
treeBuild(englishWords)
一旦我运行代码,Windows 就会显示“python.exe 已停止运行”,这可能是我忽略了一些简单的事情,所以请随意撕毁我或我写这个的方式。我会喜欢任何有助于使我成为更好的程序员的批评。