
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Find the Sum of All Nodes in a Binary Tree
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.
Below is a demonstration of the same −
Example
class Tree_struct: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): self.key = data def add_node(self, node): self.children.append(node) def search_node(self, key): if self.key == key: return self for child in self.children: temp = child.search_node(key) if temp is not None: return temp return None def sum_node(self): my_summation = self.key for child in self.children: my_summation = my_summation + child.sum_node() return my_summation my_instance = None print('Menu (assume no duplicate keys)') print('add <data> at root') print('add <data> below <data>') print('sum') print('quit') while True: my_input = input('What operation would you do ? ').split() operation = my_input[0].strip().lower() if operation == 'add': data = int(my_input[1]) new_node = Tree_struct(data) suboperation = my_input[2].strip().lower() if suboperation == 'at': my_instance = new_node elif suboperation == 'below': position = my_input[3].strip().lower() key = int(position) ref_node = None if my_instance is not None: ref_node = my_instance.search_node(key) if ref_node is None: print('No such key') continue ref_node.add_node(new_node) elif operation == 'sum': if my_instance is None: print('The tree is empty') else: my_summation = my_instance.sum_node() print('Sum of all nodes is: {}'.format(my_summation)) elif operation == 'quit': break
Output
Menu (assume no duplicate keys) add <data> at root add <data> below <data> sum quit What operation would you do ? add 5 at root What operation would you do ? add 7 below 5 What operation would you do ? add 0 below 7 What operation would you do ? sum Sum of all nodes is: 12 What operation would you do ? quit
Explanation
The ‘Tree_struct’ class with required attributes is created.
It has an ‘init’ function that is used to create an empty list.
A ‘set_root’ method is defined that helps set the root value of the binary tree.
It has an ‘add_node’ method that helps add elements to the tree.
A method named ‘search_elem’ is defined, that helps search for a specific element.
A method named ‘sum_node is defined, that helps add elements of the tree and find the sum.
An instance is created and assigned to ‘None’.
The user input is taken for the operation that needs to be performed.
Depending on the user’ choice, the operation is performed.
Relevant output is displayed on the console.
- Related Articles
- Python Program to Find the Sum of all Nodes in a Tree
- Find sum of all nodes of the given perfect binary tree in C++
- Program to find the largest sum of the path between two nodes in a binary tree in Python
- Program to find sum of all numbers formed by path of a binary tree in python
- Program to find leaf and non-leaf nodes of a binary tree in Python
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Program to remove all nodes with only one child from a binary tree in Python?
- Program to find out distance between two nodes in a binary tree in Python
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Program to find sum of all elements of a tree in Python
- Program to find most frequent subtree sum of a binary tree in Python
- Product of all nodes in a Binary Tree in C++
- Program to find out the lowest common ancestor of a binary tree of given nodes using Python
- Print all internal nodes of a Binary tree in C++
- Program to find largest sum of any path of a binary tree in Python
