繁体   English   中英

二叉树所有节点的总和

[英]Sum of all nodes of a 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, [ ], [ ]]]))

小心,

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(*write your list here*) 。但是,您需要使__init__()方法允许列表的传递。请参阅下一点。)
  • 您的__init__()方法将BinaryTree对象作为参数,因此不会解析列表。
  • __init__()方法中,将两个子节点都设置为None ,因此没有节点将拥有子节点。
  • 调用sumTree()方法时,需要指定上下文。 它必须是BinaryTree.sumTree(..) 但是,您仍然需要创建将被传递给sumTree方法的Binary树实例。
  • sumTree()方法中,您尝试访问rootObj成员-该成员不存在,因为您将其称为key

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

  • 您应该将sumTree()方法的参数重命名为与类名不同的名称。
  • 在python中,不需要Getter方法。 您可以直接访问成员。 如果仍然希望定义更复杂的获取/设置行为,则应查看python properties
  • 成员node从未使用过。

您不需要所有吸气剂 您可以简单地使用对象访问器方法,例如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, [], []]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM