Sum Of All Nodes Of A Binary Tree
Solution 1:
Be careful,
self.key = rootObj
self.leftChild = None
self.rightChild = None
are object attributes, so you can't access them with through your class directly. You have to create an instance like
obj = BinaryTree(...)
and then call the method
obj.sumTree(...)
To your sum algorithm, the easiest way to calculate the sum your way would be something like this:
classBinaryTree:
@classmethoddefcalc_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
return0
value = BinaryTree.calc_sum([9, [6, [ ], [ ]], [65, [ ], [ ]]])
print(value)
Solution 2:
You don't need all the getters. You can simply use object accessor methods, e.g. tree_a.left_child
. Secondly, you didn't create a BinaryTree out of your children, meaning that it doesn't make sense to run sum_tree on them. Read through the following code, and make sure that you understand what's going on.
Pretty sure that what you actually want, is this
classBinaryTree:
def__init__(self, root, left_child=None, right_child=None):
self.root = root
self.left_child = Noneifnot left_child else BinaryTree(*left_child)
self.right_child = Noneifnot right_child else BinaryTree(*right_child)
self.node = [root, left_child, right_child]
defset_root(self, root):
self.root = root
defsum_tree(self):
tree_sum = 0if 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())
# 8print(tree_b.sum_tree())
# 80print(tree_b.left_child.node)
# [6, [], []]
Solution 3:
Well, from what I read from this code, your recursive algorithm is correct. However, there are many syntax mistakes as well as other, semantic mistakes in it that make it impossible to run correctly.
Here is what I see:
- You created a
BinaryTree
class, but you never created an instance of it.sumTree([...])
tries to calculate that sum of a list, which will not work, because you want it to do it for aBinaryTree
object. You need to parse that list and create an instance ofBinaryTree
first. (Liketree = BinaryTree(*write your list here*)
maybe. But you need to make your__init__()
method allow that passing of the list, of course. See next point.) - Your
__init__()
method takesBinaryTree
objects as parameters, so there is no parsing of your lists. - Within the
__init__()
method, you set both children toNone
, so no node will ever have child nodes. - When calling the
sumTree()
method, you need to specify the context. It needs to beBinaryTree.sumTree(..)
. You still need to create the Binary tree instance that shall be passed to thesumTree
method, though. - Within the
sumTree()
method, you try to access therootObj
member - which does not exist, because you called itkey
.
Besides the errors, I'd like to point out some "code smells", if you like.
- You should rename the parameter of the
sumTree()
method to something different ot the class name. - In python, there is no need for Getter-methods. You can access the members directly. If you still wish to define more complex get/set behaviour, you should have a look at python properties.
- The member
node
is never used.
Post a Comment for "Sum Of All Nodes Of A Binary Tree"