# Count internal nodes in binary tree

Here given code implementation process.

``````/*
C Program
+ Count internal nodes in binary tree
*/
#include <stdio.h>

#include <stdlib.h>
//structure of Binary Tree node
struct Node
{
int data;
struct Node *left, *right;
};
//Create a binary tree nodes and node fields (data,pointer)
//And returning the reference of newly nodes
struct Node *insert(int data)
{
//create dynamic memory to new binary tree 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; //Initially node left-pointer is NULL
new_node->right = NULL; //Initially node right-pointer is NULL
}
else
{
printf("Memory Overflow\n");
exit(0); //Terminate program execution
}
//return reference
return new_node;
}
//Display tree element inorder form
int count_internal_nodes(struct Node *root)
{
//Check whether given root node is empty or not
if (root != NULL)
{
//Calculate left and right sub tree internal nodes
int result = count_internal_nodes(root->left) + count_internal_nodes(root->right);
if (root->left != NULL || root->right != NULL)
{
//When get a new internal node
return result + 1;
}
else
{
//return the previous calculated result
return result;
}
}
else
{
//When root node is empty
return 0;
}
}
int main()
{
struct Node *root = NULL;
/*Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
root = insert(9);
root->left = insert(7);
root->right = insert(2);
root->right->right = insert(3);
root->right->left = insert(6);
root->right->left->right = insert(4);
root->left->left = insert(5);
//Display Tree elements
int result = count_internal_nodes(root);
printf(" Total internal nodes : %d\n", result);
return 0;
}``````

#### Output

`` Total internal nodes : 4``
``````/*
Java Program
Count internal nodes in binary tree
*/
//Binary Tree node
class Node
{
int data;
Node left, right;
//make a tree node
public Node(int data)
{
//assign field values
this.data = data;
this.left = null;
this.right = null;
}
};
class BinaryTree
{
public Node root;
public BinaryTree()
{
//set initial tree root to null
this.root = null;
}
//Display tree element inorder form
public int count_internal_nodes(Node root)
{
//Check whether given root node is empty or not
if (root != null)
{
//Calculate left and right sub tree internal nodes
int result = count_internal_nodes(root.left) + count_internal_nodes(root.right);
if (root.left != null || root.right != null)
{
//When get a new internal node
return result + 1;
}
else
{
//return the previous calculated result
return result;
}
}
else
{
//When root node is empty
return 0;
}
}
public static void main(String[] args)
{
//Make object of Binary Tree
BinaryTree obj = new BinaryTree();
/* Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
obj.root = new Node(9);
obj.root.left = new Node(7);
obj.root.right = new Node(2);
obj.root.right.right = new Node(3);
obj.root.right.left = new Node(6);
obj.root.right.left.right = new Node(4);
obj.root.left.left = new Node(5);
//Display Tree elements
int result = obj.count_internal_nodes(obj.root);
System.out.print(" Total internal nodes : " + result + "\n");
}
}``````

#### Output

`` Total internal nodes : 4``
``````//Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Count internal nodes in binary tree
*/
//Binary Tree node
class Node
{
public: int data;
Node * left, * right;
//make a tree node
Node(int data)
{
//assign field values
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 tree element inorder form
int count_internal_nodes(Node * root)
{
//Check whether given root node is empty or not
if (root != NULL)
{
//Calculate left and right sub tree internal nodes
int result = count_internal_nodes(root->left) + count_internal_nodes(root->right);
if (root->left != NULL || root->right != NULL)
{
return
//When get a new internal node
result + 1;
}
else
{
return
//return the previous calculated result
result;
}
}
else
{
return
//When root node is empty
0;
}
}
};
int main()
{
//Make object of Binary Tree
BinaryTree  obj =  BinaryTree();
/* Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
obj.root = new Node(9);
obj.root->left = new Node(7);
obj.root->right = new Node(2);
obj.root->right->right = new Node(3);
obj.root->right->left = new Node(6);
obj.root->right->left->right = new Node(4);
obj.root->left->left = new Node(5);
//Display Tree elements
int result = obj.count_internal_nodes(obj.root);
cout << " Total internal nodes : " << result << "\n";
return 0;
}``````

#### Output

`` Total internal nodes : 4``
``````/*
C# Program
Count internal nodes in binary tree
*/
//Include namespace system
using System;

//Binary Tree node
public class Node
{
public int data;
public Node left, right;
//make a tree node
public Node(int data)
{
//assign field values
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 tree element inorder form
public int count_internal_nodes(Node root)
{
//Check whether given root node is empty or not
if (root != null)
{
//Calculate left and right sub tree internal nodes
int result = count_internal_nodes(root.left) + count_internal_nodes(root.right);
if (root.left != null || root.right != null)
{
return result + 1;
}
else
{
return result;
}
}
else
{
return 0;
}
}
public static void Main(String[] args)
{
//Make object of Binary Tree
BinaryTree obj = new BinaryTree();
/* Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
obj.root = new Node(9);
obj.root.left = new Node(7);
obj.root.right = new Node(2);
obj.root.right.right = new Node(3);
obj.root.right.left = new Node(6);
obj.root.right.left.right = new Node(4);
obj.root.left.left = new Node(5);
//Display Tree elements
int result = obj.count_internal_nodes(obj.root);
Console.Write(" Total internal nodes : " + result + "\n");
}
}``````

#### Output

`` Total internal nodes : 4``
``````<?php
/*
Php Program
Count internal nodes in binary tree
*/
//Binary Tree node
class Node
{
public \$data;
public \$left;
public \$right;
//make a tree node
function __construct(\$data)
{
//assign field values
\$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 tree element inorder form
public	function count_internal_nodes(\$root)
{
//Check whether given root node is empty or not
if (\$root != null)
{
//Calculate left and right sub tree internal nodes
\$result = \$this->count_internal_nodes(\$root->left) + \$this->count_internal_nodes(\$root->right);
if (\$root->left != null || \$root->right != null)
{
return
//When get a new internal node
\$result + 1;
}
else
{
return
//return the previous calculated result
\$result;
}
}
else
{
return
//When root node is empty
0;
}
}
}

function main()
{
//Make object of Binary Tree
\$obj = new BinaryTree();
/* Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
\$obj->root = new Node(9);
\$obj->root->left = new Node(7);
\$obj->root->right = new Node(2);
\$obj->root->right->right = new Node(3);
\$obj->root->right->left = new Node(6);
\$obj->root->right->left->right = new Node(4);
\$obj->root->left->left = new Node(5);
//Display Tree elements
\$result = \$obj->count_internal_nodes(\$obj->root);
echo " Total internal nodes : ". \$result ."\n";
}
main();``````

#### Output

`` Total internal nodes : 4``
``````/*
Node Js Program
Count internal nodes in binary tree
*/
//Binary Tree node
class Node
{
;
//make a tree node
constructor(data)
{
//assign field values
this.data = data;
this.left = null;
this.right = null;
}
};
class BinaryTree
{
constructor()
{
//set initial tree root to null
this.root = null;
}
//Display tree element inorder form
count_internal_nodes(root)
{
//Check whether given root node is empty or not
if (root != null)
{
//Calculate left and right sub tree internal nodes
var result = this.count_internal_nodes(root.left) + this.count_internal_nodes(root.right);
if (root.left != null || root.right != null)
{

//When get a new internal node
return result + 1;
}
else
{
//return the previous calculated result
return result;
}
}
else
{
//When root node is empty
return 0;
}
}
}

function main()
{
//Make object of Binary Tree
var obj = new BinaryTree();
/* Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
obj.root = new Node(9);
obj.root.left = new Node(7);
obj.root.right = new Node(2);
obj.root.right.right = new Node(3);
obj.root.right.left = new Node(6);
obj.root.right.left.right = new Node(4);
obj.root.left.left = new Node(5);
//Display Tree elements
var result = obj.count_internal_nodes(obj.root);
process.stdout.write(" Total internal nodes : " + result + "\n");
}
main();``````

#### Output

`` Total internal nodes : 4``
``````#   Python 3 Program
#   Count internal nodes in binary tree

# Binary Tree node
class Node :

# make a tree node
def __init__(self, data) :
# assign field values
self.data = data
self.left = None
self.right = None

class BinaryTree :

def __init__(self) :
# set initial tree root to null
self.root = None

# Display tree element inorder form
def count_internal_nodes(self, root) :
# Check whether given root node is empty or not
if (root != None) :
# Calculate left and right sub tree internal nodes
result = self.count_internal_nodes(root.left) + self.count_internal_nodes(root.right)
if (root.left != None or root.right != None) :
# When get a new internal node
return result + 1
else :
# return the previous calculated result
return result

else :
# When root node is empty
return 0

def main() :
# Make object of Binary Tree
obj = BinaryTree()
#  Make A Binary Tree
# 		-----------------------
# 		         9
# 		       /  \
# 		      7    2
# 		     /    /  \
# 		    5    6    3
# 		          \
# 		           4
#

# Insertion of binary tree nodes
obj.root = Node(9)
obj.root.left = Node(7)
obj.root.right = Node(2)
obj.root.right.right = Node(3)
obj.root.right.left = Node(6)
obj.root.right.left.right = Node(4)
obj.root.left.left = Node(5)
# Display Tree elements
result = obj.count_internal_nodes(obj.root)
print(" Total internal nodes : ", result ,"\n", end = "")

if __name__ == "__main__": main()``````

#### Output

`` Total internal nodes :  4``
``````#   Ruby Program
#   Count internal nodes in binary tree

# Binary Tree node
class Node

# Define the accessor and reader of class Node
attr_accessor :data, :left, :right

# make a tree node
def initialize(data)

# assign field values
self.data = data
self.left = nil
self.right = nil
end
end
class BinaryTree

# Define the accessor and reader of class BinaryTree
attr_accessor :root

def initialize()

# set initial tree root to null
self.root = nil
end
# Display tree element inorder form
def count_internal_nodes(root)

# Check whether given root node is empty or not
if (root != nil)

# Calculate left and right sub tree internal nodes
result = self.count_internal_nodes(root.left) + self.count_internal_nodes(root.right)
if (root.left != nil || root.right != nil)

# When get a new internal node
return result + 1
else

# return the previous calculated result
return result
end
else

# When root node is empty
return 0
end
end
end
def main()

# Make object of Binary Tree
obj = BinaryTree.new()
#  Make A Binary Tree
# 		-----------------------
# 		         9
# 		       /  \
# 		      7    2
# 		     /    /  \
# 		    5    6    3
# 		          \
# 		           4
#

# Insertion of binary tree nodes
obj.root = Node.new(9)
obj.root.left = Node.new(7)
obj.root.right = Node.new(2)
obj.root.right.right = Node.new(3)
obj.root.right.left = Node.new(6)
obj.root.right.left.right = Node.new(4)
obj.root.left.left = Node.new(5)
# Display Tree elements
result = obj.count_internal_nodes(obj.root)
print(" Total internal nodes : ", result ,"\n")
end
main()``````

#### Output

`````` Total internal nodes : 4
``````
``````/*
Scala Program
Count internal nodes in binary tree
*/
//Binary Tree node
class Node(var data: Int,
var left: Node,
var right: Node)
{
;
//make a tree node
def this(data: Int)
{
//assign field values
this(data, null,null);
}
};
class BinaryTree(var root: Node)
{
def this()
{
//set initial tree root to null
this(null);
}
//Display tree element inorder form
def count_internal_nodes(root: Node): Int = {
//Check whether given root node is empty or not
if (root != null)
{
//Calculate left and right sub tree internal nodes
var result: Int = count_internal_nodes(root.left) + count_internal_nodes(root.right);
if (root.left != null || root.right != null)
{

//When get a new internal node
return result + 1;
}
else
{

//return the previous calculated result
return result;
}
}
else
{

//When root node is empty
return 0;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
//Make object of Binary Tree
var obj: BinaryTree = new BinaryTree();
/* Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
obj.root = new Node(9);
obj.root.left = new Node(7);
obj.root.right = new Node(2);
obj.root.right.right = new Node(3);
obj.root.right.left = new Node(6);
obj.root.right.left.right = new Node(4);
obj.root.left.left = new Node(5);
//Display Tree elements
var result: Int = obj.count_internal_nodes(obj.root);
print(" Total internal nodes : " + result + "\n");
}
}``````

#### Output

`` Total internal nodes : 4``
``````/*
Swift Program
Count internal nodes in binary tree
*/
//Binary Tree node
class Node
{
var data: Int;
var left: Node? ;
var right: Node? ;
//make a tree node
init(_ data: Int)
{
//assign field values
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 tree element inorder form
func count_internal_nodes(_ root: Node? ) -> Int
{
//Check whether given root node is empty or not
if (root != nil)
{
//Calculate left and right sub tree internal nodes
let result: Int = self.count_internal_nodes(root!.left) + self.count_internal_nodes(root!.right);
if (root!.left != nil || root!.right != nil)
{
//When get a new internal node
return result + 1;
}
else
{
//return the previous calculated result
return result;
}
}
else
{
//When root node is empty
return 0;
}
}
}
func main()
{
//Make object of Binary Tree
let obj: BinaryTree = BinaryTree();
/* Make A Binary Tree
-----------------------
9
/  \
7    2
/    /  \
5    6    3
\
4

*/
//Insertion of binary tree nodes
obj.root = Node(9);
obj.root!.left = Node(7);
obj.root!.right = Node(2);
obj.root!.right!.right = Node(3);
obj.root!.right!.left = Node(6);
obj.root!.right!.left!.right = Node(4);
obj.root!.left!.left = Node(5);
//Display Tree elements
let result: Int = obj.count_internal_nodes(obj.root);
print(" Total internal nodes : ", result ,"\n", terminator: "");
}
main();``````

#### Output

`` Total internal nodes :  4``

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.