Find sum of all nodes of the given perfect binary tree
Here given code implementation process.
/*
C Program
Find sum of all nodes of the given perfect binary tree
Recursive solution
*/
#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 this
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;
}
//Display pre order elements
void preorder(struct Node *node)
{
if (node != NULL)
{
//Print node value
printf(" %d", node->data);
preorder(node->left);
preorder(node->right);
}
}
//Calculate min depth of binary tree
int min_depth(struct Node *node)
{
if (node == NULL)
{
return 0;
}
// Recursively, Finding the min height of nodes
int a = min_depth(node->left);
int b = min_depth(node->right);
if (a > b)
{
return b + 1;
}
else
{
return a + 1;
}
}
//Returns the sum of all nodes which are less than or equal to given level
int level_sum(struct Node *node, int k, int level)
{
if (node == NULL)
{
return 0;
}
if (k <= level)
{
// Calculate node sum
return node->data + level_sum(node->left, k + 1, level) + level_sum(node->right, k + 1, level);
}
else
{
return 0;
}
}
//Find the sum of nodes of binary tree which is perfect binary tree
int perfect_binary_tree_sum(struct Node *root)
{
if (root == NULL)
{
printf("\n Empty Binary Tree \n");
return 0;
}
//First find min depth in binary tree
int level = min_depth(root);
//It return sum of nodes which are exist in root to min depth
return level_sum(root, 1, level);
}
int main()
{
struct Node *root = NULL;
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
root = get_node(6);
root->left = get_node(7);
root->left->left = get_node(8);
root->left->right = get_node(10);
root->left->right->right = get_node(1);
root->left->right->right->left = get_node(9);
root->left->right->left = get_node(1);
root->right = get_node(3);
root->right->left = get_node(2);
root->right->right = get_node(1);
root->right->right->right = get_node(5);
root->right->right->left = get_node(4);
//Display Tree Element
printf("\n Tree Nodes : ");
preorder(root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
//Display Calculated Result
printf("\n Perfect binary tree sum : %d \n", perfect_binary_tree_sum(root));
return 0;
}
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
/*
Java Program
Find the sum of the node having child node x
Recursive solution
*/
//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;
}
}
public class BinaryTree
{
public Node root;
public BinaryTree()
{
//Set initial tree root to null
this.root = null;
}
//Display pre order elements
public void preorder(Node node)
{
if (node != null)
{
//Print node value
System.out.print(" " + node.data);
preorder(node.left);
preorder(node.right);
}
}
//Calculate min depth of binary tree
public int min_depth(Node node)
{
if (node == null)
{
return 0;
}
// Recursively, Finding the min height of nodes
int a = min_depth(node.left);
int b = min_depth(node.right);
if (a > b)
{
return b + 1;
}
else
{
return a + 1;
}
}
//Returns the sum of all nodes which are less than or equal to given level
public int level_sum(Node node, int k, int level)
{
if (node == null)
{
return 0;
}
if (k <= level)
{
//Calculate node sum
return node.data + level_sum(node.left, k + 1, level) + level_sum(node.right, k + 1, level);
}
else
{
return 0;
}
}
//Find the sum of nodes of binary tree which is perfect binary tree
public int perfect_binary_tree_sum(Node root)
{
if (root == null)
{
System.out.print("\n Empty Binary Tree \n");
return 0;
}
//First find min depth in binary tree
int level = min_depth(root);
//It return sum of nodes which are exist in root to min depth
return level_sum(root, 1, level);
}
public static void main(String[] args)
{
//Make object of binary tree
BinaryTree tree = new BinaryTree();
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
tree.root = new Node(6);
tree.root.left = new Node(7);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(10);
tree.root.left.right.right = new Node(1);
tree.root.left.right.right.left = new Node(9);
tree.root.left.right.left = new Node(1);
tree.root.right = new Node(3);
tree.root.right.left = new Node(2);
tree.root.right.right = new Node(1);
tree.root.right.right.right = new Node(5);
tree.root.right.right.left = new Node(4);
//Display Tree Element
System.out.print("\n Tree Nodes : ");
tree.preorder(tree.root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
//Display Calculated Result
System.out.print("\n Perfect binary tree sum : " + tree.perfect_binary_tree_sum(tree.root) + " \n");
}
}
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find the sum of the node having child node x
Recursive solution
*/
// 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;
}
};
class BinaryTree
{
public: Node *root;
BinaryTree()
{
// Set initial tree root to null
this->root = NULL;
}
// Display pre order elements
void preorder(Node *node)
{
if (node != NULL)
{
// Print node value
cout << " " << node->data;
this->preorder(node->left);
this->preorder(node->right);
}
}
// Calculate min depth of binary tree
int min_depth(Node *node)
{
if (node == NULL)
{
return 0;
}
// Recursively, Finding the min height of nodes
int a = this->min_depth(node->left);
int b = this->min_depth(node->right);
if (a > b)
{
return b + 1;
}
else
{
return a + 1;
}
}
// Returns the sum of all nodes which are less than or equal to given level
int level_sum(Node *node, int k, int level)
{
if (node == NULL)
{
return 0;
}
if (k <= level)
{
// Calculate node sum
return node->data + this->level_sum(node->left, k + 1, level) + this->level_sum(node->right, k + 1, level);
}
else
{
return 0;
}
}
// Find the sum of nodes of binary tree which is perfect binary tree
int perfect_binary_tree_sum(Node *root)
{
if (root == NULL)
{
cout << "\n Empty Binary Tree \n";
return 0;
}
// First find min depth in binary tree
int level = this->min_depth(root);
// It return sum of nodes which are exist in root to min depth
return this->level_sum(root, 1, level);
}
};
int main()
{
// Make object of binary tree
BinaryTree tree = BinaryTree();
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
tree.root = new Node(6);
tree.root->left = new Node(7);
tree.root->left->left = new Node(8);
tree.root->left->right = new Node(10);
tree.root->left->right->right = new Node(1);
tree.root->left->right->right->left = new Node(9);
tree.root->left->right->left = new Node(1);
tree.root->right = new Node(3);
tree.root->right->left = new Node(2);
tree.root->right->right = new Node(1);
tree.root->right->right->right = new Node(5);
tree.root->right->right->left = new Node(4);
// Display Tree Element
cout << "\n Tree Nodes : ";
tree.preorder(tree.root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
// Display Calculated Result
cout << "\n Perfect binary tree sum : " << tree.perfect_binary_tree_sum(tree.root) << " \n";
return 0;
}
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
//Include namespace system
using System;
/*
C# Program
Find the sum of the node having child node x
Recursive solution
*/
// 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;
}
}
public class BinaryTree
{
public Node root;
public BinaryTree()
{
// Set initial tree root to null
this.root = null;
}
// Display pre order elements
public void preorder(Node node)
{
if (node != null)
{
// Print node value
Console.Write(" " + node.data);
preorder(node.left);
preorder(node.right);
}
}
// Calculate min depth of binary tree
public int min_depth(Node node)
{
if (node == null)
{
return 0;
}
// Recursively, Finding the min height of nodes
int a = min_depth(node.left);
int b = min_depth(node.right);
if (a > b)
{
return b + 1;
}
else
{
return a + 1;
}
}
// Returns the sum of all nodes which are less than or equal to given level
public int level_sum(Node node, int k, int level)
{
if (node == null)
{
return 0;
}
if (k <= level)
{
// Calculate node sum
return node.data + level_sum(node.left, k + 1, level) + level_sum(node.right, k + 1, level);
}
else
{
return 0;
}
}
// Find the sum of nodes of binary tree which is perfect binary tree
public int perfect_binary_tree_sum(Node root)
{
if (root == null)
{
Console.Write("\n Empty Binary Tree \n");
return 0;
}
// First find min depth in binary tree
int level = min_depth(root);
// It return sum of nodes which are exist in root to min depth
return level_sum(root, 1, level);
}
public static void Main(String[] args)
{
// Make object of binary tree
BinaryTree tree = new BinaryTree();
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
tree.root = new Node(6);
tree.root.left = new Node(7);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(10);
tree.root.left.right.right = new Node(1);
tree.root.left.right.right.left = new Node(9);
tree.root.left.right.left = new Node(1);
tree.root.right = new Node(3);
tree.root.right.left = new Node(2);
tree.root.right.right = new Node(1);
tree.root.right.right.right = new Node(5);
tree.root.right.right.left = new Node(4);
// Display Tree Element
Console.Write("\n Tree Nodes : ");
tree.preorder(tree.root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
// Display Calculated Result
Console.Write("\n Perfect binary tree sum : " + tree.perfect_binary_tree_sum(tree.root) + " \n");
}
}
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
<?php
/*
Php Program
Find the sum of the node having child node x
Recursive solution
*/
// 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;
}
}
class BinaryTree
{
public $root;
function __construct()
{
// Set initial tree root to null
$this->root = null;
}
// Display pre order elements
public function preorder($node)
{
if ($node != null)
{
// Print node value
echo " ". $node->data;
$this->preorder($node->left);
$this->preorder($node->right);
}
}
// Calculate min depth of binary tree
public function min_depth($node)
{
if ($node == null)
{
return 0;
}
// Recursively, Finding the min height of nodes
$a = $this->min_depth($node->left);
$b = $this->min_depth($node->right);
if ($a > $b)
{
return $b + 1;
}
else
{
return $a + 1;
}
}
// Returns the sum of all nodes which are less than or equal to given level
public function level_sum($node, $k, $level)
{
if ($node == null)
{
return 0;
}
if ($k <= $level)
{
// Calculate node sum
return $node->data + $this->level_sum($node->left, $k + 1, $level) +
$this->level_sum($node->right, $k + 1, $level);
}
else
{
return 0;
}
}
// Find the sum of nodes of binary tree which is perfect binary tree
public function perfect_binary_tree_sum($root)
{
if ($root == null)
{
echo "\n Empty Binary Tree \n";
return 0;
}
// First find min depth in binary tree
$level = $this->min_depth($root);
// It return sum of nodes which are exist in root to min depth
return $this->level_sum($root, 1, $level);
}
}
function main()
{
// Make object of binary tree
$tree = new BinaryTree();
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
$tree->root = new Node(6);
$tree->root->left = new Node(7);
$tree->root->left->left = new Node(8);
$tree->root->left->right = new Node(10);
$tree->root->left->right->right = new Node(1);
$tree->root->left->right->right->left = new Node(9);
$tree->root->left->right->left = new Node(1);
$tree->root->right = new Node(3);
$tree->root->right->left = new Node(2);
$tree->root->right->right = new Node(1);
$tree->root->right->right->right = new Node(5);
$tree->root->right->right->left = new Node(4);
// Display Tree Element
echo "\n Tree Nodes : ";
$tree->preorder($tree->root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
// Display Calculated Result
echo "\n Perfect binary tree sum : ". $tree->perfect_binary_tree_sum($tree->root) ." \n";
}
main();
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
/*
Node Js Program
Find the sum of the node having child node x
Recursive solution
*/
// Binary Tree node
class Node
{
constructor(data)
{
// set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
// Set initial tree root to null
this.root = null;
}
// Display pre order elements
preorder(node)
{
if (node != null)
{
// Print node value
process.stdout.write(" " + node.data);
this.preorder(node.left);
this.preorder(node.right);
}
}
// Calculate min depth of binary tree
min_depth(node)
{
if (node == null)
{
return 0;
}
// Recursively, Finding the min height of nodes
var a = this.min_depth(node.left);
var b = this.min_depth(node.right);
if (a > b)
{
return b + 1;
}
else
{
return a + 1;
}
}
// Returns the sum of all nodes which are less than or equal to given level
level_sum(node, k, level)
{
if (node == null)
{
return 0;
}
if (k <= level)
{
// Calculate node sum
return node.data + this.level_sum(node.left, k + 1, level) +
this.level_sum(node.right, k + 1, level);
}
else
{
return 0;
}
}
// Find the sum of nodes of binary tree which is perfect binary tree
perfect_binary_tree_sum(root)
{
if (root == null)
{
process.stdout.write("\n Empty Binary Tree \n");
return 0;
}
// First find min depth in binary tree
var level = this.min_depth(root);
// It return sum of nodes which are exist in root to min depth
return this.level_sum(root, 1, level);
}
}
function main()
{
// Make object of binary tree
var tree = new BinaryTree();
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
tree.root = new Node(6);
tree.root.left = new Node(7);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(10);
tree.root.left.right.right = new Node(1);
tree.root.left.right.right.left = new Node(9);
tree.root.left.right.left = new Node(1);
tree.root.right = new Node(3);
tree.root.right.left = new Node(2);
tree.root.right.right = new Node(1);
tree.root.right.right.right = new Node(5);
tree.root.right.right.left = new Node(4);
// Display Tree Element
process.stdout.write("\n Tree Nodes : ");
tree.preorder(tree.root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
// Display Calculated Result
process.stdout.write("\n Perfect binary tree sum : " + tree.perfect_binary_tree_sum(tree.root) + " \n");
}
main();
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
# Python 3 Program
# Find the sum of the node having child node x
# Recursive solution
# Binary Tree node
class Node :
def __init__(self, data) :
# set node value
self.data = data
self.left = None
self.right = None
class BinaryTree :
def __init__(self) :
# Set initial tree root to null
self.root = None
# Display pre order elements
def preorder(self, node) :
if (node != None) :
# Print node value
print(" ", node.data, end = "")
self.preorder(node.left)
self.preorder(node.right)
# Calculate min depth of binary tree
def min_depth(self, node) :
if (node == None) :
return 0
# Recursively, Finding the min height of nodes
a = self.min_depth(node.left)
b = self.min_depth(node.right)
if (a > b) :
return b + 1
else :
return a + 1
# Returns the sum of all nodes which are less than or equal to given level
def level_sum(self, node, k, level) :
if (node == None) :
return 0
if (k <= level) :
# Calculate node sum
return node.data + self.level_sum(node.left, k + 1, level) + self.level_sum(node.right, k + 1, level)
else :
return 0
# Find the sum of nodes of binary tree which is perfect binary tree
def perfect_binary_tree_sum(self, root) :
if (root == None) :
print("\n Empty Binary Tree \n", end = "")
return 0
# First find min depth in binary tree
level = self.min_depth(root)
# It return sum of nodes which are exist in root to min depth
return self.level_sum(root, 1, level)
def main() :
# Make object of binary tree
tree = BinaryTree()
#
# constructor binary tree
# -----------------------
# 6
# / \
# / \
# 7 3
# / \ / \
# 8 10 2 1
# / \ / \
# 1 1 4 5
# /
# 9
# -----------------------
#
tree.root = Node(6)
tree.root.left = Node(7)
tree.root.left.left = Node(8)
tree.root.left.right = Node(10)
tree.root.left.right.right = Node(1)
tree.root.left.right.right.left = Node(9)
tree.root.left.right.left = Node(1)
tree.root.right = Node(3)
tree.root.right.left = Node(2)
tree.root.right.right = Node(1)
tree.root.right.right.right = Node(5)
tree.root.right.right.left = Node(4)
# Display Tree Element
print("\n Tree Nodes : ", end = "")
tree.preorder(tree.root)
#
# Perfect form of above binary tree
# -----------------------
# 6
# / \
# / \
# 7 3
# / \ / \
# 8 10 2 1
# -----------------------
#
# Display Calculated Result
print("\n Perfect binary tree sum : ", tree.perfect_binary_tree_sum(tree.root) )
if __name__ == "__main__": main()
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
# Ruby Program
# Find the sum of the node having child node x
# Recursive solution
# 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
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_reader :root
attr_accessor :root
def initialize()
# Set initial tree root to null
self.root = nil
end
# Display pre order elements
def preorder(node)
if (node != nil)
# Print node value
print(" ", node.data)
self.preorder(node.left)
self.preorder(node.right)
end
end
# Calculate min depth of binary tree
def min_depth(node)
if (node == nil)
return 0
end
# Recursively, Finding the min height of nodes
a = self.min_depth(node.left)
b = self.min_depth(node.right)
if (a > b)
return b + 1
else
return a + 1
end
end
# Returns the sum of all nodes which are less than or equal to given level
def level_sum(node, k, level)
if (node == nil)
return 0
end
if (k <= level)
# Calculate node sum
return node.data + self.level_sum(node.left, k + 1, level) + self.level_sum(node.right, k + 1, level)
else
return 0
end
end
# Find the sum of nodes of binary tree which is perfect binary tree
def perfect_binary_tree_sum(root)
if (root == nil)
print("\n Empty Binary Tree \n")
return 0
end
# First find min depth in binary tree
level = self.min_depth(root)
# It return sum of nodes which are exist in root to min depth
return self.level_sum(root, 1, level)
end
end
def main()
# Make object of binary tree
tree = BinaryTree.new()
#
# constructor binary tree
# -----------------------
# 6
# / \
# / \
# 7 3
# / \ / \
# 8 10 2 1
# / \ / \
# 1 1 4 5
# /
# 9
# -----------------------
#
tree.root = Node.new(6)
tree.root.left = Node.new(7)
tree.root.left.left = Node.new(8)
tree.root.left.right = Node.new(10)
tree.root.left.right.right = Node.new(1)
tree.root.left.right.right.left = Node.new(9)
tree.root.left.right.left = Node.new(1)
tree.root.right = Node.new(3)
tree.root.right.left = Node.new(2)
tree.root.right.right = Node.new(1)
tree.root.right.right.right = Node.new(5)
tree.root.right.right.left = Node.new(4)
# Display Tree Element
print("\n Tree Nodes : ")
tree.preorder(tree.root)
#
# Perfect form of above binary tree
# -----------------------
# 6
# / \
# / \
# 7 3
# / \ / \
# 8 10 2 1
# -----------------------
#
# Display Calculated Result
print("\n Perfect binary tree sum : ", tree.perfect_binary_tree_sum(tree.root) ," \n")
end
main()
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
/*
Scala Program
Find the sum of the node having child node x
Recursive solution
*/
// Binary Tree node
class Node(var data: Int , var left: Node , var right: Node)
{
def this(data: Int)
{
this(data, null, null);
}
}
class BinaryTree(var root: Node)
{
def this()
{
this(null);
}
// Display pre order elements
def preorder(node: Node): Unit = {
if (node != null)
{
// Print node value
print(" " + node.data);
preorder(node.left);
preorder(node.right);
}
}
// Calculate min depth of binary tree
def min_depth(node: Node): Int = {
if (node == null)
{
return 0;
}
// Recursively, Finding the min height of nodes
var a: Int = min_depth(node.left);
var b: Int = min_depth(node.right);
if (a > b)
{
return b + 1;
}
else
{
return a + 1;
}
}
// Returns the sum of all nodes which are less than or equal to given level
def level_sum(node: Node, k: Int, level: Int): Int = {
if (node == null)
{
return 0;
}
if (k <= level)
{
// Calculate node sum
return node.data +
level_sum(node.left, k + 1, level) +
level_sum(node.right, k + 1, level);
}
else
{
return 0;
}
}
// Find the sum of nodes of binary tree which is perfect binary tree
def perfect_binary_tree_sum(root: Node): Int = {
if (root == null)
{
print("\n Empty Binary Tree \n");
return 0;
}
// First find min depth in binary tree
var level: Int = min_depth(root);
// It return sum of nodes which are exist in root to min depth
return level_sum(root, 1, level);
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Make object of binary tree
var tree: BinaryTree = new BinaryTree();
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
tree.root = new Node(6);
tree.root.left = new Node(7);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(10);
tree.root.left.right.right = new Node(1);
tree.root.left.right.right.left = new Node(9);
tree.root.left.right.left = new Node(1);
tree.root.right = new Node(3);
tree.root.right.left = new Node(2);
tree.root.right.right = new Node(1);
tree.root.right.right.right = new Node(5);
tree.root.right.right.left = new Node(4);
// Display Tree Element
print("\n Tree Nodes : ");
tree.preorder(tree.root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
// Display Calculated Result
print("\n Perfect binary tree sum : " + tree.perfect_binary_tree_sum(tree.root) + " \n");
}
}
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
/*
Swift 4 Program
Find the sum of the node having child node x
Recursive solution
*/
// 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;
}
}
class BinaryTree
{
var root: Node? ;
init()
{
// Set initial tree root to null
self.root = nil;
}
// Display pre order elements
func preorder(_ node: Node? )
{
if (node != nil)
{
// Print node value
print(" ", node!.data, terminator: "");
self.preorder(node!.left);
self.preorder(node!.right);
}
}
// Calculate min depth of binary tree
func min_depth(_ node: Node? )->Int
{
if (node == nil)
{
return 0;
}
// Recursively, Finding the min height of nodes
let a: Int = self.min_depth(node!.left);
let b: Int = self.min_depth(node!.right);
if (a > b)
{
return b + 1;
}
else
{
return a + 1;
}
}
// Returns the sum of all nodes which are less than or equal to given level
func level_sum(_ node: Node? , _ k : Int, _ level: Int)->Int
{
if (node == nil)
{
return 0;
}
if (k <= level)
{
// Calculate node sum
return node!.data +
self.level_sum(node!.left, k + 1, level) +
self.level_sum(node!.right, k + 1, level);
}
else
{
return 0;
}
}
// Find the sum of nodes of binary tree which is perfect binary tree
func perfect_binary_tree_sum(_ root: Node? )->Int
{
if (root == nil)
{
print("\n Empty Binary Tree \n", terminator: "");
return 0;
}
// First find min depth in binary tree
let level: Int = self.min_depth(root);
// It return sum of nodes which are exist in root to min depth
return self.level_sum(root, 1, level);
}
}
func main()
{
// Make object of binary tree
let tree: BinaryTree = BinaryTree();
/*
constructor binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
/ \ / \
1 1 4 5
/
9
-----------------------
*/
tree.root = Node(6);
tree.root!.left = Node(7);
tree.root!.left!.left = Node(8);
tree.root!.left!.right = Node(10);
tree.root!.left!.right!.right = Node(1);
tree.root!.left!.right!.right!.left = Node(9);
tree.root!.left!.right!.left = Node(1);
tree.root!.right = Node(3);
tree.root!.right!.left = Node(2);
tree.root!.right!.right = Node(1);
tree.root!.right!.right!.right = Node(5);
tree.root!.right!.right!.left = Node(4);
// Display Tree Element
print("\n Tree Nodes : ", terminator: "");
tree.preorder(tree.root);
/*
Perfect form of above binary tree
-----------------------
6
/ \
/ \
7 3
/ \ / \
8 10 2 1
-----------------------
*/
// Display Calculated Result
print("\n Perfect binary tree sum : ", tree.perfect_binary_tree_sum(tree.root) ," \n", terminator: "");
}
main();
Output
Tree Nodes : 6 7 8 10 1 1 9 3 2 1 4 5
Perfect binary tree sum : 37
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