Sum and product of internal nodes in binary tree

Here given code implementation process.
/*
C Program
Sum and product of internal nodes in binary tree
*/
#include <stdio.h>
#include <stdlib.h>
// Tree Node
struct TreeNode
{
int data;
struct TreeNode *left;
struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
struct TreeNode *root;
};
struct Result
{
int sum;
int product;
};
// Create new tree
struct BinaryTree *newTree()
{
// Create dynamic node
struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
if (tree != NULL)
{
tree->root = NULL;
}
else
{
printf("Memory Overflow to Create tree Tree\n");
}
//return new tree
return tree;
}
// returns a new node of tree
struct TreeNode *newNode(int data)
{
// Create dynamic node
struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
if (node != NULL)
{
//Set data and pointer values
node->data = data;
node->left = NULL;
node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return node;
}
// Function which is calculating the sum and product of internal nodes
void getResult(struct TreeNode *node, struct Result *output)
{
if (node != NULL)
{
if (node->left != NULL || node->right != NULL)
{
// When node is internal node
output->sum += node->data;
output->product *= node->data;
}
// Recursively visiting left and right subtree
getResult(node->left, output);
getResult(node->right, output);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
void sumAndProduct(struct TreeNode *root)
{
if (root == NULL)
{
printf("\n Empty Tree \n");
return;
}
// Create dynamic result node
// Otherwise use two variable which is pass by reference
struct Result *output = (struct Result *) malloc(sizeof(struct Result));
output->sum = 0;
output->product = 1;
getResult(root, output);
// Sum and product of internal nodes
printf(" Sum : %d", output->sum);
printf("\n Product : %d\n", output->product);
}
int main(int argc, char
const *argv[])
{
struct BinaryTree *tree = newTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree->root = newNode(1);
tree->root->left = newNode(2);
tree->root->right = newNode(8);
tree->root->left->left = newNode(3);
tree->root->left->right = newNode(10);
tree->root->left->right->left = newNode(7);
tree->root->right->left = newNode(6);
tree->root->right->left->right = newNode(9);
tree->root->right->right = newNode(4);
tree->root->right->left->left = newNode(5);
tree->root->right->right->right = newNode(11);
sumAndProduct(tree->root);
return 0;
}
Output
Sum : 31
Product : 3840
/*
Java Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public int sum;
public int product;
public BinaryTree()
{
this.root = null;
this.sum = 0;
this.product = 0;
}
// Function which is calculating the sum and product of internal nodes
public void getResult(TreeNode node)
{
if (node != null)
{
if (node.left != null || node.right != null)
{
// When node is internal node
this.sum += node.data;
this.product *= node.data;
}
// Recursively visiting left and right subtree
getResult(node.left);
getResult(node.right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
public void sumAndProduct()
{
if (this.root == null)
{
System.out.print("\n Empty Tree \n");
return;
}
this.sum = 0;
this.product = 1;
getResult(root);
// Sum and product of internal nodes
System.out.print(" Sum : " + this.sum);
System.out.print("\n Product : " + this.product + "\n");
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct();
}
}
Output
Sum : 31
Product : 3840
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
public:
int data;
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
// Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
// Define Binary Tree
class BinaryTree
{
public:
TreeNode *root;
int sum;
int product;
BinaryTree()
{
this->root = NULL;
this->sum = 0;
this->product = 0;
}
// Function which is calculating the sum and product of internal nodes
void getResult(TreeNode *node)
{
if (node != NULL)
{
if (node->left != NULL || node->right != NULL)
{
// When node is internal node
this->sum += node->data;
this->product *= node->data;
}
// Recursively visiting left and right subtree
this->getResult(node->left);
this->getResult(node->right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
void sumAndProduct()
{
if (this->root == NULL)
{
cout << "\n Empty Tree \n";
return;
}
this->sum = 0;
this->product = 1;
this->getResult(this->root);
// Sum and product of internal nodes
cout << " Sum : " << this->sum;
cout << "\n Product : " << this->product << "\n";
}
};
int main()
{
BinaryTree tree = BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
tree.root->left = new TreeNode(2);
tree.root->right = new TreeNode(8);
tree.root->left->left = new TreeNode(3);
tree.root->left->right = new TreeNode(10);
tree.root->left->right->left = new TreeNode(7);
tree.root->right->left = new TreeNode(6);
tree.root->right->left->right = new TreeNode(9);
tree.root->right->right = new TreeNode(4);
tree.root->right->left->left = new TreeNode(5);
tree.root->right->right->right = new TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct();
return 0;
}
Output
Sum : 31
Product : 3840
// Include namespace system
using System;
/*
C# Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
public class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public int sum;
public int product;
public BinaryTree()
{
this.root = null;
this.sum = 0;
this.product = 0;
}
// Function which is calculating the sum and product of internal nodes
public void getResult(TreeNode node)
{
if (node != null)
{
if (node.left != null || node.right != null)
{
// When node is internal node
this.sum += node.data;
this.product *= node.data;
}
// Recursively visiting left and right subtree
getResult(node.left);
getResult(node.right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
public void sumAndProduct()
{
if (this.root == null)
{
Console.Write("\n Empty Tree \n");
return;
}
this.sum = 0;
this.product = 1;
getResult(root);
// Sum and product of internal nodes
Console.Write(" Sum : " + this.sum);
Console.Write("\n Product : " + this.product + "\n");
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct();
}
}
Output
Sum : 31
Product : 3840
<?php
/*
Php Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
public $data;
public $left;
public $right;
function __construct($data)
{
// Set node value
$this->data = $data;
$this->left = null;
$this->right = null;
}
}
// Define Binary Tree
class BinaryTree
{
public $root;
public $sum;
public $product;
function __construct()
{
$this->root = null;
$this->sum = 0;
$this->product = 0;
}
// Function which is calculating the sum and product of internal nodes
public function getResult($node)
{
if ($node != null)
{
if ($node->left != null || $node->right != null)
{
// When node is internal node
$this->sum += $node->data;
$this->product *= $node->data;
}
// Recursively visiting left and right subtree
$this->getResult($node->left);
$this->getResult($node->right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
public function sumAndProduct()
{
if ($this->root == null)
{
echo "\n Empty Tree \n";
return;
}
$this->sum = 0;
$this->product = 1;
$this->getResult($this->root);
// Sum and product of internal nodes
echo " Sum : ". $this->sum;
echo "\n Product : ". $this->product ."\n";
}
}
function main()
{
$tree = new BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
$tree->root = new TreeNode(1);
$tree->root->left = new TreeNode(2);
$tree->root->right = new TreeNode(8);
$tree->root->left->left = new TreeNode(3);
$tree->root->left->right = new TreeNode(10);
$tree->root->left->right->left = new TreeNode(7);
$tree->root->right->left = new TreeNode(6);
$tree->root->right->left->right = new TreeNode(9);
$tree->root->right->right = new TreeNode(4);
$tree->root->right->left->left = new TreeNode(5);
$tree->root->right->right->right = new TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
$tree->sumAndProduct();
}
main();
Output
Sum : 31
Product : 3840
/*
Node Js Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
class BinaryTree
{
constructor()
{
this.root = null;
this.sum = 0;
this.product = 0;
}
// Function which is calculating the sum and product of internal nodes
getResult(node)
{
if (node != null)
{
if (node.left != null || node.right != null)
{
// When node is internal node
this.sum += node.data;
this.product *= node.data;
}
// Recursively visiting left and right subtree
this.getResult(node.left);
this.getResult(node.right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
sumAndProduct()
{
if (this.root == null)
{
process.stdout.write("\n Empty Tree \n");
return;
}
this.sum = 0;
this.product = 1;
this.getResult(this.root);
// Sum and product of internal nodes
process.stdout.write(" Sum : " + this.sum);
process.stdout.write("\n Product : " + this.product + "\n");
}
}
function main()
{
var tree = new BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct();
}
main();
Output
Sum : 31
Product : 3840
# Python 3 Program
# Sum and product of internal nodes in binary tree
# Binary Tree node
class TreeNode :
def __init__(self, data) :
# Set node value
self.data = data
self.left = None
self.right = None
# Define Binary Tree
class BinaryTree :
def __init__(self) :
self.root = None
self.sum = 0
self.product = 0
# Function which is calculating the sum and product of internal nodes
def getResult(self, node) :
if (node != None) :
if (node.left != None or node.right != None) :
# When node is internal node
self.sum += node.data
self.product *= node.data
# Recursively visiting left and right subtree
self.getResult(node.left)
self.getResult(node.right)
# Handles the request of finding sum and product of all internal nodes in binary tree
def sumAndProduct(self) :
if (self.root == None) :
print("\n Empty Tree ")
return
self.sum = 0
self.product = 1
self.getResult(self.root)
# Sum and product of internal nodes
print(" Sum : ", self.sum, end = "")
print("\n Product : ", self.product )
def main() :
tree = BinaryTree()
#
# 1
# / \
# / \
# 2 8
# / \ / \
# 3 10 6 4
# / / \ \
# 7 5 9 11
#
# -----------------------
# Binary Tree
# -----------------------
tree.root = TreeNode(1)
tree.root.left = TreeNode(2)
tree.root.right = TreeNode(8)
tree.root.left.left = TreeNode(3)
tree.root.left.right = TreeNode(10)
tree.root.left.right.left = TreeNode(7)
tree.root.right.left = TreeNode(6)
tree.root.right.left.right = TreeNode(9)
tree.root.right.right = TreeNode(4)
tree.root.right.left.left = TreeNode(5)
tree.root.right.right.right = TreeNode(11)
# Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct()
if __name__ == "__main__": main()
Output
Sum : 31
Product : 3840
# Ruby Program
# Sum and product of internal nodes in binary tree
# Binary Tree node
class TreeNode
# Define the accessor and reader of class TreeNode
attr_reader :data, :left, :right
attr_accessor :data, :left, :right
def initialize(data)
# Set node value
self.data = data
self.left = nil
self.right = nil
end
end
# Define Binary Tree
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_reader :root, :sum, :product
attr_accessor :root, :sum, :product
def initialize()
self.root = nil
self.sum = 0
self.product = 0
end
# Function which is calculating the sum and product of internal nodes
def getResult(node)
if (node != nil)
if (node.left != nil || node.right != nil)
# When node is internal node
self.sum += node.data
self.product *= node.data
end
# Recursively visiting left and right subtree
self.getResult(node.left)
self.getResult(node.right)
end
end
# Handles the request of finding sum and product of all internal nodes in binary tree
def sumAndProduct()
if (self.root == nil)
print("\n Empty Tree \n")
return
end
self.sum = 0
self.product = 1
self.getResult(root)
# Sum and product of internal nodes
print(" Sum : ", self.sum)
print("\n Product : ", self.product ,"\n")
end
end
def main()
tree = BinaryTree.new()
#
# 1
# / \
# / \
# 2 8
# / \ / \
# 3 10 6 4
# / / \ \
# 7 5 9 11
#
# -----------------------
# Binary Tree
# -----------------------
tree.root = TreeNode.new(1)
tree.root.left = TreeNode.new(2)
tree.root.right = TreeNode.new(8)
tree.root.left.left = TreeNode.new(3)
tree.root.left.right = TreeNode.new(10)
tree.root.left.right.left = TreeNode.new(7)
tree.root.right.left = TreeNode.new(6)
tree.root.right.left.right = TreeNode.new(9)
tree.root.right.right = TreeNode.new(4)
tree.root.right.left.left = TreeNode.new(5)
tree.root.right.right.right = TreeNode.new(11)
# Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct()
end
main()
Output
Sum : 31
Product : 3840
/*
Scala Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
def this(data: Int)
{
this(data, null, null);
}
}
// Define Binary Tree
class BinaryTree(var root: TreeNode , var sum: Int , var product: Int)
{
def this()
{
this(null, 0, 0);
}
// Function which is calculating the sum and product of internal nodes
def getResult(node: TreeNode): Unit = {
if (node != null)
{
if (node.left != null || node.right != null)
{
// When node is internal node
this.sum += node.data;
this.product *= node.data;
}
// Recursively visiting left and right subtree
this.getResult(node.left);
this.getResult(node.right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
def sumAndProduct(): Unit = {
if (this.root == null)
{
print("\n Empty Tree \n");
return;
}
this.sum = 0;
this.product = 1;
this.getResult(root);
// Sum and product of internal nodes
print(" Sum : " + this.sum);
print("\n Product : " + this.product + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct();
}
}
Output
Sum : 31
Product : 3840
/*
Swift 4 Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode? ;
var right: TreeNode? ;
init(_ data: Int)
{
// Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode? ;
var sum: Int;
var product: Int;
init()
{
self.root = nil;
self.sum = 0;
self.product = 0;
}
// Function which is calculating the sum and product of internal nodes
func getResult(_ node: TreeNode? )
{
if (node != nil)
{
if (node!.left != nil || node!.right != nil)
{
// When node is internal node
self.sum += node!.data;
self.product *= node!.data;
}
// Recursively visiting left and right subtree
self.getResult(node!.left);
self.getResult(node!.right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
func sumAndProduct()
{
if (self.root == nil)
{
print("\n Empty Tree ");
return;
}
self.sum = 0;
self.product = 1;
self.getResult(self.root);
// Sum and product of internal nodes
print(" Sum : ", self.sum, terminator: "");
print("\n Product : ", self.product );
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree.root = TreeNode(1);
tree.root!.left = TreeNode(2);
tree.root!.right = TreeNode(8);
tree.root!.left!.left = TreeNode(3);
tree.root!.left!.right = TreeNode(10);
tree.root!.left!.right!.left = TreeNode(7);
tree.root!.right!.left = TreeNode(6);
tree.root!.right!.left!.right = TreeNode(9);
tree.root!.right!.right = TreeNode(4);
tree.root!.right!.left!.left = TreeNode(5);
tree.root!.right!.right!.right = TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct();
}
main();
Output
Sum : 31
Product : 3840
/*
Kotlin Program
Sum and product of internal nodes in binary tree
*/
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode ? ;
var sum: Int;
var product: Int;
constructor()
{
this.root = null;
this.sum = 0;
this.product = 0;
}
// Function which is calculating the sum and product of internal nodes
fun getResult(node: TreeNode ? ): Unit
{
if (node != null)
{
if (node.left != null || node.right != null)
{
// When node is internal node
this.sum += node.data;
this.product *= node.data;
}
// Recursively visiting left and right subtree
this.getResult(node.left);
this.getResult(node.right);
}
}
// Handles the request of finding sum and product of all internal nodes in binary tree
fun sumAndProduct(): Unit
{
if (this.root == null)
{
print("\n Empty Tree \n");
return;
}
this.sum = 0;
this.product = 1;
this.getResult(root);
// Sum and product of internal nodes
print(" Sum : " + this.sum);
print("\n Product : " + this.product + "\n");
}
}
fun main(args: Array < String > ): Unit
{
var tree: BinaryTree = BinaryTree();
/*
1
/ \
/ \
2 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 11
-----------------------
Binary Tree
-----------------------
*/
tree.root = TreeNode(1);
tree.root?.left = TreeNode(2);
tree.root?.right = TreeNode(8);
tree.root?.left?.left = TreeNode(3);
tree.root?.left?.right = TreeNode(10);
tree.root?.left?.right?.left = TreeNode(7);
tree.root?.right?.left = TreeNode(6);
tree.root?.right?.left?.right = TreeNode(9);
tree.root?.right?.right = TreeNode(4);
tree.root?.right?.left?.left = TreeNode(5);
tree.root?.right?.right?.right = TreeNode(11);
// Internal nodes (1,2,8,10,6,4)
tree.sumAndProduct();
}
Output
Sum : 31
Product : 3840
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment