二叉树的所有节点的总和

时间:2018-05-16 12:50:53

标签: 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.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, [ ], [ ]]]))

3 个答案:

答案 0 :(得分:2)

小心,

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)

答案 1 :(得分:2)

好吧,根据我从这段代码中读到的内容,您的递归算法是正确的。 但是,很多语法错误以及其他语义错误导致无法正确运行。

以下是我看到的内容:

  • 您创建了一个BinaryTree类,但您从未创建过它的实例。 sumTree([...])尝试计算列表的总和,这将无效,因为您希望它为BinaryTree对象执行此操作。您需要解析该列表并首先创建BinaryTree的实例。 (比如tree = BinaryTree(*write your list here*)也许。但是你需要让你的__init__()方法允许传递列表。请参阅下一点。)
  • 您的__init__()方法将BinaryTree个对象作为参数,因此无法解析您的列表。
  • __init__()方法中,您将两个孩子都设置为None,因此没有节点将拥有子节点。
  • 调用sumTree()方法时,需要指定上下文。 它必须是BinaryTree.sumTree(..)。但是,您仍然需要创建应传递给sumTree方法的二叉树实例。
  • sumTree()方法中,您尝试访问不存在的rootObj成员,因为您将其称为key

除了错误之外,如果你愿意,我想指出一些“代码味道”。

  • 您应该将sumTree()方法的参数重命名为与类名不同的内容。
  • 在python中,不需要Getter方法。您可以直接访问成员。如果您仍希望定义更复杂的get / set行为,您应该查看python 属性
  • 永远不会使用成员node

答案 2 :(得分:2)

您不需要所有 getters 。您可以简单地使用对象访问器方法,例如tree_a.left_child。其次,你没有从你的孩子那里创建一个BinaryTree,这意味着对他们运行 sum_tree 是没有意义的。阅读以下代码,确保您了解正在发生的事情。

非常确定实际想要的是这个

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, [], []]
相关问题