Print middle level of perfect binary tree

Here given code implementation process.

/*
    C Program 
    Print middle level of perfect binary tree
*/
#include <stdio.h>
#include <stdlib.h>

//Binary Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
// Returns a new node of binary tree
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);
	}
}
//Print all middle level nodes in given perfect binary tree
void print_middle_level(struct Node *first, struct Node *second)
{
	if (first == NULL || second == NULL)
	{
		return;
	}
	if (second->left == NULL && second->right == NULL)
	{
		//When get middle level node
		printf(" %d ", first->data);
		return;
	}
	// Recursively, executing process
	print_middle_level(first->left, second->left->left);
	print_middle_level(first->right, second->right->right);
}
int main()
{
	struct Node *root = NULL;
	/*
    constructor binary tree
    -----------------------

                             6
                           /  \
                          /    \
                         /      \
                        /        \ 
                       /          \  
                      /            \   
                     /              \
                    /                \ 
                   /                  \ 
                  /                    \ 
                 /                      \                           
                /                        \    
               7                          3  
              /  \                        / \
             /    \                      /   \
            /      \                    /     \ 
           /        \                  /       \ 
          /          \                /         \
         /            \              /           \               
        3              8             2             1
       /  \           / \           /\            /  \ 
      /    \         /   \         /  \          /    \  
     /      \       /     \       /    \        /      \
    1       2      1       1     8      9      4        5
   / \     / \    / \     / \   / \    /  \   / \      /  \   
  3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
                     
    -----------------------
    */
	root = get_node(6);
	root->left = get_node(7);
	root->left->left = get_node(3);
	root->left->left->left = get_node(1);
	root->left->left->left->left = get_node(3);
	root->left->left->left->right = get_node(4);
	root->left->left->right = get_node(2);
	root->left->left->right->left = get_node(5);
	root->left->left->right->right = get_node(6);
	root->left->right = get_node(8);
	root->left->right->right = get_node(1);
	root->left->right->left = get_node(1);
	root->left->right->left->left = get_node(7);
	root->left->right->left->right = get_node(8);
	root->left->right->right->left = get_node(9);
	root->left->right->right->right = get_node(1);
	root->right = get_node(3);
	root->right->left = get_node(2);
	root->right->left->left = get_node(8);
	root->right->left->left->left = get_node(1);
	root->right->left->left->right = get_node(2);
	root->right->left->right = get_node(9);
	root->right->left->right->left = get_node(3);
	root->right->left->right->right = get_node(4);
	root->right->right = get_node(1);
	root->right->right->right = get_node(5);
	root->right->right->right->left = get_node(6);
	root->right->right->right->right = get_node(7);
	root->right->right->left = get_node(4);
	root->right->right->left->left = get_node(5);
	root->right->right->left->right = get_node(6);
	//Display Tree Element
	printf("\n Tree Nodes : ");
	preorder(root);
	printf("\n Middle Level : ");
	print_middle_level(root, root);
	return 0;
}

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :  3  8  2  1
/*
    Java Program 
    Print middle level of 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);
		}
	}
	//Print all middle level nodes in given perfect binary tree
	public void print_middle_level(Node first, Node second)
	{
		if (first == null || second == null)
		{
			return;
		}
		if (second.left == null && second.right == null)
		{
			//When get middle level node
			System.out.print("  " + first.data);
			return;
		}
		// Recursively, executing process
		print_middle_level(first.left, second.left.left);
		print_middle_level(first.right, second.right.right);
	}
	public static void main(String[] args)
	{
		//Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/*
		constructor binary tree
		-----------------------

		                           6
		                         /  \
		                        /    \
		                       /      \
		                      /        \ 
		                     /          \  
		                    /            \   
		                   /              \
		                  /                \ 
		                 /                  \ 
		                /                    \ 
		               /                      \                           
		              /                        \    
		             7                          3  
		            /  \                        / \
		           /    \                      /   \
		          /      \                    /     \ 
		         /        \                  /       \ 
		        /          \                /         \
		       /            \              /           \               
		      3              8             2             1
		     /  \           / \           / \           /  \ 
		    /    \         /   \         /   \         /    \  
		   /      \       /     \       /     \       /      \
		  1       2      1       1     8      9      4        5
		 / \     / \    / \     / \   / \    /  \   / \      /  \   
		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7

		-----------------------
		*/
		tree.root = new Node(6);
		tree.root.left = new Node(7);
		tree.root.left.left = new Node(3);
		tree.root.left.left.left = new Node(1);
		tree.root.left.left.left.left = new Node(3);
		tree.root.left.left.left.right = new Node(4);
		tree.root.left.left.right = new Node(2);
		tree.root.left.left.right.left = new Node(5);
		tree.root.left.left.right.right = new Node(6);
		tree.root.left.right = new Node(8);
		tree.root.left.right.right = new Node(1);
		tree.root.left.right.left = new Node(1);
		tree.root.left.right.left.left = new Node(7);
		tree.root.left.right.left.right = new Node(8);
		tree.root.left.right.right.left = new Node(9);
		tree.root.left.right.right.right = new Node(1);
		tree.root.right = new Node(3);
		tree.root.right.left = new Node(2);
		tree.root.right.left.left = new Node(8);
		tree.root.right.left.left.left = new Node(1);
		tree.root.right.left.left.right = new Node(2);
		tree.root.right.left.right = new Node(9);
		tree.root.right.left.right.left = new Node(3);
		tree.root.right.left.right.right = new Node(4);
		tree.root.right.right = new Node(1);
		tree.root.right.right.right = new Node(5);
		tree.root.right.right.right.left = new Node(6);
		tree.root.right.right.right.right = new Node(7);
		tree.root.right.right.left = new Node(4);
		tree.root.right.right.left.left = new Node(5);
		tree.root.right.right.left.right = new Node(6);
		//Display Tree Element
		System.out.print("\n Tree Nodes : ");
		tree.preorder(tree.root);
		System.out.print("\n Middle Level : ");
		tree.print_middle_level(tree.root, tree.root);
	}
}

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :   3  8  2  1
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program 
  Print middle level of 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);
		}
	}
	// Print all middle level nodes in given perfect binary tree
	void print_middle_level(Node *first, Node *second)
	{
		if (first == NULL || second == NULL)
		{
			return;
		}
		if (second->left == NULL && second->right == NULL)
		{
			// When get middle level node
			cout << "  " << first->data;
			return;
		}
		//  Recursively, executing process
		this->print_middle_level(first->left, second->left->left);
		this->print_middle_level(first->right, second->right->right);
	}
};
int main()
{
	// Make object of binary tree
	BinaryTree tree = BinaryTree();
	/*
	  		constructor binary tree
	  		-----------------------
	  		                           6
	  		                         /  \
	  		                        /    \
	  		                       /      \
	  		                      /        \ 
	  		                     /          \  
	  		                    /            \   
	  		                   /              \
	  		                  /                \ 
	  		                 /                  \ 
	  		                /                    \ 
	  		               /                      \                           
	  		              /                        \    
	  		             7                          3  
	  		            /  \                        / \
	  		           /    \                      /   \
	  		          /      \                    /     \ 
	  		         /        \                  /       \ 
	  		        /          \                /         \
	  		       /            \              /           \               
	  		      3              8             2             1
	  		     /  \           / \           / \           /  \ 
	  		    /    \         /   \         /   \         /    \  
	  		   /      \       /     \       /     \       /      \
	  		  1       2      1       1     8      9      4        5
	  		 / \     / \    / \     / \   / \    /  \   / \      /  \   
	  		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
	  		-----------------------
	*/
	tree.root = new Node(6);
	tree.root->left = new Node(7);
	tree.root->left->left = new Node(3);
	tree.root->left->left->left = new Node(1);
	tree.root->left->left->left->left = new Node(3);
	tree.root->left->left->left->right = new Node(4);
	tree.root->left->left->right = new Node(2);
	tree.root->left->left->right->left = new Node(5);
	tree.root->left->left->right->right = new Node(6);
	tree.root->left->right = new Node(8);
	tree.root->left->right->right = new Node(1);
	tree.root->left->right->left = new Node(1);
	tree.root->left->right->left->left = new Node(7);
	tree.root->left->right->left->right = new Node(8);
	tree.root->left->right->right->left = new Node(9);
	tree.root->left->right->right->right = new Node(1);
	tree.root->right = new Node(3);
	tree.root->right->left = new Node(2);
	tree.root->right->left->left = new Node(8);
	tree.root->right->left->left->left = new Node(1);
	tree.root->right->left->left->right = new Node(2);
	tree.root->right->left->right = new Node(9);
	tree.root->right->left->right->left = new Node(3);
	tree.root->right->left->right->right = new Node(4);
	tree.root->right->right = new Node(1);
	tree.root->right->right->right = new Node(5);
	tree.root->right->right->right->left = new Node(6);
	tree.root->right->right->right->right = new Node(7);
	tree.root->right->right->left = new Node(4);
	tree.root->right->right->left->left = new Node(5);
	tree.root->right->right->left->right = new Node(6);
	// Display Tree Element
	cout << "\n Tree Nodes : ";
	tree.preorder(tree.root);
	cout << "\n Middle Level : ";
	tree.print_middle_level(tree.root, tree.root);
	return 0;
}

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :   3  8  2  1
// Include namespace system
using System;
/*
  C# Program 
  Print middle level of 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);
		}
	}
	// Print all middle level nodes in given perfect binary tree
	public void print_middle_level(Node first, Node second)
	{
		if (first == null || second == null)
		{
			return;
		}
		if (second.left == null && second.right == null)
		{
			// When get middle level node
			Console.Write("  " + first.data);
			return;
		}
		//  Recursively, executing process
		print_middle_level(first.left, second.left.left);
		print_middle_level(first.right, second.right.right);
	}
	public static void Main(String[] args)
	{
		// Make object of binary tree
		BinaryTree tree = new BinaryTree();
		/*
		  		constructor binary tree
		  		-----------------------
		  		                           6
		  		                         /  \
		  		                        /    \
		  		                       /      \
		  		                      /        \ 
		  		                     /          \  
		  		                    /            \   
		  		                   /              \
		  		                  /                \ 
		  		                 /                  \ 
		  		                /                    \ 
		  		               /                      \                           
		  		              /                        \    
		  		             7                          3  
		  		            /  \                        / \
		  		           /    \                      /   \
		  		          /      \                    /     \ 
		  		         /        \                  /       \ 
		  		        /          \                /         \
		  		       /            \              /           \               
		  		      3              8             2             1
		  		     /  \           / \           / \           /  \ 
		  		    /    \         /   \         /   \         /    \  
		  		   /      \       /     \       /     \       /      \
		  		  1       2      1       1     8      9      4        5
		  		 / \     / \    / \     / \   / \    /  \   / \      /  \   
		  		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
		  		-----------------------
		*/
		tree.root = new Node(6);
		tree.root.left = new Node(7);
		tree.root.left.left = new Node(3);
		tree.root.left.left.left = new Node(1);
		tree.root.left.left.left.left = new Node(3);
		tree.root.left.left.left.right = new Node(4);
		tree.root.left.left.right = new Node(2);
		tree.root.left.left.right.left = new Node(5);
		tree.root.left.left.right.right = new Node(6);
		tree.root.left.right = new Node(8);
		tree.root.left.right.right = new Node(1);
		tree.root.left.right.left = new Node(1);
		tree.root.left.right.left.left = new Node(7);
		tree.root.left.right.left.right = new Node(8);
		tree.root.left.right.right.left = new Node(9);
		tree.root.left.right.right.right = new Node(1);
		tree.root.right = new Node(3);
		tree.root.right.left = new Node(2);
		tree.root.right.left.left = new Node(8);
		tree.root.right.left.left.left = new Node(1);
		tree.root.right.left.left.right = new Node(2);
		tree.root.right.left.right = new Node(9);
		tree.root.right.left.right.left = new Node(3);
		tree.root.right.left.right.right = new Node(4);
		tree.root.right.right = new Node(1);
		tree.root.right.right.right = new Node(5);
		tree.root.right.right.right.left = new Node(6);
		tree.root.right.right.right.right = new Node(7);
		tree.root.right.right.left = new Node(4);
		tree.root.right.right.left.left = new Node(5);
		tree.root.right.right.left.right = new Node(6);
		// Display Tree Element
		Console.Write("\n Tree Nodes : ");
		tree.preorder(tree.root);
		Console.Write("\n Middle Level : ");
		tree.print_middle_level(tree.root, tree.root);
	}
}

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :   3  8  2  1
<?php
/*
  Php Program 
  Print middle level of 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);
		}
	}
	// Print all middle level nodes in given perfect binary tree
	public	function print_middle_level($first, $second)
	{
		if ($first == null || $second == null)
		{
			return;
		}
		if ($second->left == null && $second->right == null)
		{
			// When get middle level node
			echo "  ". $first->data;
			return;
		}
		//  Recursively, executing process
		$this->print_middle_level($first->left, $second->left->left);
		$this->print_middle_level($first->right, $second->right->right);
	}
}

function main()
{
	// Make object of binary tree
	$tree = new BinaryTree();
	/*
	  		constructor binary tree
	  		-----------------------
	  		                           6
	  		                         /  \
	  		                        /    \
	  		                       /      \
	  		                      /        \ 
	  		                     /          \  
	  		                    /            \   
	  		                   /              \
	  		                  /                \ 
	  		                 /                  \ 
	  		                /                    \ 
	  		               /                      \                           
	  		              /                        \    
	  		             7                          3  
	  		            /  \                        / \
	  		           /    \                      /   \
	  		          /      \                    /     \ 
	  		         /        \                  /       \ 
	  		        /          \                /         \
	  		       /            \              /           \               
	  		      3              8             2             1
	  		     /  \           / \           / \           /  \ 
	  		    /    \         /   \         /   \         /    \  
	  		   /      \       /     \       /     \       /      \
	  		  1       2      1       1     8      9      4        5
	  		 / \     / \    / \     / \   / \    /  \   / \      /  \   
	  		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
	  		-----------------------
	*/
	$tree->root = new Node(6);
	$tree->root->left = new Node(7);
	$tree->root->left->left = new Node(3);
	$tree->root->left->left->left = new Node(1);
	$tree->root->left->left->left->left = new Node(3);
	$tree->root->left->left->left->right = new Node(4);
	$tree->root->left->left->right = new Node(2);
	$tree->root->left->left->right->left = new Node(5);
	$tree->root->left->left->right->right = new Node(6);
	$tree->root->left->right = new Node(8);
	$tree->root->left->right->right = new Node(1);
	$tree->root->left->right->left = new Node(1);
	$tree->root->left->right->left->left = new Node(7);
	$tree->root->left->right->left->right = new Node(8);
	$tree->root->left->right->right->left = new Node(9);
	$tree->root->left->right->right->right = new Node(1);
	$tree->root->right = new Node(3);
	$tree->root->right->left = new Node(2);
	$tree->root->right->left->left = new Node(8);
	$tree->root->right->left->left->left = new Node(1);
	$tree->root->right->left->left->right = new Node(2);
	$tree->root->right->left->right = new Node(9);
	$tree->root->right->left->right->left = new Node(3);
	$tree->root->right->left->right->right = new Node(4);
	$tree->root->right->right = new Node(1);
	$tree->root->right->right->right = new Node(5);
	$tree->root->right->right->right->left = new Node(6);
	$tree->root->right->right->right->right = new Node(7);
	$tree->root->right->right->left = new Node(4);
	$tree->root->right->right->left->left = new Node(5);
	$tree->root->right->right->left->right = new Node(6);
	// Display Tree Element
	echo "\n Tree Nodes : ";
	$tree->preorder($tree->root);
	echo "\n Middle Level : ";
	$tree->print_middle_level($tree->root, $tree->root);
}
main();

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :   3  8  2  1
/*
  Node Js Program 
  Print middle level of 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);
		}
	}
	// Print all middle level nodes in given perfect binary tree
	print_middle_level(first, second)
	{
		if (first == null || second == null)
		{
			return;
		}
		if (second.left == null && second.right == null)
		{
			// When get middle level node
			process.stdout.write("  " + first.data);
			return;
		}
		//  Recursively, executing process
		this.print_middle_level(first.left, second.left.left);
		this.print_middle_level(first.right, second.right.right);
	}
}

function main()
{
	// Make object of binary tree
	var tree = new BinaryTree();
	/*
	  		constructor binary tree
	  		-----------------------
	  		                           6
	  		                         /  \
	  		                        /    \
	  		                       /      \
	  		                      /        \ 
	  		                     /          \  
	  		                    /            \   
	  		                   /              \
	  		                  /                \ 
	  		                 /                  \ 
	  		                /                    \ 
	  		               /                      \                           
	  		              /                        \    
	  		             7                          3  
	  		            /  \                        / \
	  		           /    \                      /   \
	  		          /      \                    /     \ 
	  		         /        \                  /       \ 
	  		        /          \                /         \
	  		       /            \              /           \               
	  		      3              8             2             1
	  		     /  \           / \           / \           /  \ 
	  		    /    \         /   \         /   \         /    \  
	  		   /      \       /     \       /     \       /      \
	  		  1       2      1       1     8      9      4        5
	  		 / \     / \    / \     / \   / \    /  \   / \      /  \   
	  		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
	  		-----------------------
	*/
	tree.root = new Node(6);
	tree.root.left = new Node(7);
	tree.root.left.left = new Node(3);
	tree.root.left.left.left = new Node(1);
	tree.root.left.left.left.left = new Node(3);
	tree.root.left.left.left.right = new Node(4);
	tree.root.left.left.right = new Node(2);
	tree.root.left.left.right.left = new Node(5);
	tree.root.left.left.right.right = new Node(6);
	tree.root.left.right = new Node(8);
	tree.root.left.right.right = new Node(1);
	tree.root.left.right.left = new Node(1);
	tree.root.left.right.left.left = new Node(7);
	tree.root.left.right.left.right = new Node(8);
	tree.root.left.right.right.left = new Node(9);
	tree.root.left.right.right.right = new Node(1);
	tree.root.right = new Node(3);
	tree.root.right.left = new Node(2);
	tree.root.right.left.left = new Node(8);
	tree.root.right.left.left.left = new Node(1);
	tree.root.right.left.left.right = new Node(2);
	tree.root.right.left.right = new Node(9);
	tree.root.right.left.right.left = new Node(3);
	tree.root.right.left.right.right = new Node(4);
	tree.root.right.right = new Node(1);
	tree.root.right.right.right = new Node(5);
	tree.root.right.right.right.left = new Node(6);
	tree.root.right.right.right.right = new Node(7);
	tree.root.right.right.left = new Node(4);
	tree.root.right.right.left.left = new Node(5);
	tree.root.right.right.left.right = new Node(6);
	// Display Tree Element
	process.stdout.write("\n Tree Nodes : ");
	tree.preorder(tree.root);
	process.stdout.write("\n Middle Level : ");
	tree.print_middle_level(tree.root, tree.root);
}
main();

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :   3  8  2  1
#     Python 3 Program 
#     Print middle level of 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)
		
	
	# Print all middle level nodes in given perfect binary tree
	def print_middle_level(self, first, second) :
		if (first == None or second == None) :
			return
		
		if (second.left == None and second.right == None) :
			# When get middle level node
			print("  ", first.data, end = "")
			return
		
		#  Recursively, executing process
		self.print_middle_level(first.left, second.left.left)
		self.print_middle_level(first.right, second.right.right)
	

def main() :
	# Make object of binary tree
	tree = BinaryTree()
	# 
	# 		constructor binary tree
	# 		-----------------------
	# 		                           6
	# 		                         /  \
	# 		                        /    \
	# 		                       /      \
	# 		                      /        \ 
	# 		                     /          \  
	# 		                    /            \   
	# 		                   /              \
	# 		                  /                \ 
	# 		                 /                  \ 
	# 		                /                    \ 
	# 		               /                      \                           
	# 		              /                        \    
	# 		             7                          3  
	# 		            /  \                        / \
	# 		           /    \                      /   \
	# 		          /      \                    /     \ 
	# 		         /        \                  /       \ 
	# 		        /          \                /         \
	# 		       /            \              /           \               
	# 		      3              8             2             1
	# 		     /  \           / \           / \           /  \ 
	# 		    /    \         /   \         /   \         /    \  
	# 		   /      \       /     \       /     \       /      \
	# 		  1       2      1       1     8      9      4        5
	# 		 / \     / \    / \     / \   / \    /  \   / \      /  \   
	# 		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
	# 		-----------------------
	# 		
	
	tree.root = Node(6)
	tree.root.left = Node(7)
	tree.root.left.left = Node(3)
	tree.root.left.left.left = Node(1)
	tree.root.left.left.left.left = Node(3)
	tree.root.left.left.left.right = Node(4)
	tree.root.left.left.right = Node(2)
	tree.root.left.left.right.left = Node(5)
	tree.root.left.left.right.right = Node(6)
	tree.root.left.right = Node(8)
	tree.root.left.right.right = Node(1)
	tree.root.left.right.left = Node(1)
	tree.root.left.right.left.left = Node(7)
	tree.root.left.right.left.right = Node(8)
	tree.root.left.right.right.left = Node(9)
	tree.root.left.right.right.right = Node(1)
	tree.root.right = Node(3)
	tree.root.right.left = Node(2)
	tree.root.right.left.left = Node(8)
	tree.root.right.left.left.left = Node(1)
	tree.root.right.left.left.right = Node(2)
	tree.root.right.left.right = Node(9)
	tree.root.right.left.right.left = Node(3)
	tree.root.right.left.right.right = Node(4)
	tree.root.right.right = Node(1)
	tree.root.right.right.right = Node(5)
	tree.root.right.right.right.left = Node(6)
	tree.root.right.right.right.right = Node(7)
	tree.root.right.right.left = Node(4)
	tree.root.right.right.left.left = Node(5)
	tree.root.right.right.left.right = Node(6)
	# Display Tree Element
	print("\n Tree Nodes : ", end = "")
	tree.preorder(tree.root)
	print("\n Middle Level : ", end = "")
	tree.print_middle_level(tree.root, tree.root)

if __name__ == "__main__": main()

Output

 Tree Nodes :    6   7   3   1   3   4   2   5   6   8   1   7   8   1   9   1   3   2   8   1   2   9   3   4   1   4   5   6   5   6   7
 Middle Level :    3   8   2   1
#     Ruby Program 
#     Print middle level of 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

	# Print all middle level nodes in given perfect binary tree
	def print_middle_level(first, second) 
		if (first == nil || second == nil) 
			return
		end

		if (second.left == nil && second.right == nil) 
			# When get middle level node
			print("  ", first.data)
			return
		end

		#  Recursively, executing process
		self.print_middle_level(first.left, second.left.left)
		self.print_middle_level(first.right, second.right.right)
	end

end

def main() 
	# Make object of binary tree
	tree = BinaryTree.new()
	# 
	# 		constructor binary tree
	# 		-----------------------
	# 		                           6
	# 		                         /  \
	# 		                        /    \
	# 		                       /      \
	# 		                      /        \ 
	# 		                     /          \  
	# 		                    /            \   
	# 		                   /              \
	# 		                  /                \ 
	# 		                 /                  \ 
	# 		                /                    \ 
	# 		               /                      \                           
	# 		              /                        \    
	# 		             7                          3  
	# 		            /  \                        / \
	# 		           /    \                      /   \
	# 		          /      \                    /     \ 
	# 		         /        \                  /       \ 
	# 		        /          \                /         \
	# 		       /            \              /           \               
	# 		      3              8             2             1
	# 		     /  \           / \           / \           /  \ 
	# 		    /    \         /   \         /   \         /    \  
	# 		   /      \       /     \       /     \       /      \
	# 		  1       2      1       1     8      9      4        5
	# 		 / \     / \    / \     / \   / \    /  \   / \      /  \   
	# 		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
	# 		-----------------------
	# 		
	
	tree.root = Node.new(6)
	tree.root.left = Node.new(7)
	tree.root.left.left = Node.new(3)
	tree.root.left.left.left = Node.new(1)
	tree.root.left.left.left.left = Node.new(3)
	tree.root.left.left.left.right = Node.new(4)
	tree.root.left.left.right = Node.new(2)
	tree.root.left.left.right.left = Node.new(5)
	tree.root.left.left.right.right = Node.new(6)
	tree.root.left.right = Node.new(8)
	tree.root.left.right.right = Node.new(1)
	tree.root.left.right.left = Node.new(1)
	tree.root.left.right.left.left = Node.new(7)
	tree.root.left.right.left.right = Node.new(8)
	tree.root.left.right.right.left = Node.new(9)
	tree.root.left.right.right.right = Node.new(1)
	tree.root.right = Node.new(3)
	tree.root.right.left = Node.new(2)
	tree.root.right.left.left = Node.new(8)
	tree.root.right.left.left.left = Node.new(1)
	tree.root.right.left.left.right = Node.new(2)
	tree.root.right.left.right = Node.new(9)
	tree.root.right.left.right.left = Node.new(3)
	tree.root.right.left.right.right = Node.new(4)
	tree.root.right.right = Node.new(1)
	tree.root.right.right.right = Node.new(5)
	tree.root.right.right.right.left = Node.new(6)
	tree.root.right.right.right.right = Node.new(7)
	tree.root.right.right.left = Node.new(4)
	tree.root.right.right.left.left = Node.new(5)
	tree.root.right.right.left.right = Node.new(6)
	# Display Tree Element
	print("\n Tree Nodes : ")
	tree.preorder(tree.root)
	print("\n Middle Level : ")
	tree.print_middle_level(tree.root, tree.root)
end

main()

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :   3  8  2  1
/*
  Scala Program 
  Print middle level of 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);
		}
	}
	// Print all middle level nodes in given perfect binary tree
	def print_middle_level(first: Node, second: Node): Unit = {
		if (first == null || second == null)
		{
			return;
		}
		if (second.left == null && second.right == null)
		{
			// When get middle level node
			print("  " + first.data);
			return;
		}
		//  Recursively, executing process
		print_middle_level(first.left, second.left.left);
		print_middle_level(first.right, second.right.right);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Make object of binary tree
		var tree: BinaryTree = new BinaryTree();
		/*
		  		constructor binary tree
		  		-----------------------
		  		                           6
		  		                         /  \
		  		                        /    \
		  		                       /      \
		  		                      /        \ 
		  		                     /          \  
		  		                    /            \   
		  		                   /              \
		  		                  /                \ 
		  		                 /                  \ 
		  		                /                    \ 
		  		               /                      \                           
		  		              /                        \    
		  		             7                          3  
		  		            /  \                        / \
		  		           /    \                      /   \
		  		          /      \                    /     \ 
		  		         /        \                  /       \ 
		  		        /          \                /         \
		  		       /            \              /           \               
		  		      3              8             2             1
		  		     /  \           / \           / \           /  \ 
		  		    /    \         /   \         /   \         /    \  
		  		   /      \       /     \       /     \       /      \
		  		  1       2      1       1     8      9      4        5
		  		 / \     / \    / \     / \   / \    /  \   / \      /  \   
		  		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
		  		-----------------------
		*/
		tree.root = new Node(6);
		tree.root.left = new Node(7);
		tree.root.left.left = new Node(3);
		tree.root.left.left.left = new Node(1);
		tree.root.left.left.left.left = new Node(3);
		tree.root.left.left.left.right = new Node(4);
		tree.root.left.left.right = new Node(2);
		tree.root.left.left.right.left = new Node(5);
		tree.root.left.left.right.right = new Node(6);
		tree.root.left.right = new Node(8);
		tree.root.left.right.right = new Node(1);
		tree.root.left.right.left = new Node(1);
		tree.root.left.right.left.left = new Node(7);
		tree.root.left.right.left.right = new Node(8);
		tree.root.left.right.right.left = new Node(9);
		tree.root.left.right.right.right = new Node(1);
		tree.root.right = new Node(3);
		tree.root.right.left = new Node(2);
		tree.root.right.left.left = new Node(8);
		tree.root.right.left.left.left = new Node(1);
		tree.root.right.left.left.right = new Node(2);
		tree.root.right.left.right = new Node(9);
		tree.root.right.left.right.left = new Node(3);
		tree.root.right.left.right.right = new Node(4);
		tree.root.right.right = new Node(1);
		tree.root.right.right.right = new Node(5);
		tree.root.right.right.right.left = new Node(6);
		tree.root.right.right.right.right = new Node(7);
		tree.root.right.right.left = new Node(4);
		tree.root.right.right.left.left = new Node(5);
		tree.root.right.right.left.right = new Node(6);
		// Display Tree Element
		print("\n Tree Nodes : ");
		tree.preorder(tree.root);
		print("\n Middle Level : ");
		tree.print_middle_level(tree.root, tree.root);
	}
}

Output

 Tree Nodes :   6  7  3  1  3  4  2  5  6  8  1  7  8  1  9  1  3  2  8  1  2  9  3  4  1  4  5  6  5  6  7
 Middle Level :   3  8  2  1
/*
  Swift 4 Program 
  Print middle level of 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);
		}
	}
	// Print all middle level nodes in given perfect binary tree
	func print_middle_level(_ first: Node? , _ second : Node? )
	{
		if (first == nil || second == nil)
		{
			return;
		}
		if (second!.left == nil && second!.right == nil)
		{
			// When get middle level node
			print("  ", first!.data, terminator: "");
			return;
		}
		//  Recursively, executing process
		self.print_middle_level(first!.left, second!.left!.left);
		self.print_middle_level(first!.right, second!.right!.right);
	}
}
func main()
{
	// Make object of binary tree
	let tree: BinaryTree = BinaryTree();
	/*
  		constructor binary tree
  		-----------------------
  		                           6
  		                         /  \
  		                        /    \
  		                       /      \
  		                      /        \ 
  		                     /          \  
  		                    /            \   
  		                   /              \
  		                  /                \ 
  		                 /                  \ 
  		                /                    \ 
  		               /                      \                           
  		              /                        \    
  		             7                          3  
  		            /  \                        / \
  		           /    \                      /   \
  		          /      \                    /     \ 
  		         /        \                  /       \ 
  		        /          \                /         \
  		       /            \              /           \               
  		      3              8             2             1
  		     /  \           / \           / \           /  \ 
  		    /    \         /   \         /   \         /    \  
  		   /      \       /     \       /     \       /      \
  		  1       2      1       1     8      9      4        5
  		 / \     / \    / \     / \   / \    /  \   / \      /  \   
  		3   4   5   6  7   8   9   1 1   2  3    4 5   6    6    7
  		-----------------------
*/
	tree.root = Node(6);
	tree.root!.left = Node(7);
	tree.root!.left!.left = Node(3);
	tree.root!.left!.left!.left = Node(1);
	tree.root!.left!.left!.left!.left = Node(3);
	tree.root!.left!.left!.left!.right = Node(4);
	tree.root!.left!.left!.right = Node(2);
	tree.root!.left!.left!.right!.left = Node(5);
	tree.root!.left!.left!.right!.right = Node(6);
	tree.root!.left!.right = Node(8);
	tree.root!.left!.right!.right = Node(1);
	tree.root!.left!.right!.left = Node(1);
	tree.root!.left!.right!.left!.left = Node(7);
	tree.root!.left!.right!.left!.right = Node(8);
	tree.root!.left!.right!.right!.left = Node(9);
	tree.root!.left!.right!.right!.right = Node(1);
	tree.root!.right = Node(3);
	tree.root!.right!.left = Node(2);
	tree.root!.right!.left!.left = Node(8);
	tree.root!.right!.left!.left!.left = Node(1);
	tree.root!.right!.left!.left!.right = Node(2);
	tree.root!.right!.left!.right = Node(9);
	tree.root!.right!.left!.right!.left = Node(3);
	tree.root!.right!.left!.right!.right = Node(4);
	tree.root!.right!.right = Node(1);
	tree.root!.right!.right!.right = Node(5);
	tree.root!.right!.right!.right!.left = Node(6);
	tree.root!.right!.right!.right!.right = Node(7);
	tree.root!.right!.right!.left = Node(4);
	tree.root!.right!.right!.left!.left = Node(5);
	tree.root!.right!.right!.left!.right = Node(6);
	// Display Tree Element
	print("\n Tree Nodes : ", terminator: "");
	tree.preorder(tree.root);
	print("\n Middle Level : ", terminator: "");
	tree.print_middle_level(tree.root, tree.root);
}
main();

Output

 Tree Nodes :    6   7   3   1   3   4   2   5   6   8   1   7   8   1   9   1   3   2   8   1   2   9   3   4   1   4   5   6   5   6   7
 Middle Level :    3   8   2   1


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