Python を使用して BST を実装しましたが、要素をツリーに追加するときにいくつかのエラーが発生します。
class Node:
def __init__(self,word,meaning):
self.left=None
self.right=None
self.word=word
self.meaning=meaning
class BST:
def __init__(self,word,meaning):
self.root=Node(word,meaning)
def add_word(self,word,meaning):
if(self.root.word==None):
self.root.word=word
self.root.meaning=meaning
return "Create root"
else:
current=self.root
while(1):
if(word<current.word):
if(current.left):
self.add_word(word,meaning)
else:
current.left=Node(word,meaning)
break
elif(word>current.word):
if(current.right):
self.add_word(word,meaning)
else:
current.right=Node(word,meaning)
break
else:
break
def in_order(self,node):
if(node!=None):
self.in_order(node.root.left)
print(node.root.word,node.root.meaning)
self.in_order(node.root.right)