2016-06-21 38 views
0
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成爲無

+1

如果root沒有返回根(插入時)。返回終止功能。 – syntonym

+0

@syntonym是的,但返回也將它存儲在一些變量的權利?你不能添加任何會影響真實的變量。如果你是對的,請發佈你的代碼版本。 –

+0

@TomKarzes你可以發佈你的版本相同的代碼?請編輯它 –

回答

1

這裏是工作的代碼,我將解釋評論中出現問題。

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: 
      #earlier you tried to return here which leads to dead end. 
      self.root = node(data) 
     else:  
      #You need the assign the data here, as if you'll 
      #try to update in the above if statement you are doing 
      #nothing but just updating the root again and again 
      if root.data > data: 
       if root.left == None: 
        root.left = node(data) 
       else:  
        self.insertt(root.left, data) 
      else: 
       if root.right == None: 
        root.right = node(data) 
       else:  
        self.insertt(root.right, data) 
    def insert(self,data): 
     self.insertt(self.root, data) 
    def printtall(self,root): 
     #inorder traversal 
     if root != None: 
      self.printtall(root.left) 
      print (root.data) 
      self.printtall(root.right) 
    def printall(self): 
     self.printtall(self.root) 


a=btree() 
a.insertt(a.root, 2) 
a.insertt(a.root, 1) 
a.insertt(a.root, 6) 
a.insertt(a.root, 3) 
a.printall() 

輸出 -

1 
2 
3 
6 
+1

因此,當您調用函數self.insertt(self.root,data)時,self.root的一個副本會轉到那裏。不是嗎? Python不支持按引用調用。它是如何工作的呢? –

+0

那麼,第一個if語句只運行一次,那是明確的嗎?我編輯了代碼,'seld.root.left'是一個錯誤的調用。它所做的總是關於根**而言左**。 Python是通過對象引用調用的,你會從這個源得到它的工作原理,它解釋得非常好... – hashcode55

+0

http://robertheaton.com/2014/02/09/pythons-pass-by-object-reference -as-解釋逐菲利普-K-迪克/ – hashcode55

0

在賦值給函數「insertt」中的根之前,你有一個return語句

+0

嘗試過但無法使其正常工作。你將如何使用相同的return語句。你可以發佈相同的上述代碼的編輯版本嗎? –

+0

立即檢查。它不起作用 –

1

這是我的解決方案。請注意,我將類名更改爲混合大小寫以遵循標準約定,並且更容易避免名稱衝突。

class Node(object): 
    def __init__(self, data): 
     self.data = data 
     self.left = None 
     self.right = None 

class BTree(object): 
    def __init__(self): 
     self.root = None 

    def insert(self, data): 
     self.root = self.insertt(self.root, Node(data)) 

    def insertt(self, root, node): 
     if root == None: 
      root = node 
     elif node.data < root.data: 
      root.left = self.insertt(root.left, node) 
     else: 
      root.right = self.insertt(root.right, node) 

     return root 

    def printall(self): 
     self.printtall(self.root, 0) 

    def printtall(self, root, indent): 
     if root == None: 
      print ". " * indent + "(empty)" 
     else: 
      print ". " * indent + str(root.data) 
      self.printtall(root.left, indent + 1) 
      self.printtall(root.right, indent + 1) 

a = BTree() 
a.insert(2) 
a.insert(1) 
a.insert(6) 
a.insert(3) 
a.printall() 

這是輸出。我更改爲使用前綴,而不是中綴順序打印功能,因爲我發現它是清晰的,但你可以很容易地改回來,如果你喜歡:

2 
. 1 
. . (empty) 
. . (empty) 
. 6 
. . 3 
. . . (empty) 
. . . (empty) 
. . (empty) 
相關問題