Find the height of stratified perfect Binary Tree

Here given code implementation process.

/*
    C Program 
    Find the height of stratified perfect 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;
}
//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);
	}
}
// Find the height of binary tree which is stratified perfect Binary tree
int height_perfect_bt(struct Node *node)
{
	if (node == NULL)
	{
		return 0;
	}
	// Recursively, Finding the min height of nodes
	int a = height_perfect_bt(node->left);
	int b = height_perfect_bt(node->right);
	if (a > b)
	{
		return b + 1;
	}
	else
	{
		return a + 1;
	}
}
int main()
{
	struct Node *root1 = NULL;
	struct Node *root2 = NULL;
	/*
	constructor binary tree
	-----------------
	    10                            
	   /   \    
	  2     3     
	 / \      \               
	8   7      1
	   /      /  \
	  6      4    5
	.................
	*/
	root1 = get_node(10);
	root1->left = get_node(2);
	root1->left->left = get_node(8);
	root1->left->right = get_node(7);
	root1->left->right->left = get_node(6);
	root1->right = get_node(3);
	root1->right->right = get_node(1);
	root1->right->right->left = get_node(4);
	root1->right->right->right = get_node(5);
	/*
	Perfect Binary Tree
	-----------------
	    10                            
	   /   \    
	  2     3     
	------------
	*/
	printf("Perfect Binary Tree Height : %d\n", height_perfect_bt(root1));
  
	/*
	constructor binary tree
	-----------------
	    20                            
	   /   \    
	  2     3     
	 / \   /  \               
	8   7 11   1
	   /      /  \
	  6      4    5
	.................
	*/
	root2 = get_node(20);
	root2->left = get_node(2);
	root2->left->left = get_node(8);
	root2->left->right = get_node(7);
	root2->left->right->left = get_node(6);
	root2->right = get_node(3);
	root2->right->left = get_node(11);
	root2->right->right = get_node(1);
	root2->right->right->left = get_node(4);
	root2->right->right->right = get_node(5);
	/*
	Perfect Binary Tree
	---------------
	    20                            
	   /   \    
	  2     3     
	 / \   /  \               
	8   7 11   1   
	------------
	*/
	printf("Perfect Binary Tree Height : %d\n", height_perfect_bt(root2));
	return 0;
}

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
/*
    Java Program 
    Find the height of stratified perfect 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;
	}
}
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);
		}
	}
	// Find the height of binary tree which is stratified perfect Binary tree
	public int height_perfect_bt(Node node)
	{
		if (node == null)
		{
			return 0;
		}
		// Recursively, Finding the min height of nodes
		int a = height_perfect_bt(node.left);
		int b = height_perfect_bt(node.right);
		if (a > b)
		{
			return b + 1;
		}
		else
		{
			return a + 1;
		}
	}
	public static void main(String[] args)
	{
		//Create tree objects
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		     constructor binary tree
		     -----------------
		            10                            
		           /   \    
		          2     3     
		         / \      \               
		        8   7      1
		           /      /  \
		          6      4    5
		     ---------------------
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.left.right = new Node(7);
		tree1.root.left.right.left = new Node(6);
		tree1.root.right = new Node(3);
		tree1.root.right.right = new Node(1);
		tree1.root.right.right.left = new Node(4);
		tree1.root.right.right.right = new Node(5);
		/*
		    Perfect Binary Tree
		    -----------------
		        10                            
		       /   \    
		      2     3     
		    ------------
		*/
		System.out.print("Perfect Binary Tree Height : " + tree1.height_perfect_bt(tree1.root) + "\n");
		/*
		    constructor binary tree
		    -----------------
		        20                            
		       /   \    
		      2     3     
		     / \   /  \               
		    8   7 11   1
		       /      /  \
		      6      4    5
		    ------------
		*/
		tree2.root = new Node(20);
		tree2.root.left = new Node(2);
		tree2.root.left.left = new Node(8);
		tree2.root.left.right = new Node(7);
		tree2.root.left.right.left = new Node(6);
		tree2.root.right = new Node(3);
		tree2.root.right.left = new Node(11);
		tree2.root.right.right = new Node(1);
		tree2.root.right.right.left = new Node(4);
		tree2.root.right.right.right = new Node(5);
		/*
 		Perfect Binary Tree
        ---------------
            20                            
           /   \    
          2     3     
         / \   /  \               
        8   7 11   1   
        ------------
        */
		System.out.print("Perfect Binary Tree Height : " + tree2.height_perfect_bt(tree2.root) + "\n");
	}
}

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
// Include header file
#include <iostream>

using namespace std;
/*
     C++ Program 
     Find the height of stratified perfect 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;
	}
	// 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);
		}
	}
	//  Find the height of binary tree which is stratified perfect Binary tree
	int height_perfect_bt(Node *node)
	{
		if (node == NULL)
		{
			return 0;
		}
		//  Recursively, Finding the min height of nodes
		int a = this->height_perfect_bt(node->left);
		int b = this->height_perfect_bt(node->right);
		if (a > b)
		{
			return b + 1;
		}
		else
		{
			return a + 1;
		}
	}
};
int main()
{
	// Create tree objects
	BinaryTree tree1 = BinaryTree();
	BinaryTree tree2 = BinaryTree();
	/*
	  		     constructor binary tree
	  		     -----------------
	  		            10                            
	  		           /   \    
	  		          2     3     
	  		         / \      \               
	  		        8   7      1
	  		           /      /  \
	  		          6      4    5
	  		     ---------------------
	*/
	tree1.root = new Node(10);
	tree1.root->left = new Node(2);
	tree1.root->left->left = new Node(8);
	tree1.root->left->right = new Node(7);
	tree1.root->left->right->left = new Node(6);
	tree1.root->right = new Node(3);
	tree1.root->right->right = new Node(1);
	tree1.root->right->right->left = new Node(4);
	tree1.root->right->right->right = new Node(5);
	/*
	  		    Perfect Binary Tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      2     3     
	  		    ------------
	*/
	cout << "Perfect Binary Tree Height : " << tree1.height_perfect_bt(tree1.root) << "\n";
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        20                            
	  		       /   \    
	  		      2     3     
	  		     / \   /  \               
	  		    8   7 11   1
	  		       /      /  \
	  		      6      4    5
	  		    ------------
	*/
	tree2.root = new Node(20);
	tree2.root->left = new Node(2);
	tree2.root->left->left = new Node(8);
	tree2.root->left->right = new Node(7);
	tree2.root->left->right->left = new Node(6);
	tree2.root->right = new Node(3);
	tree2.root->right->left = new Node(11);
	tree2.root->right->right = new Node(1);
	tree2.root->right->right->left = new Node(4);
	tree2.root->right->right->right = new Node(5);
	/*
	  		Perfect Binary Tree
	         ---------------
	             20                            
	            /   \    
	           2     3     
	          / \   /  \               
	         8   7 11   1   
	         ------------
	*/
	cout << "Perfect Binary Tree Height : " << tree2.height_perfect_bt(tree2.root) << "\n";
	return 0;
}

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
// Include namespace system
using System;
/*
     C# Program 
     Find the height of stratified perfect 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;
	}
}
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);
		}
	}
	//  Find the height of binary tree which is stratified perfect Binary tree
	public int height_perfect_bt(Node node)
	{
		if (node == null)
		{
			return 0;
		}
		//  Recursively, Finding the min height of nodes
		int a = height_perfect_bt(node.left);
		int b = height_perfect_bt(node.right);
		if (a > b)
		{
			return b + 1;
		}
		else
		{
			return a + 1;
		}
	}
	public static void Main(String[] args)
	{
		// Create tree objects
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		  		     constructor binary tree
		  		     -----------------
		  		            10                            
		  		           /   \    
		  		          2     3     
		  		         / \      \               
		  		        8   7      1
		  		           /      /  \
		  		          6      4    5
		  		     ---------------------
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.left.right = new Node(7);
		tree1.root.left.right.left = new Node(6);
		tree1.root.right = new Node(3);
		tree1.root.right.right = new Node(1);
		tree1.root.right.right.left = new Node(4);
		tree1.root.right.right.right = new Node(5);
		/*
		  		    Perfect Binary Tree
		  		    -----------------
		  		        10                            
		  		       /   \    
		  		      2     3     
		  		    ------------
		*/
		Console.Write("Perfect Binary Tree Height : " + tree1.height_perfect_bt(tree1.root) + "\n");
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        20                            
		  		       /   \    
		  		      2     3     
		  		     / \   /  \               
		  		    8   7 11   1
		  		       /      /  \
		  		      6      4    5
		  		    ------------
		*/
		tree2.root = new Node(20);
		tree2.root.left = new Node(2);
		tree2.root.left.left = new Node(8);
		tree2.root.left.right = new Node(7);
		tree2.root.left.right.left = new Node(6);
		tree2.root.right = new Node(3);
		tree2.root.right.left = new Node(11);
		tree2.root.right.right = new Node(1);
		tree2.root.right.right.left = new Node(4);
		tree2.root.right.right.right = new Node(5);
		/*
		  		Perfect Binary Tree
		         ---------------
		             20                            
		            /   \    
		           2     3     
		          / \   /  \               
		         8   7 11   1   
		         ------------
		*/
		Console.Write("Perfect Binary Tree Height : " + tree2.height_perfect_bt(tree2.root) + "\n");
	}
}

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
<?php
/*
     Php Program 
     Find the height of stratified perfect 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;
	}
	// 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);
		}
	}
	//  Find the height of binary tree which is stratified perfect Binary tree
	public	function height_perfect_bt($node)
	{
		if ($node == null)
		{
			return 0;
		}
		//  Recursively, Finding the min height of nodes
		$a = $this->height_perfect_bt($node->left);
		$b = $this->height_perfect_bt($node->right);
		if ($a > $b)
		{
			return $b + 1;
		}
		else
		{
			return $a + 1;
		}
	}
}

function main()
{
	// Create tree objects
	$tree1 = new BinaryTree();
	$tree2 = new BinaryTree();
	/*
	  		     constructor binary tree
	  		     -----------------
	  		            10                            
	  		           /   \    
	  		          2     3     
	  		         / \      \               
	  		        8   7      1
	  		           /      /  \
	  		          6      4    5
	  		     ---------------------
	*/
	$tree1->root = new Node(10);
	$tree1->root->left = new Node(2);
	$tree1->root->left->left = new Node(8);
	$tree1->root->left->right = new Node(7);
	$tree1->root->left->right->left = new Node(6);
	$tree1->root->right = new Node(3);
	$tree1->root->right->right = new Node(1);
	$tree1->root->right->right->left = new Node(4);
	$tree1->root->right->right->right = new Node(5);
	/*
	  		    Perfect Binary Tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      2     3     
	  		    ------------
	*/
	echo "Perfect Binary Tree Height : ". $tree1->height_perfect_bt($tree1->root) ."\n";
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        20                            
	  		       /   \    
	  		      2     3     
	  		     / \   /  \               
	  		    8   7 11   1
	  		       /      /  \
	  		      6      4    5
	  		    ------------
	*/
	$tree2->root = new Node(20);
	$tree2->root->left = new Node(2);
	$tree2->root->left->left = new Node(8);
	$tree2->root->left->right = new Node(7);
	$tree2->root->left->right->left = new Node(6);
	$tree2->root->right = new Node(3);
	$tree2->root->right->left = new Node(11);
	$tree2->root->right->right = new Node(1);
	$tree2->root->right->right->left = new Node(4);
	$tree2->root->right->right->right = new Node(5);
	/*
	  		Perfect Binary Tree
	         ---------------
	             20                            
	            /   \    
	           2     3     
	          / \   /  \               
	         8   7 11   1   
	         ------------
	*/
	echo "Perfect Binary Tree Height : ". $tree2->height_perfect_bt($tree2->root) ."\n";
}
main();

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
/*
     Node Js Program 
     Find the height of stratified perfect 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;
	}
	// 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);
		}
	}
	//  Find the height of binary tree which is stratified perfect Binary tree
	height_perfect_bt(node)
	{
		if (node == null)
		{
			return 0;
		}
		//  Recursively, Finding the min height of nodes
		var a = this.height_perfect_bt(node.left);
		var b = this.height_perfect_bt(node.right);
		if (a > b)
		{
			return b + 1;
		}
		else
		{
			return a + 1;
		}
	}
}

function main()
{
	// Create tree objects
	var tree1 = new BinaryTree();
	var tree2 = new BinaryTree();
	/*
	  		     constructor binary tree
	  		     -----------------
	  		            10                            
	  		           /   \    
	  		          2     3     
	  		         / \      \               
	  		        8   7      1
	  		           /      /  \
	  		          6      4    5
	  		     ---------------------
	*/
	tree1.root = new Node(10);
	tree1.root.left = new Node(2);
	tree1.root.left.left = new Node(8);
	tree1.root.left.right = new Node(7);
	tree1.root.left.right.left = new Node(6);
	tree1.root.right = new Node(3);
	tree1.root.right.right = new Node(1);
	tree1.root.right.right.left = new Node(4);
	tree1.root.right.right.right = new Node(5);
	/*
	  		    Perfect Binary Tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      2     3     
	  		    ------------
	*/
	process.stdout.write("Perfect Binary Tree Height : " + tree1.height_perfect_bt(tree1.root) + "\n");
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        20                            
	  		       /   \    
	  		      2     3     
	  		     / \   /  \               
	  		    8   7 11   1
	  		       /      /  \
	  		      6      4    5
	  		    ------------
	*/
	tree2.root = new Node(20);
	tree2.root.left = new Node(2);
	tree2.root.left.left = new Node(8);
	tree2.root.left.right = new Node(7);
	tree2.root.left.right.left = new Node(6);
	tree2.root.right = new Node(3);
	tree2.root.right.left = new Node(11);
	tree2.root.right.right = new Node(1);
	tree2.root.right.right.left = new Node(4);
	tree2.root.right.right.right = new Node(5);
	/*
	  		Perfect Binary Tree
	         ---------------
	             20                            
	            /   \    
	           2     3     
	          / \   /  \               
	         8   7 11   1   
	         ------------
	*/
	process.stdout.write("Perfect Binary Tree Height : " + tree2.height_perfect_bt(tree2.root) + "\n");
}
main();

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
#     Python 3 Program 
#     Find the height of stratified perfect 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
	
	# 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)
		
	
	#  Find the height of binary tree which is stratified perfect Binary tree
	def height_perfect_bt(self, node) :
		if (node == None) :
			return 0
		
		#  Recursively, Finding the min height of nodes
		a = self.height_perfect_bt(node.left)
		b = self.height_perfect_bt(node.right)
		if (a > b) :
			return b + 1
		else :
			return a + 1
		
	

def main() :
	# Create tree objects
	tree1 = BinaryTree()
	tree2 = BinaryTree()
	# 
	# 		     constructor binary tree
	# 		     -----------------
	# 		            10                            
	# 		           /   \    
	# 		          2     3     
	# 		         / \      \               
	# 		        8   7      1
	# 		           /      /  \
	# 		          6      4    5
	# 		     ---------------------
	# 		
	
	tree1.root = Node(10)
	tree1.root.left = Node(2)
	tree1.root.left.left = Node(8)
	tree1.root.left.right = Node(7)
	tree1.root.left.right.left = Node(6)
	tree1.root.right = Node(3)
	tree1.root.right.right = Node(1)
	tree1.root.right.right.left = Node(4)
	tree1.root.right.right.right = Node(5)
	# 
	# 		    Perfect Binary Tree
	# 		    -----------------
	# 		        10                            
	# 		       /   \    
	# 		      2     3     
	# 		    ------------
	# 		
	
	print("Perfect Binary Tree Height : ", tree1.height_perfect_bt(tree1.root) ,"\n", end = "")
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        20                            
	# 		       /   \    
	# 		      2     3     
	# 		     / \   /  \               
	# 		    8   7 11   1
	# 		       /      /  \
	# 		      6      4    5
	# 		    ------------
	# 		
	
	tree2.root = Node(20)
	tree2.root.left = Node(2)
	tree2.root.left.left = Node(8)
	tree2.root.left.right = Node(7)
	tree2.root.left.right.left = Node(6)
	tree2.root.right = Node(3)
	tree2.root.right.left = Node(11)
	tree2.root.right.right = Node(1)
	tree2.root.right.right.left = Node(4)
	tree2.root.right.right.right = Node(5)
	# 
	#  		Perfect Binary Tree
	#         ---------------
	#             20                            
	#            /   \    
	#           2     3     
	#          / \   /  \               
	#         8   7 11   1   
	#         ------------
	#         
	
	print("Perfect Binary Tree Height : ", tree2.height_perfect_bt(tree2.root) ,"\n", end = "")

if __name__ == "__main__": main()

Output

Perfect Binary Tree Height :  2
Perfect Binary Tree Height :  3
#     Ruby Program 
#     Find the height of stratified perfect 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

	# 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

	#  Find the height of binary tree which is stratified perfect Binary tree
	def height_perfect_bt(node) 
		if (node == nil) 
			return 0
		end

		#  Recursively, Finding the min height of nodes
		a = self.height_perfect_bt(node.left)
		b = self.height_perfect_bt(node.right)
		if (a > b) 
			return b + 1
		else 
			return a + 1
		end

	end

end

def main() 
	# Create tree objects
	tree1 = BinaryTree.new()
	tree2 = BinaryTree.new()
	# 
	# 		     constructor binary tree
	# 		     -----------------
	# 		            10                            
	# 		           /   \    
	# 		          2     3     
	# 		         / \      \               
	# 		        8   7      1
	# 		           /      /  \
	# 		          6      4    5
	# 		     ---------------------
	# 		
	
	tree1.root = Node.new(10)
	tree1.root.left = Node.new(2)
	tree1.root.left.left = Node.new(8)
	tree1.root.left.right = Node.new(7)
	tree1.root.left.right.left = Node.new(6)
	tree1.root.right = Node.new(3)
	tree1.root.right.right = Node.new(1)
	tree1.root.right.right.left = Node.new(4)
	tree1.root.right.right.right = Node.new(5)
	# 
	# 		    Perfect Binary Tree
	# 		    -----------------
	# 		        10                            
	# 		       /   \    
	# 		      2     3     
	# 		    ------------
	# 		
	
	print("Perfect Binary Tree Height : ", tree1.height_perfect_bt(tree1.root) ,"\n")
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        20                            
	# 		       /   \    
	# 		      2     3     
	# 		     / \   /  \               
	# 		    8   7 11   1
	# 		       /      /  \
	# 		      6      4    5
	# 		    ------------
	# 		
	
	tree2.root = Node.new(20)
	tree2.root.left = Node.new(2)
	tree2.root.left.left = Node.new(8)
	tree2.root.left.right = Node.new(7)
	tree2.root.left.right.left = Node.new(6)
	tree2.root.right = Node.new(3)
	tree2.root.right.left = Node.new(11)
	tree2.root.right.right = Node.new(1)
	tree2.root.right.right.left = Node.new(4)
	tree2.root.right.right.right = Node.new(5)
	# 
	#  		Perfect Binary Tree
	#         ---------------
	#             20                            
	#            /   \    
	#           2     3     
	#          / \   /  \               
	#         8   7 11   1   
	#         ------------
	#         
	
	print("Perfect Binary Tree Height : ", tree2.height_perfect_bt(tree2.root) ,"\n")
end

main()

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
/*
     Scala Program 
     Find the height of stratified perfect 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);
	}
	// Display pre order elements
	def preorder(node: Node): Unit = {
		if (node != null)
		{
			// Print node value
			print("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	//  Find the height of binary tree which is stratified perfect Binary tree
	def height_perfect_bt(node: Node): Int = {
		if (node == null)
		{
			return 0;
		}
		//  Recursively, Finding the min height of nodes
		var a: Int = height_perfect_bt(node.left);
		var b: Int = height_perfect_bt(node.right);
		if (a > b)
		{
			return b + 1;
		}
		else
		{
			return a + 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create tree objects
		var tree1: BinaryTree = new BinaryTree();
		var tree2: BinaryTree = new BinaryTree();
		/*
		  		     constructor binary tree
		  		     -----------------
		  		            10                            
		  		           /   \    
		  		          2     3     
		  		         / \      \               
		  		        8   7      1
		  		           /      /  \
		  		          6      4    5
		  		     ---------------------
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.left.right = new Node(7);
		tree1.root.left.right.left = new Node(6);
		tree1.root.right = new Node(3);
		tree1.root.right.right = new Node(1);
		tree1.root.right.right.left = new Node(4);
		tree1.root.right.right.right = new Node(5);
		/*
		  		    Perfect Binary Tree
		  		    -----------------
		  		        10                            
		  		       /   \    
		  		      2     3     
		  		    ------------
		*/
		print("Perfect Binary Tree Height : " + tree1.height_perfect_bt(tree1.root) + "\n");
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        20                            
		  		       /   \    
		  		      2     3     
		  		     / \   /  \               
		  		    8   7 11   1
		  		       /      /  \
		  		      6      4    5
		  		    ------------
		*/
		tree2.root = new Node(20);
		tree2.root.left = new Node(2);
		tree2.root.left.left = new Node(8);
		tree2.root.left.right = new Node(7);
		tree2.root.left.right.left = new Node(6);
		tree2.root.right = new Node(3);
		tree2.root.right.left = new Node(11);
		tree2.root.right.right = new Node(1);
		tree2.root.right.right.left = new Node(4);
		tree2.root.right.right.right = new Node(5);
		/*
		  		Perfect Binary Tree
		         ---------------
		             20                            
		            /   \    
		           2     3     
		          / \   /  \               
		         8   7 11   1   
		         ------------
		*/
		print("Perfect Binary Tree Height : " + tree2.height_perfect_bt(tree2.root) + "\n");
	}
}

Output

Perfect Binary Tree Height : 2
Perfect Binary Tree Height : 3
/*
     Swift 4 Program 
     Find the height of stratified perfect 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;
	}
	// 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);
		}
	}
	//  Find the height of binary tree which is stratified perfect Binary tree
	func height_perfect_bt(_ node: Node? )->Int
	{
		if (node == nil)
		{
			return 0;
		}
		//  Recursively, Finding the min height of nodes
		let a: Int = self.height_perfect_bt(node!.left);
		let b: Int = self.height_perfect_bt(node!.right);
		if (a > b)
		{
			return b + 1;
		}
		else
		{
			return a + 1;
		}
	}
}
func main()
{
	// Create tree objects
	let tree1: BinaryTree = BinaryTree();
	let tree2: BinaryTree = BinaryTree();
	/*
  		     constructor binary tree
  		     -----------------
  		            10                            
  		           /   \    
  		          2     3     
  		         / \      \               
  		        8   7      1
  		           /      /  \
  		          6      4    5
  		     ---------------------
*/
	tree1.root = Node(10);
	tree1.root!.left = Node(2);
	tree1.root!.left!.left = Node(8);
	tree1.root!.left!.right = Node(7);
	tree1.root!.left!.right!.left = Node(6);
	tree1.root!.right = Node(3);
	tree1.root!.right!.right = Node(1);
	tree1.root!.right!.right!.left = Node(4);
	tree1.root!.right!.right!.right = Node(5);
	/*
  		    Perfect Binary Tree
  		    -----------------
  		        10                            
  		       /   \    
  		      2     3     
  		    ------------
*/
	print("Perfect Binary Tree Height : ", tree1.height_perfect_bt(tree1.root) ,"\n", terminator: "");
	/*
  		    constructor binary tree
  		    -----------------
  		        20                            
  		       /   \    
  		      2     3     
  		     / \   /  \               
  		    8   7 11   1
  		       /      /  \
  		      6      4    5
  		    ------------
*/
	tree2.root = Node(20);
	tree2.root!.left = Node(2);
	tree2.root!.left!.left = Node(8);
	tree2.root!.left!.right = Node(7);
	tree2.root!.left!.right!.left = Node(6);
	tree2.root!.right = Node(3);
	tree2.root!.right!.left = Node(11);
	tree2.root!.right!.right = Node(1);
	tree2.root!.right!.right!.left = Node(4);
	tree2.root!.right!.right!.right = Node(5);
	/*
  		Perfect Binary Tree
         ---------------
             20                            
            /   \    
           2     3     
          / \   /  \               
         8   7 11   1   
         ------------
*/
	print("Perfect Binary Tree Height : ", tree2.height_perfect_bt(tree2.root) ,"\n", terminator: "");
}
main();

Output

Perfect Binary Tree Height :  2
Perfect Binary Tree Height :  3

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