二叉树大小函数python

时间:2018-02-06 05:31:47

标签: python tree binary-tree

我是Python和数据结构的初学者,我在设计二叉树大小方法时遇到问题,我的代码和错误如下

class BinaryTree:
    def get_left(self):
         return self.left
    def get_right(self):
         return self.right
    def set_data(self, data):
         self.data = data
    def get_data(self):
         return self.data

    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
    def insert_left(self, new_data):
        if self.left == None:
            self.left = BinaryTree(new_data)
        else:
            t = BinaryTree(new_data)
            t.left = self.left
            self.left = t
    def insert_right(self, new_data):
        if self.right == None:
            self.right = BinaryTree(new_data)
        else:
            t = BinaryTree(new_data)
            t.right = self.right
            self.right = t
    def tree_size(self):
       if self== None:
           return 0
       else:
           if self.left != None and self.right!= None:
               return 1+ self.left.tree_size()+ self.right.tree_size()
           elif self.left!=None:
               return 1+ self.left.tree_size()
           elif self.right!=None:
               return 1+ self.right.tree_size()
           else:
               return 1

获得以下错误,

  

AttributeError:' BinaryTree'对象没有属性' tree_size'

任何帮助?

0 个答案:

没有答案