Count Even paths in Binary Tree

Here given code implementation process.

// C program 
// Count even paths in 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 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;
}
// Count all paths from root to leaf which containing all Even nodes
int count_even_path(struct Node *root)
{
	if (root == NULL || root->data % 2 != 0)
	{
		//When tree node is null or its contain Odd value
		return 0;
	}
	else
	{
		if (root->left == NULL && root->right == NULL)
		{
			//When get leaf node
			return 1;
		}
		return count_even_path(root->left) + count_even_path(root->right);
	}
}
int main()
{
	struct Node *root = NULL;
	/*
	Construct Binary Tree
	-----------------------
	         2
	       /   \
	      4     10
	     / \    / \
	    7   6  12  3
	   / \    /  \  \
	  10  3  7    4  8
	*/
	//Add tree node
	root = get_node(2);
	root->left = get_node(4);
	root->right = get_node(10);
	root->right->right = get_node(3);
	root->left->right = get_node(6);
	root->right->left = get_node(12);
	root->left->left = get_node(7);
	root->left->left->left = get_node(10);
	root->left->left->right = get_node(3);
	root->right->left->right = get_node(4);
	root->right->left->left = get_node(7);
	root->right->right->right = get_node(8);
	int counter = count_even_path(root);
   	// Display calculated result
  	printf("Total Even path in this tree are : %d \n", counter);
	return 0;
}

Output

Total Even path in this tree are : 2
/* 
  Java Program 
  Count even paths in 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;
	}
}

class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	// Count all paths from root to leaf which containing all Even nodes
	public int count_even_path(Node root)
	{
		if (root == null || root.data % 2 != 0)
		{
			//When tree node is null or its contain Odd value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return count_even_path(root.left) + count_even_path(root.right);
		}
	}
	public static void main(String[] args)
	{
		//Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/* 
		Construct Binary Tree
		-----------------------
		         2
		       /   \
		      4     10
		     / \    / \
		    7   6  12  3
		   / \    /  \  \
		  10  3  7    4  8
		*/
      
		//Add tree node
		tree.root = new Node(2);
		tree.root.left = new Node(4);
		tree.root.right = new Node(10);
		tree.root.right.right = new Node(3);
		tree.root.left.right = new Node(6);
		tree.root.right.left = new Node(12);
		tree.root.left.left = new Node(7);
		tree.root.left.left.left = new Node(10);
		tree.root.left.left.right = new Node(3);
		tree.root.right.left.right = new Node(4);
		tree.root.right.left.left = new Node(7);
		tree.root.right.right.right = new Node(8);
		int counter = tree.count_even_path(tree.root);
		// Display calculated result
		System.out.print("Total Even path in this tree are : " + counter + " \n");
	}
}

Output

Total Even path in this tree are : 2
//Include header file
#include <iostream>
using namespace std;

/*
  C++ Program 
  Count even paths in 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;
	}
};

class BinaryTree
{
	public: 
    Node *root;
	BinaryTree()
	{
		//Set initial tree root to null
		this->root = NULL;
	}
	// Count all paths from root to leaf which containing all Even nodes
	int count_even_path(Node *root)
	{
		if (root == NULL || root->data % 2 != 0)
		{
			//When tree node is null or its contain Odd value
			return 0;
		}
		else
		{
			if (root->left == NULL && root->right == NULL)
			{
				//When get leaf node
				return 1;
			}
			return this->count_even_path(root->left) + this->count_even_path(root->right);
		}
	}
};

int main()
{
	//Make object of binary tree
	BinaryTree tree = BinaryTree();
	/*
			Construct Binary Tree
			-----------------------
			         2
			       /   \
			      4     10
			     / \    / \
			    7   6  12  3
			   / \    /  \  \
			  10  3  7    4  8
	*/
	//Add tree node
	tree.root = new Node(2);
	tree.root->left = new Node(4);
	tree.root->right = new Node(10);
	tree.root->right->right = new Node(3);
	tree.root->left->right = new Node(6);
	tree.root->right->left = new Node(12);
	tree.root->left->left = new Node(7);
	tree.root->left->left->left = new Node(10);
	tree.root->left->left->right = new Node(3);
	tree.root->right->left->right = new Node(4);
	tree.root->right->left->left = new Node(7);
	tree.root->right->right->right = new Node(8);
	int counter = tree.count_even_path(tree.root);
	// Display calculated result
	cout << "Total Even path in this tree are : " << counter << " \n";
	return 0;
}

Output

Total Even path in this tree are : 2
//Include namespace system
using System;

/* 
  C# Program 
  Count even paths in 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;
	}
}
class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	// Count all paths from root to leaf which containing all Even nodes
	public int count_even_path(Node root)
	{
		if (root == null || root.data % 2 != 0)
		{
			//When tree node is null or its contain Odd value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return count_even_path(root.left) + count_even_path(root.right);
		}
	}
	public static void Main(String[] args)
	{
		//Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				         2
				       /   \
				      4     10
				     / \    / \
				    7   6  12  3
				   / \    /  \  \
				  10  3  7    4  8
				*/
		//Add tree node
		tree.root = new Node(2);
		tree.root.left = new Node(4);
		tree.root.right = new Node(10);
		tree.root.right.right = new Node(3);
		tree.root.left.right = new Node(6);
		tree.root.right.left = new Node(12);
		tree.root.left.left = new Node(7);
		tree.root.left.left.left = new Node(10);
		tree.root.left.left.right = new Node(3);
		tree.root.right.left.right = new Node(4);
		tree.root.right.left.left = new Node(7);
		tree.root.right.right.right = new Node(8);
		int counter = tree.count_even_path(tree.root);
		// Display calculated result
		Console.Write("Total Even path in this tree are : " + counter + " \n");
	}
}

Output

Total Even path in this tree are : 2
<?php
/* 
  Php Program 
  Count even paths in 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;
	}
}
class BinaryTree
{
	public $root;

	function __construct()
	{
		//Set initial tree root to null
		$this->root = null;
	}
	// Count all paths from root to leaf which containing all Even nodes
	public	function count_even_path($root)
	{
		if ($root == null || $root->data % 2 != 0)
		{
			//When tree node is null or its contain Odd value
			return 0;
		}
		else
		{
			if ($root->left == null && $root->right == null)
			{
				//When get leaf node
				return 1;
			}
			return $this->count_even_path($root->left) + $this->count_even_path($root->right);
		}
	}
}

function main()
{
	//Make object of binary tree
	$tree = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			         2
			       /   \
			      4     10
			     / \    / \
			    7   6  12  3
			   / \    /  \  \
			  10  3  7    4  8
			*/
	//Add tree node
	$tree->root = new Node(2);
	$tree->root->left = new Node(4);
	$tree->root->right = new Node(10);
	$tree->root->right->right = new Node(3);
	$tree->root->left->right = new Node(6);
	$tree->root->right->left = new Node(12);
	$tree->root->left->left = new Node(7);
	$tree->root->left->left->left = new Node(10);
	$tree->root->left->left->right = new Node(3);
	$tree->root->right->left->right = new Node(4);
	$tree->root->right->left->left = new Node(7);
	$tree->root->right->right->right = new Node(8);
	$counter = $tree->count_even_path($tree->root);
	// Display calculated result
	echo "Total Even path in this tree are : ". $counter ." \n";
}
main();

Output

Total Even path in this tree are : 2
/* 
  Node Js Program 
  Count even paths in Binary Tree
*/

//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;
	}
	// Count all paths from root to leaf which containing all Even nodes
	count_even_path(root)
	{
		if (root == null || root.data % 2 != 0)
		{
			//When tree node is null or its contain Odd value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return this.count_even_path(root.left) + this.count_even_path(root.right);
		}
	}
}

function main()
{
	//Make object of binary tree
	var tree = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			         2
			       /   \
			      4     10
			     / \    / \
			    7   6  12  3
			   / \    /  \  \
			  10  3  7    4  8
			*/
	//Add tree node
	tree.root = new Node(2);
	tree.root.left = new Node(4);
	tree.root.right = new Node(10);
	tree.root.right.right = new Node(3);
	tree.root.left.right = new Node(6);
	tree.root.right.left = new Node(12);
	tree.root.left.left = new Node(7);
	tree.root.left.left.left = new Node(10);
	tree.root.left.left.right = new Node(3);
	tree.root.right.left.right = new Node(4);
	tree.root.right.left.left = new Node(7);
	tree.root.right.right.right = new Node(8);
	var counter = tree.count_even_path(tree.root);
	// Display calculated result
	process.stdout.write("Total Even path in this tree are : " + counter + " \n");
}
main();

Output

Total Even path in this tree are : 2
#   Python 3 Program 
#   Count even paths in Binary Tree

# 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
	
	#  Count all paths from root to leaf which containing all Even nodes
	def count_even_path(self, root) :
		if (root == None or root.data % 2 != 0) :
			# When tree node is null or its contain Odd value
			return 0
		else :
			if (root.left == None and root.right == None) :
				# When get leaf node
				return 1
			
			return self.count_even_path(root.left) + self.count_even_path(root.right)
		
	

def main() :
	# Make object of binary tree
	tree = BinaryTree()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		         2
	# 		       /   \
	# 		      4     10
	# 		     / \    / \
	# 		    7   6  12  3
	# 		   / \    /  \  \
	# 		  10  3  7    4  8
	# 		
	
	# Add tree node
	tree.root = Node(2)
	tree.root.left = Node(4)
	tree.root.right = Node(10)
	tree.root.right.right = Node(3)
	tree.root.left.right = Node(6)
	tree.root.right.left = Node(12)
	tree.root.left.left = Node(7)
	tree.root.left.left.left = Node(10)
	tree.root.left.left.right = Node(3)
	tree.root.right.left.right = Node(4)
	tree.root.right.left.left = Node(7)
	tree.root.right.right.right = Node(8)
	counter = tree.count_even_path(tree.root)
	#  Display calculated result
	print("Total Even path in this tree are : ", counter ," \n", end = "")

if __name__ == "__main__": main()

Output

Total Even path in this tree are :  2
#   Ruby Program 
#   Count even paths in 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

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

	#  Count all paths from root to leaf which containing all Even nodes
	def count_even_path(root) 
		if (root == nil || root.data % 2 != 0) 
			# When tree node is null or its contain Odd value
			return 0
		else 
			if (root.left == nil && root.right == nil) 
				# When get leaf node
				return 1
			end

			return self.count_even_path(root.left) + self.count_even_path(root.right)
		end

	end

end

def main() 
	# Make object of binary tree
	tree = BinaryTree.new()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		         2
	# 		       /   \
	# 		      4     10
	# 		     / \    / \
	# 		    7   6  12  3
	# 		   / \    /  \  \
	# 		  10  3  7    4  8
	# 		
	
	# Add tree node
	tree.root = Node.new(2)
	tree.root.left = Node.new(4)
	tree.root.right = Node.new(10)
	tree.root.right.right = Node.new(3)
	tree.root.left.right = Node.new(6)
	tree.root.right.left = Node.new(12)
	tree.root.left.left = Node.new(7)
	tree.root.left.left.left = Node.new(10)
	tree.root.left.left.right = Node.new(3)
	tree.root.right.left.right = Node.new(4)
	tree.root.right.left.left = Node.new(7)
	tree.root.right.right.right = Node.new(8)
	counter = tree.count_even_path(tree.root)
	#  Display calculated result
	print("Total Even path in this tree are : ", counter ," \n")
end

main()

Output

Total Even path in this tree are : 2 
/* 
  Scala Program 
  Count even paths in Binary Tree
*/

//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);
	}
	// Count all paths from root to leaf which containing all Even nodes
	def count_even_path(root: Node): Int = {
		if (root == null || root.data % 2 != 0)
		{
			//When tree node is null or its contain Odd value
			return 0;
		}
		else
		{
			if (root.left == null && root.right == null)
			{
				//When get leaf node
				return 1;
			}
			return count_even_path(root.left) + count_even_path(root.right);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		//Make object of binary tree
		var tree: BinaryTree = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				         2
				       /   \
				      4     10
				     / \    / \
				    7   6  12  3
				   / \    /  \  \
				  10  3  7    4  8
				*/
		//Add tree node
		tree.root = new Node(2);
		tree.root.left = new Node(4);
		tree.root.right = new Node(10);
		tree.root.right.right = new Node(3);
		tree.root.left.right = new Node(6);
		tree.root.right.left = new Node(12);
		tree.root.left.left = new Node(7);
		tree.root.left.left.left = new Node(10);
		tree.root.left.left.right = new Node(3);
		tree.root.right.left.right = new Node(4);
		tree.root.right.left.left = new Node(7);
		tree.root.right.right.right = new Node(8);
		var counter: Int = tree.count_even_path(tree.root);
		// Display calculated result
		print("Total Even path in this tree are : " + counter + " \n");
	}
}

Output

Total Even path in this tree are : 2
/* 
  Swift 4 Program 
  Count even paths in 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;
	}
}
class BinaryTree
{
	var root: Node? ;
	init()
	{
		//Set initial tree root to null
		self.root = nil;
	}
	// Count all paths from root to leaf which containing all Even nodes
	func count_even_path(_ root: Node? ) -> Int
	{
		if (root == nil || root!.data % 2 != 0)
		{
			//When tree node is null or its contain Odd value
			return 0;
		}
		else
		{
			if (root!.left == nil && root!.right == nil)
			{
				//When get leaf node
				return 1;
			}
			return self.count_even_path(root!.left) + self.count_even_path(root!.right);
		}
	}
}
func main()
{
	//Make object of binary tree
	let tree: BinaryTree = BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			         2
			       /   \
			      4     10
			     / \    / \
			    7   6  12  3
			   / \    /  \  \
			  10  3  7    4  8
			*/
	//Add tree node
	tree.root = Node(2);
	tree.root!.left = Node(4);
	tree.root!.right = Node(10);
	tree.root!.right!.right = Node(3);
	tree.root!.left!.right = Node(6);
	tree.root!.right!.left = Node(12);
	tree.root!.left!.left = Node(7);
	tree.root!.left!.left!.left = Node(10);
	tree.root!.left!.left!.right = Node(3);
	tree.root!.right!.left!.right = Node(4);
	tree.root!.right!.left!.left = Node(7);
	tree.root!.right!.right!.right = Node(8);
	let counter: Int = tree.count_even_path(tree.root);
	// Display calculated result
	print("Total Even path in this tree are : ", counter ," \n", terminator: "");
}
main();

Output

Total Even path in this tree are :  2


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







© 2021, kalkicode.com, All rights reserved