Sum of heights of all every nodes in a binary tree
Here given code implementation process.
/*
C Program
Sum of heights of all every nodes in a binary tree
*/
#include <stdio.h>
#include <stdlib.h>
//Binary Tree node
struct Node
{
int data;
struct Node *left, *right;
};
//This is creating a binary tree node and return new node
struct Node *get_node(int data)
{
// Create dynamic node
struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
if (new_node != NULL)
{
//Set data and pointer values
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return new_node;
}
//Calculates the sum of the height of each node in the binary tree
int height_sum(struct Node*node,int *height)
{
if(node == NULL)
{
return 0;
}
// Find the height of left and right subtree
int a = height_sum(node->left,height);
int b = height_sum(node->right,height);
if(a > b)
{
// add new height
*height += a+1;
return a+1;
}
else
{
// add new height
*height += b+1;
return b+1;
}
}
//Display inorder elements
void print_inorder(struct Node *node)
{
if (node != NULL)
{
print_inorder(node->left);
//Print node value
printf(" %d", node->data);
print_inorder(node->right);
}
}
//Handles the request to find height of each node in given binary tree
void height_of_nodes(struct Node*root)
{
if(root==NULL)
{
printf("\n Empty Tree");
}
else
{
int height = 0;
height_sum(root,&height);
printf("\n Tree Node : ");
print_inorder(root);
//Display the calculated result
printf("\n Total height : %d \n",height);
}
}
int main()
{
struct Node*root = NULL;
/* Binary Tree
-----------------------
17
/ \
11 13
/ \ / \
12 9 10 2
/ / \ \
4 5 8 3
*/
//Add nodes
root = get_node(17);
root->left = get_node(11);
root->right = get_node(13);
root->right->right = get_node(2);
root->right->left = get_node(10);
root->right->left->right = get_node(3);
root->left->left = get_node(12);
root->left->left->left = get_node(4);
root->left->right = get_node(9);
root->left->right->left = get_node(5);
root->left->right->right = get_node(8);
//Display Tree elements
height_of_nodes(root);
return 0;
}
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
/*
Java Program
Sum of heights of all every nodes in a binary tree
*/
// Binary Tree node
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
//Define Binary Tree
public class BinaryTree
{
public Node root;
public int height;
public BinaryTree()
{
//Set root of tree
this.root = null;
this.height = 0;
}
//Calculates the sum of the height of each node in the binary tree
public int height_sum(Node node)
{
if (node == null)
{
return 0;
}
// Find the height of left and right subtree
int a = height_sum(node.left);
int b = height_sum(node.right);
if (a > b)
{
// add new height
this.height += a + 1;
return a + 1;
}
else
{
// add new height
this.height += b + 1;
return b + 1;
}
}
//Display inorder elements
public void print_inorder(Node node)
{
if (node != null)
{
print_inorder(node.left);
//Print node value
System.out.print(" " + node.data + "");
print_inorder(node.right);
}
}
//Handles the request to find height of each node in given binary tree
public void height_of_nodes()
{
if (this.root == null)
{
System.out.print("\n Empty Tree");
}
else
{
this.height = 0;
height_sum(this.root);
System.out.print("\n Tree Node : ");
print_inorder(this.root);
//Display the calculated result
System.out.print("\n Total height : " + this.height + " \n");
}
}
public static void main(String[] args)
{
//Create tree object
BinaryTree tree = new BinaryTree();
/* Binary Tree
-----------------------
17
/ \
11 13
/ \ / \
12 9 10 2
/ / \ \
4 5 8 3
*/
//Add nodes
tree.root = new Node(17);
tree.root.left = new Node(11);
tree.root.right = new Node(13);
tree.root.right.right = new Node(2);
tree.root.right.left = new Node(10);
tree.root.right.left.right = new Node(3);
tree.root.left.left = new Node(12);
tree.root.left.left.left = new Node(4);
tree.root.left.right = new Node(9);
tree.root.left.right.left = new Node(5);
tree.root.left.right.right = new Node(8);
//Display Tree elements
tree.height_of_nodes();
}
}
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Sum of heights of all every nodes in a binary tree
// Binary Tree node
class Node
{
public: int data;
Node *left;
Node *right;
Node(int data)
{
// Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
// Define Binary Tree
class BinaryTree
{
public: Node *root;
int height;
BinaryTree()
{
// Set root of tree
this->root = NULL;
this->height = 0;
}
// Calculates the sum of the height of each node in the binary tree
int height_sum(Node *node)
{
if (node == NULL)
{
return 0;
}
// Find the height of left and right subtree
int a = this->height_sum(node->left);
int b = this->height_sum(node->right);
if (a > b)
{
// add new height
this->height += a + 1;
return a + 1;
}
else
{
// add new height
this->height += b + 1;
return b + 1;
}
}
// Display inorder elements
void print_inorder(Node *node)
{
if (node != NULL)
{
this->print_inorder(node->left);
// Print node value
cout << " " << node->data << "";
this->print_inorder(node->right);
}
}
// Handles the request to find height of each node in given binary tree
void height_of_nodes()
{
if (this->root == NULL)
{
cout << "\n Empty Tree";
}
else
{
this->height = 0;
this->height_sum(this->root);
cout << "\n Tree Node : ";
this->print_inorder(this->root);
// Display the calculated result
cout << "\n Total height : " << this->height << " \n";
}
}
};
int main()
{
// Create tree object
BinaryTree tree = BinaryTree();
// Binary Tree
// -----------------------
// 17
// / \
// 11 13
// / \ / \
// 12 9 10 2
// / / \ \
// 4 5 8 3
//
// Add nodes
tree.root = new Node(17);
tree.root->left = new Node(11);
tree.root->right = new Node(13);
tree.root->right->right = new Node(2);
tree.root->right->left = new Node(10);
tree.root->right->left->right = new Node(3);
tree.root->left->left = new Node(12);
tree.root->left->left->left = new Node(4);
tree.root->left->right = new Node(9);
tree.root->left->right->left = new Node(5);
tree.root->left->right->right = new Node(8);
// Display Tree elements
tree.height_of_nodes();
return 0;
}
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
// Include namespace system
using System;
// C# Program
// Sum of heights of all every nodes in a binary tree
// Binary Tree node
public class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
public class BinaryTree
{
public Node root;
public int height;
public BinaryTree()
{
// Set root of tree
this.root = null;
this.height = 0;
}
// Calculates the sum of the height of each node in the binary tree
public int height_sum(Node node)
{
if (node == null)
{
return 0;
}
// Find the height of left and right subtree
int a = height_sum(node.left);
int b = height_sum(node.right);
if (a > b)
{
// add new height
this.height += a + 1;
return a + 1;
}
else
{
// add new height
this.height += b + 1;
return b + 1;
}
}
// Display inorder elements
public void print_inorder(Node node)
{
if (node != null)
{
print_inorder(node.left);
// Print node value
Console.Write(" " + node.data + "");
print_inorder(node.right);
}
}
// Handles the request to find height of each node in given binary tree
public void height_of_nodes()
{
if (this.root == null)
{
Console.Write("\n Empty Tree");
}
else
{
this.height = 0;
height_sum(this.root);
Console.Write("\n Tree Node : ");
print_inorder(this.root);
// Display the calculated result
Console.Write("\n Total height : " + this.height + " \n");
}
}
public static void Main(String[] args)
{
// Create tree object
BinaryTree tree = new BinaryTree();
// Binary Tree
// -----------------------
// 17
// / \
// 11 13
// / \ / \
// 12 9 10 2
// / / \ \
// 4 5 8 3
//
// Add nodes
tree.root = new Node(17);
tree.root.left = new Node(11);
tree.root.right = new Node(13);
tree.root.right.right = new Node(2);
tree.root.right.left = new Node(10);
tree.root.right.left.right = new Node(3);
tree.root.left.left = new Node(12);
tree.root.left.left.left = new Node(4);
tree.root.left.right = new Node(9);
tree.root.left.right.left = new Node(5);
tree.root.left.right.right = new Node(8);
// Display Tree elements
tree.height_of_nodes();
}
}
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
<?php
// Php Program
// Sum of heights of all every nodes in a binary tree
// Binary Tree node
class Node
{
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 $height;
function __construct()
{
// Set root of tree
$this->root = null;
$this->height = 0;
}
// Calculates the sum of the height of each node in the binary tree
public function height_sum($node)
{
if ($node == null)
{
return 0;
}
// Find the height of left and right subtree
$a = $this->height_sum($node->left);
$b = $this->height_sum($node->right);
if ($a > $b)
{
// add new height
$this->height += $a + 1;
return $a + 1;
}
else
{
// add new height
$this->height += $b + 1;
return $b + 1;
}
}
// Display inorder elements
public function print_inorder($node)
{
if ($node != null)
{
$this->print_inorder($node->left);
// Print node value
echo " ". $node->data ."";
$this->print_inorder($node->right);
}
}
// Handles the request to find height of each node in given binary tree
public function height_of_nodes()
{
if ($this->root == null)
{
echo "\n Empty Tree";
}
else
{
$this->height = 0;
$this->height_sum($this->root);
echo "\n Tree Node : ";
$this->print_inorder($this->root);
// Display the calculated result
echo "\n Total height : ". $this->height ." \n";
}
}
}
function main()
{
// Create tree object
$tree = new BinaryTree();
// Binary Tree
// -----------------------
// 17
// / \
// 11 13
// / \ / \
// 12 9 10 2
// / / \ \
// 4 5 8 3
//
// Add nodes
$tree->root = new Node(17);
$tree->root->left = new Node(11);
$tree->root->right = new Node(13);
$tree->root->right->right = new Node(2);
$tree->root->right->left = new Node(10);
$tree->root->right->left->right = new Node(3);
$tree->root->left->left = new Node(12);
$tree->root->left->left->left = new Node(4);
$tree->root->left->right = new Node(9);
$tree->root->left->right->left = new Node(5);
$tree->root->left->right->right = new Node(8);
// Display Tree elements
$tree->height_of_nodes();
}
main();
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
// Node Js Program
// Sum of heights of all every nodes in a binary tree
// Binary Tree node
class Node
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
class BinaryTree
{
constructor()
{
// Set root of tree
this.root = null;
this.height = 0;
}
// Calculates the sum of the height of each node in the binary tree
height_sum(node)
{
if (node == null)
{
return 0;
}
// Find the height of left and right subtree
var a = this.height_sum(node.left);
var b = this.height_sum(node.right);
if (a > b)
{
// add new height
this.height += a + 1;
return a + 1;
}
else
{
// add new height
this.height += b + 1;
return b + 1;
}
}
// Display inorder elements
print_inorder(node)
{
if (node != null)
{
this.print_inorder(node.left);
// Print node value
process.stdout.write(" " + node.data + "");
this.print_inorder(node.right);
}
}
// Handles the request to find height of each node in given binary tree
height_of_nodes()
{
if (this.root == null)
{
process.stdout.write("\n Empty Tree");
}
else
{
this.height = 0;
this.height_sum(this.root);
process.stdout.write("\n Tree Node : ");
this.print_inorder(this.root);
// Display the calculated result
process.stdout.write("\n Total height : " + this.height + " \n");
}
}
}
function main()
{
// Create tree object
var tree = new BinaryTree();
// Binary Tree
// -----------------------
// 17
// / \
// 11 13
// / \ / \
// 12 9 10 2
// / / \ \
// 4 5 8 3
//
// Add nodes
tree.root = new Node(17);
tree.root.left = new Node(11);
tree.root.right = new Node(13);
tree.root.right.right = new Node(2);
tree.root.right.left = new Node(10);
tree.root.right.left.right = new Node(3);
tree.root.left.left = new Node(12);
tree.root.left.left.left = new Node(4);
tree.root.left.right = new Node(9);
tree.root.left.right.left = new Node(5);
tree.root.left.right.right = new Node(8);
// Display Tree elements
tree.height_of_nodes();
}
main();
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
# Python 3 Program
# Sum of heights of all every nodes in a binary tree
# Binary Tree node
class Node :
def __init__(self, data) :
# Set node value
self.data = data
self.left = None
self.right = None
# Define Binary Tree
class BinaryTree :
def __init__(self) :
# Set root of tree
self.root = None
self.height = 0
# Calculates the sum of the height of each node in the binary tree
def height_sum(self, node) :
if (node == None) :
return 0
# Find the height of left and right subtree
a = self.height_sum(node.left)
b = self.height_sum(node.right)
if (a > b) :
# add new height
self.height += a + 1
return a + 1
else :
# add new height
self.height += b + 1
return b + 1
# Display inorder elements
def print_inorder(self, node) :
if (node != None) :
self.print_inorder(node.left)
# Print node value
print(" ", node.data ,"", end = "")
self.print_inorder(node.right)
# Handles the request to find height of each node in given binary tree
def height_of_nodes(self) :
if (self.root == None) :
print("\n Empty Tree", end = "")
else :
self.height = 0
self.height_sum(self.root)
print("\n Tree Node : ", end = "")
self.print_inorder(self.root)
# Display the calculated result
print("\n Total height : ", self.height ," \n", end = "")
def main() :
# Create tree object
tree = BinaryTree()
# Binary Tree
# -----------------------
# 17
# / \
# 11 13
# / \ / \
# 12 9 10 2
# / / \ \
# 4 5 8 3
#
# Add nodes
tree.root = Node(17)
tree.root.left = Node(11)
tree.root.right = Node(13)
tree.root.right.right = Node(2)
tree.root.right.left = Node(10)
tree.root.right.left.right = Node(3)
tree.root.left.left = Node(12)
tree.root.left.left.left = Node(4)
tree.root.left.right = Node(9)
tree.root.left.right.left = Node(5)
tree.root.left.right.right = Node(8)
# Display Tree elements
tree.height_of_nodes()
if __name__ == "__main__": main()
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
# Ruby Program
# Sum of heights of all every nodes in a binary tree
# Binary Tree node
class Node
# Define the accessor and reader of class Node
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, :height
attr_accessor :root, :height
def initialize()
# Set root of tree
self.root = nil
self.height = 0
end
# Calculates the sum of the height of each node in the binary tree
def height_sum(node)
if (node == nil)
return 0
end
# Find the height of left and right subtree
a = self.height_sum(node.left)
b = self.height_sum(node.right)
if (a > b)
# add new height
self.height += a + 1
return a + 1
else
# add new height
self.height += b + 1
return b + 1
end
end
# Display inorder elements
def print_inorder(node)
if (node != nil)
self.print_inorder(node.left)
# Print node value
print(" ", node.data ,"")
self.print_inorder(node.right)
end
end
# Handles the request to find height of each node in given binary tree
def height_of_nodes()
if (self.root == nil)
print("\n Empty Tree")
else
self.height = 0
self.height_sum(self.root)
print("\n Tree Node : ")
self.print_inorder(self.root)
# Display the calculated result
print("\n Total height : ", self.height ," \n")
end
end
end
def main()
# Create tree object
tree = BinaryTree.new()
# Binary Tree
# -----------------------
# 17
# / \
# 11 13
# / \ / \
# 12 9 10 2
# / / \ \
# 4 5 8 3
#
# Add nodes
tree.root = Node.new(17)
tree.root.left = Node.new(11)
tree.root.right = Node.new(13)
tree.root.right.right = Node.new(2)
tree.root.right.left = Node.new(10)
tree.root.right.left.right = Node.new(3)
tree.root.left.left = Node.new(12)
tree.root.left.left.left = Node.new(4)
tree.root.left.right = Node.new(9)
tree.root.left.right.left = Node.new(5)
tree.root.left.right.right = Node.new(8)
# Display Tree elements
tree.height_of_nodes()
end
main()
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
// Scala Program
// Sum of heights of all every nodes in a binary tree
// Binary Tree node
class Node(var data: Int , var left: Node , var right: Node)
{
def this(data: Int)
{
this(data, null, null);
}
}
// Define Binary Tree
class BinaryTree(var root: Node , var height: Int)
{
def this()
{
this(null, 0);
}
// Calculates the sum of the height of each node in the binary tree
def height_sum(node: Node): Int = {
if (node == null)
{
return 0;
}
// Find the height of left and right subtree
var a: Int = height_sum(node.left);
var b: Int = height_sum(node.right);
if (a > b)
{
// add new height
this.height += a + 1;
return a + 1;
}
else
{
// add new height
this.height += b + 1;
return b + 1;
}
}
// Display inorder elements
def print_inorder(node: Node): Unit = {
if (node != null)
{
print_inorder(node.left);
// Print node value
print(" " + node.data + "");
print_inorder(node.right);
}
}
// Handles the request to find height of each node in given binary tree
def height_of_nodes(): Unit = {
if (this.root == null)
{
print("\n Empty Tree");
}
else
{
this.height = 0;
height_sum(this.root);
print("\n Tree Node : ");
print_inorder(this.root);
// Display the calculated result
print("\n Total height : " + this.height + " \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create tree object
var tree: BinaryTree = new BinaryTree();
// Binary Tree
// -----------------------
// 17
// / \
// 11 13
// / \ / \
// 12 9 10 2
// / / \ \
// 4 5 8 3
//
// Add nodes
tree.root = new Node(17);
tree.root.left = new Node(11);
tree.root.right = new Node(13);
tree.root.right.right = new Node(2);
tree.root.right.left = new Node(10);
tree.root.right.left.right = new Node(3);
tree.root.left.left = new Node(12);
tree.root.left.left.left = new Node(4);
tree.root.left.right = new Node(9);
tree.root.left.right.left = new Node(5);
tree.root.left.right.right = new Node(8);
// Display Tree elements
tree.height_of_nodes();
}
}
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
// Swift 4 Program
// Sum of heights of all every nodes in a binary tree
// Binary Tree node
class Node
{
var data: Int;
var left: Node? ;
var right: Node? ;
init(_ data: Int)
{
// Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
// Define Binary Tree
class BinaryTree
{
var root: Node? ;
var height: Int;
init()
{
// Set root of tree
self.root = nil;
self.height = 0;
}
// Calculates the sum of the height of each node in the binary tree
func height_sum(_ node: Node? )->Int
{
if (node == nil)
{
return 0;
}
// Find the height of left and right subtree
let a: Int = self.height_sum(node!.left);
let b: Int = self.height_sum(node!.right);
if (a > b)
{
// add new height
self.height += a + 1;
return a + 1;
}
else
{
// add new height
self.height += b + 1;
return b + 1;
}
}
// Display inorder elements
func print_inorder(_ node: Node? )
{
if (node != nil)
{
self.print_inorder(node!.left);
// Print node value
print(" ", node!.data ,"", terminator: "");
self.print_inorder(node!.right);
}
}
// Handles the request to find height of each node in given binary tree
func height_of_nodes()
{
if (self.root == nil)
{
print("\n Empty Tree", terminator: "");
}
else
{
self.height = 0;
let _ = self.height_sum(self.root);
print("\n Tree Node : ", terminator: "");
self.print_inorder(self.root);
// Display the calculated result
print("\n Total height : ", self.height ," \n", terminator: "");
}
}
}
func main()
{
// Create tree object
let tree: BinaryTree = BinaryTree();
// Binary Tree
// -----------------------
// 17
// / \
// 11 13
// / \ / \
// 12 9 10 2
// / / \ \
// 4 5 8 3
//
// Add nodes
tree.root = Node(17);
tree.root!.left = Node(11);
tree.root!.right = Node(13);
tree.root!.right!.right = Node(2);
tree.root!.right!.left = Node(10);
tree.root!.right!.left!.right = Node(3);
tree.root!.left!.left = Node(12);
tree.root!.left!.left!.left = Node(4);
tree.root!.left!.right = Node(9);
tree.root!.left!.right!.left = Node(5);
tree.root!.left!.right!.right = Node(8);
// Display Tree elements
tree.height_of_nodes();
}
main();
Output
Tree Node : 4 12 11 5 9 8 17 10 3 13 2
Total height : 21
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