class node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
class btree:
def __init__(self):
self.root=None
def insertt(self,root, data):
if root==None:
root=node(data)
elif root.data > data:
self.insertt(self,root.left, data)
else:
self.insertt(self,root.right, data)
def insert(self,data):
self.insertt(self.root, data)
def printall(self):
self.printtall(self.root)
def printtall(self,root):
if root==None:
print "reached end "
else:
printtall(root.left)
print root.data
printtall(root.right)
a=btree()
a.insert(2)
a.insert(1)
a.insert(6)
a.insert(3)
a.printall()
因此self.root始終爲無。我是一名C++程序員,我發現很難處理Python,因爲在這裏找不到引用調用。我應該怎麼做才能使它工作?謝謝你的幫助。功能未被引用調用導致self.root成爲無
如果root沒有返回根(插入時)。返回終止功能。 – syntonym
@syntonym是的,但返回也將它存儲在一些變量的權利?你不能添加任何會影響真實的變量。如果你是對的,請發佈你的代碼版本。 –
@TomKarzes你可以發佈你的版本相同的代碼?請編輯它 –