Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 二叉树的所有节点之和_Python_Python 3.x_List_Sum_Binary Tree - Fatal编程技术网

Python 二叉树的所有节点之和

Python 二叉树的所有节点之和,python,python-3.x,list,sum,binary-tree,Python,Python 3.x,List,Sum,Binary Tree,我正试图编写一个程序来计算由列表列表表示的二叉树(不是二叉搜索树)中所有节点(包括根)的总和。我从概念上理解,递归地处理这个问题是最好的方法,但就是无法理解代码。到目前为止,我的代码是: class BinaryTree: def __init__(self,rootObj, leftChild = None, rightChild = None): self.key = rootObj self.leftChild = None self

我正试图编写一个程序来计算由列表列表表示的二叉树(不是二叉搜索树)中所有节点(包括根)的总和。我从概念上理解,递归地处理这个问题是最好的方法,但就是无法理解代码。到目前为止,我的代码是:

class BinaryTree:
    def __init__(self,rootObj, leftChild = None, rightChild = None):
        self.key = rootObj
        self.leftChild = None
        self.rightChild = None
        self.node=[rootObj, leftChild, rightChild]
    def getrightChild(self):
        return self.rightChild
    def getleftChild(self):
        return self.leftChild
    def setRootObj(self,obj):
        self.key = obj
    def getRootObj(self):
        return self.key
    def sumTree(BinaryTree):
        if BinaryTree is None: return 0
        return sumTree(BinaryTree.leftChild) \ 
             + sumTree(BinaryTree.rightChild)\
             + BinaryTree.rootObj

print(sumTree([8,[],[]]))
print(sumTree([9, [6, [ ], [ ]], [65, [ ], [ ]]]))
小心,

self.key = rootObj
self.leftChild = None
self.rightChild = None
是对象属性,因此不能通过类直接访问它们。您必须创建一个实例,如

obj = BinaryTree(...)
然后调用该方法

obj.sumTree(...)
对于求和算法,计算求和的最简单方法如下:

class BinaryTree:       

    @classmethod
    def calc_sum(cls, list_tree):
        print(list_tree)
        if list_tree:
            left_node_value = BinaryTree.calc_sum(list_tree[1])
            right_node_value = BinaryTree.calc_sum(list_tree[2])
            return list_tree[0] + left_node_value + right_node_value
        return 0        

value = BinaryTree.calc_sum([9, [6, [ ], [ ]], [65, [ ], [ ]]])
print(value)
小心,

self.key = rootObj
self.leftChild = None
self.rightChild = None
是对象属性,因此不能通过类直接访问它们。您必须创建一个实例,如

obj = BinaryTree(...)
然后调用该方法

obj.sumTree(...)
对于求和算法,计算求和的最简单方法如下:

class BinaryTree:       

    @classmethod
    def calc_sum(cls, list_tree):
        print(list_tree)
        if list_tree:
            left_node_value = BinaryTree.calc_sum(list_tree[1])
            right_node_value = BinaryTree.calc_sum(list_tree[2])
            return list_tree[0] + left_node_value + right_node_value
        return 0        

value = BinaryTree.calc_sum([9, [6, [ ], [ ]], [65, [ ], [ ]]])
print(value)

从我从这段代码中读到的,你的递归算法是正确的。 然而,它有许多语法错误以及其他语义错误,使得它无法正确运行

以下是我看到的:

  • 您创建了一个
    BinaryTree
    类,但从未创建过它的实例。
    sumTree([…])
    尝试计算列表的总和,但这不起作用,因为您希望它对
    BinaryTree
    对象进行计算。您需要首先解析该列表并创建
    BinaryTree
    的实例。(比如
    tree=BinaryTree(*在这里写下你的列表*)
    也许吧。但是你需要让你的
    \uuu init\uuuu()方法允许传递列表,当然。请看下一点。)
  • 您的
    \uuu init\uu()
    方法将
    BinaryTree
    对象作为参数,因此不需要解析列表
  • \uuuu init\uuuuu()方法中,您将两个子节点都设置为
    None
    ,因此no节点将永远具有子节点
  • 调用
    sumTree()
    方法时,需要指定上下文。 它必须是
    BinaryTree.sumTree(..)
    。不过,您仍然需要创建二叉树实例,该实例将传递给
    sumTree
    方法
  • sumTree()
除了错误之外,如果您愿意,我还想指出一些“代码气味”

  • 您应该将
    sumTree()
    方法的参数重命名为与类名不同的名称
  • 在python中,不需要Getter方法。您可以直接访问成员。如果您仍然希望定义更复杂的get/set行为,那么应该看看python属性
  • 从未使用成员
    节点

根据我从这段代码中读到的信息,您的递归算法是正确的。 然而,它有许多语法错误以及其他语义错误,使得它无法正确运行

以下是我看到的:

  • 您创建了一个
    BinaryTree
    类,但从未创建过它的实例。
    sumTree([…])
    尝试计算列表的总和,但这不起作用,因为您希望它对
    BinaryTree
    对象进行计算。您需要首先解析该列表并创建
    BinaryTree
    的实例。(比如
    tree=BinaryTree(*在这里写下你的列表*)
    也许吧。但是你需要让你的
    \uuu init\uuuu()方法允许传递列表,当然。请看下一点。)
  • 您的
    \uuu init\uu()
    方法将
    BinaryTree
    对象作为参数,因此不需要解析列表
  • \uuuu init\uuuuu()方法中,您将两个子节点都设置为
    None
    ,因此no节点将永远具有子节点
  • 调用
    sumTree()
    方法时,需要指定上下文。 它必须是
    BinaryTree.sumTree(..)
    。不过,您仍然需要创建二叉树实例,该实例将传递给
    sumTree
    方法
  • sumTree()
除了错误之外,如果您愿意,我还想指出一些“代码气味”

  • 您应该将
    sumTree()
    方法的参数重命名为与类名不同的名称
  • 在python中,不需要Getter方法。您可以直接访问成员。如果您仍然希望定义更复杂的get/set行为,那么应该看看python属性
  • 从未使用成员
    节点
    • 您不需要所有的getter。您可以简单地使用对象访问器方法,例如
      tree\u a.left\u child
      。其次,您没有用您的孩子创建二进制树,这意味着对他们运行sum_树是没有意义的。通读下面的代码,确保您理解发生了什么

      很确定你真正想要的是这个

      class BinaryTree:
          def __init__(self, root, left_child=None, right_child=None):
              self.root = root
              self.left_child = None if not left_child else BinaryTree(*left_child)
              self.right_child = None if not right_child else BinaryTree(*right_child)
              self.node = [root, left_child, right_child]
      
          def set_root(self, root):
              self.root = root
      
          def sum_tree(self):
              tree_sum = 0
              if self.left_child:
                  tree_sum += self.left_child.sum_tree()
              if self.right_child:
                  tree_sum += self.right_child.sum_tree()
      
              return tree_sum + self.root
      
      tree_a = BinaryTree(8)
      tree_b = BinaryTree(9, [6, [], []], [65, [], []])
      print(tree_a.sum_tree())
      # 8
      print(tree_b.sum_tree())
      # 80
      print(tree_b.left_child.node)
      # [6, [], []]
      
      你不需要所有的getter。您可以简单地使用对象访问器方法,例如
      tree\u a.left\u child
      。其次,您没有用您的孩子创建二进制树,这意味着对他们运行sum_树是没有意义的。通读下面的代码,确保您理解发生了什么

      很确定你真正想要的是这个

      class BinaryTree:
          def __init__(self, root, left_child=None, right_child=None):
              self.root = root
              self.left_child = None if not left_child else BinaryTree(*left_child)
              self.right_child = None if not right_child else BinaryTree(*right_child)
              self.node = [root, left_child, right_child]
      
          def set_root(self, root):
              self.root = root
      
          def sum_tree(self):
              tree_sum = 0
              if self.left_child:
                  tree_sum += self.left_child.sum_tree()
              if self.right_child:
                  tree_sum += self.right_child.sum_tree()
      
              return tree_sum + self.root
      
      tree_a = BinaryTree(8)
      tree_b = BinaryTree(9, [6, [], []], [65, [], []])
      print(tree_a.sum_tree())
      # 8
      print(tree_b.sum_tree())
      # 80
      print(tree_b.left_child.node)
      # [6, [], []]
      

      那么调试器是怎么说的呢?您应该决定是用类表示树还是树节点。如果在某些变量中保留对根节点的引用,则只能使用节点构建树。节点可以保留对child的引用,那么为什么要使用列表来存储树呢?您还必须决定sum方法应该在树或节点类中,还是应该是在其根节点上调用的自由方法