Print node at corresponding level in binary tree
Here given code implementation process.
// Java program for
// Print node at corresponding level in binary tree
// Binary Tree node
class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Queue Node
class QNode
{
public TreeNode n;
public QNode next;
public QNode(TreeNode n)
{
this.n = n;
this.next = null;
}
}
//Define custom queue class
class MyQueue
{
public QNode front;
public QNode rear;
public int size;
public MyQueue()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
public void enqueue(TreeNode n)
{
QNode node = new QNode(n);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size++;
this.rear = node;
}
// Delete front node of queue
public void dequeue()
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size--;
}
}
public int isSize()
{
return this.size;
}
public boolean isEmpty()
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
public TreeNode peek()
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front.n;
}
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public BinaryTree()
{
this.root = null;
}
// Print corresponding node of each level when exist
public void printLevelNode()
{
if (this.root == null)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
MyQueue q1 = new MyQueue();
MyQueue q2 = new MyQueue();
// Add root to q1
q1.enqueue(this.root);
// auxiliary temp variable
TreeNode temp = null;
int level = 1;
int count = 1;
System.out.print("\n Middle Element Of Level");
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1.isEmpty() || !q2.isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!q1.isEmpty())
{
// Get top node of q1 queue
temp = q1.peek();
if (level == count)
{
// Display the corresponding level node
System.out.print("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q2.enqueue(temp.left);
}
if (temp.right != null)
{
q2.enqueue(temp.right);
}
// Remove top element of q1 queue
q1.dequeue();
count++;
}
if (count < level)
{
System.out.print("\n Level " + level + " : None ");
}
// Change level
level++;
count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!q2.isEmpty())
{
// Get top node of q2 queue
temp = q2.peek();
if (level == count)
{
// Display the corresponding level node
System.out.print("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q1.enqueue(temp.left);
}
if (temp.right != null)
{
q1.enqueue(temp.right);
}
// Remove top element of q2 queue
q2.dequeue();
count++;
}
if (count < level && count > 1)
{
System.out.print("\n Level " + level + " : None ");
}
count = 1;
// Change level
level++;
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
tree.root = new TreeNode(-6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(1);
tree.root.left.right.right = new TreeNode(-4);
tree.root.left.right.right.right = new TreeNode(7);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(6);
tree.root.right = new TreeNode(-7);
tree.root.right.left = new TreeNode(1);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(9);
tree.root.right.right.right.right = new TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
tree.printLevelNode();
}
}
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Print node at corresponding level in binary tree
// Binary Tree node
class TreeNode
{
public: int data;
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
// Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
// Queue Node
class QNode
{
public:
TreeNode *n;
QNode *next;
QNode(TreeNode *n)
{
this->n = n;
this->next = NULL;
}
};
//Define custom queue class
class MyQueue
{
public:
QNode *front;
QNode *rear;
int size;
MyQueue()
{
this->front = NULL;
this->rear = NULL;
this->size = 0;
}
// Add a new node at last of queue
void enqueue(TreeNode *n)
{
QNode *node = new QNode(n);
if (this->front == NULL)
{
// When first node of queue
this->front = node;
}
else
{
// Add node at last level
this->rear->next = node;
}
this->size++;
this->rear = node;
}
// Delete front node of queue
void dequeue()
{
if (this->front != NULL)
{
if (this->rear == this->front)
{
this->rear = NULL;
this->front = NULL;
}
else
{
this->front = this->front->next;
}
this->size--;
}
}
int isSize()
{
return this->size;
}
bool isEmpty()
{
if (this->isSize() == 0)
{
return true;
}
return false;
}
TreeNode *peek()
{
if (this->isSize() == 0)
{
return NULL;
}
else
{
return this->front->n;
}
}
};
// Define Binary Tree
class BinaryTree
{
public:
TreeNode *root;
BinaryTree()
{
this->root = NULL;
}
// Print corresponding node of each level when exist
void printLevelNode()
{
if (this->root == NULL)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
MyQueue *q1 = new MyQueue();
MyQueue *q2 = new MyQueue();
// Add root to q1
q1->enqueue(this->root);
// auxiliary temp variable
TreeNode *temp = NULL;
int level = 1;
int count = 1;
cout << "\n Middle Element Of Level";
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1->isEmpty() || !q2->isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!q1->isEmpty())
{
// Get top node of q1 queue
temp = q1->peek();
if (level == count)
{
// Display the corresponding level node
cout << "\n Level " << level << " : " << temp->data;
}
if (temp->left != NULL)
{
q2->enqueue(temp->left);
}
if (temp->right != NULL)
{
q2->enqueue(temp->right);
}
// Remove top element of q1 queue
q1->dequeue();
count++;
}
if (count < level)
{
cout << "\n Level " << level << " : None ";
}
// Change level
level++;
count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!q2->isEmpty())
{
// Get top node of q2 queue
temp = q2->peek();
if (level == count)
{
// Display the corresponding level node
cout << "\n Level " << level << " : " << temp->data;
}
if (temp->left != NULL)
{
q1->enqueue(temp->left);
}
if (temp->right != NULL)
{
q1->enqueue(temp->right);
}
// Remove top element of q2 queue
q2->dequeue();
count++;
}
if (count < level && count > 1)
{
cout << "\n Level " << level << " : None ";
}
count = 1;
// Change level
level++;
}
}
};
int main()
{
BinaryTree *tree = new BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
tree->root = new TreeNode(-6);
tree->root->left = new TreeNode(4);
tree->root->left->right = new TreeNode(3);
tree->root->left->right->left = new TreeNode(10);
tree->root->left->right->left->left = new TreeNode(1);
tree->root->left->right->right = new TreeNode(-4);
tree->root->left->right->right->right = new TreeNode(7);
tree->root->left->left = new TreeNode(2);
tree->root->left->left->left = new TreeNode(6);
tree->root->right = new TreeNode(-7);
tree->root->right->left = new TreeNode(1);
tree->root->right->right = new TreeNode(12);
tree->root->right->right->left = new TreeNode(5);
tree->root->right->right->right = new TreeNode(9);
tree->root->right->right->right->right = new TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
tree->printLevelNode();
return 0;
}
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
// Include namespace system
using System;
// Csharp program for
// Print node at corresponding level in binary tree
// Binary Tree node
public class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Queue Node
public class QNode
{
public TreeNode n;
public QNode next;
public QNode(TreeNode n)
{
this.n = n;
this.next = null;
}
}
//Define custom queue class
public class MyQueue
{
public QNode front;
public QNode rear;
public int size;
public MyQueue()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
public void enqueue(TreeNode n)
{
QNode node = new QNode(n);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size++;
this.rear = node;
}
// Delete front node of queue
public void dequeue()
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size--;
}
}
public int isSize()
{
return this.size;
}
public Boolean isEmpty()
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
public TreeNode peek()
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front.n;
}
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public BinaryTree()
{
this.root = null;
}
// Print corresponding node of each level when exist
public void printLevelNode()
{
if (this.root == null)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
MyQueue q1 = new MyQueue();
MyQueue q2 = new MyQueue();
// Add root to q1
q1.enqueue(this.root);
// auxiliary temp variable
TreeNode temp = null;
int level = 1;
int count = 1;
Console.Write("\n Middle Element Of Level");
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1.isEmpty() || !q2.isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!q1.isEmpty())
{
// Get top node of q1 queue
temp = q1.peek();
if (level == count)
{
// Display the corresponding level node
Console.Write("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q2.enqueue(temp.left);
}
if (temp.right != null)
{
q2.enqueue(temp.right);
}
// Remove top element of q1 queue
q1.dequeue();
count++;
}
if (count < level)
{
Console.Write("\n Level " + level + " : None ");
}
// Change level
level++;
count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!q2.isEmpty())
{
// Get top node of q2 queue
temp = q2.peek();
if (level == count)
{
// Display the corresponding level node
Console.Write("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q1.enqueue(temp.left);
}
if (temp.right != null)
{
q1.enqueue(temp.right);
}
// Remove top element of q2 queue
q2.dequeue();
count++;
}
if (count < level && count > 1)
{
Console.Write("\n Level " + level + " : None ");
}
count = 1;
// Change level
level++;
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
tree.root = new TreeNode(-6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(1);
tree.root.left.right.right = new TreeNode(-4);
tree.root.left.right.right.right = new TreeNode(7);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(6);
tree.root.right = new TreeNode(-7);
tree.root.right.left = new TreeNode(1);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(9);
tree.root.right.right.right.right = new TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
tree.printLevelNode();
}
}
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
<?php
// Php program for
// Print node at corresponding level in binary tree
// Binary Tree node
class TreeNode
{
public $data;
public $left;
public $right;
public function __construct($data)
{
// Set node value
$this->data = $data;
$this->left = NULL;
$this->right = NULL;
}
}
// Queue Node
class QNode
{
public $n;
public $next;
public function __construct($n)
{
$this->n = $n;
$this->next = NULL;
}
}
//Define custom queue class
class MyQueue
{
public $front;
public $rear;
public $size;
public function __construct()
{
$this->front = NULL;
$this->rear = NULL;
$this->size = 0;
}
// Add a new node at last of queue
public function enqueue($n)
{
$node = new QNode($n);
if ($this->front == NULL)
{
// When first node of queue
$this->front = $node;
}
else
{
// Add node at last level
$this->rear->next = $node;
}
$this->size++;
$this->rear = $node;
}
// Delete front node of queue
public function dequeue()
{
if ($this->front != NULL)
{
if ($this->rear == $this->front)
{
$this->rear = NULL;
$this->front = NULL;
}
else
{
$this->front = $this->front->next;
}
$this->size--;
}
}
public function isSize()
{
return $this->size;
}
public function isEmpty()
{
if ($this->isSize() == 0)
{
return true;
}
return false;
}
public function peek()
{
if ($this->isSize() == 0)
{
return NULL;
}
else
{
return $this->front->n;
}
}
}
// Define Binary Tree
class BinaryTree
{
public $root;
public function __construct()
{
$this->root = NULL;
}
// Print corresponding node of each level when exist
public function printLevelNode()
{
if ($this->root == NULL)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
$q1 = new MyQueue();
$q2 = new MyQueue();
// Add root to q1
$q1->enqueue($this->root);
// auxiliary temp variable
$temp = NULL;
$level = 1;
$count = 1;
echo("\n Middle Element Of Level");
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!$q1->isEmpty() || !$q2->isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!$q1->isEmpty())
{
// Get top node of q1 queue
$temp = $q1->peek();
if ($level == $count)
{
// Display the corresponding level node
echo("\n Level ".$level.
" : ".$temp->data);
}
if ($temp->left != NULL)
{
$q2->enqueue($temp->left);
}
if ($temp->right != NULL)
{
$q2->enqueue($temp->right);
}
// Remove top element of q1 queue
$q1->dequeue();
$count++;
}
if ($count < $level)
{
echo("\n Level ".$level.
" : None ");
}
// Change level
$level++;
$count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!$q2->isEmpty())
{
// Get top node of q2 queue
$temp = $q2->peek();
if ($level == $count)
{
// Display the corresponding level node
echo("\n Level ".$level.
" : ".$temp->data);
}
if ($temp->left != NULL)
{
$q1->enqueue($temp->left);
}
if ($temp->right != NULL)
{
$q1->enqueue($temp->right);
}
// Remove top element of q2 queue
$q2->dequeue();
$count++;
}
if ($count < $level && $count > 1)
{
echo("\n Level ".$level.
" : None ");
}
$count = 1;
// Change level
$level++;
}
}
}
function main()
{
$tree = new BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
$tree->root = new TreeNode(-6);
$tree->root->left = new TreeNode(4);
$tree->root->left->right = new TreeNode(3);
$tree->root->left->right->left = new TreeNode(10);
$tree->root->left->right->left->left = new TreeNode(1);
$tree->root->left->right->right = new TreeNode(-4);
$tree->root->left->right->right->right = new TreeNode(7);
$tree->root->left->left = new TreeNode(2);
$tree->root->left->left->left = new TreeNode(6);
$tree->root->right = new TreeNode(-7);
$tree->root->right->left = new TreeNode(1);
$tree->root->right->right = new TreeNode(12);
$tree->root->right->right->left = new TreeNode(5);
$tree->root->right->right->right = new TreeNode(9);
$tree->root->right->right->right->right = new TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
$tree->printLevelNode();
}
main();
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
// Node JS program for
// Print node at corresponding level in binary tree
// Binary Tree node
class TreeNode
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Queue Node
class QNode
{
constructor(n)
{
this.n = n;
this.next = null;
}
}
//Define custom queue class
class MyQueue
{
constructor()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
enqueue(n)
{
var node = new QNode(n);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size++;
this.rear = node;
}
// Delete front node of queue
dequeue()
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size--;
}
}
isSize()
{
return this.size;
}
isEmpty()
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
peek()
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front.n;
}
}
}
// Define Binary Tree
class BinaryTree
{
constructor()
{
this.root = null;
}
// Print corresponding node of each level when exist
printLevelNode()
{
if (this.root == null)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
var q1 = new MyQueue();
var q2 = new MyQueue();
// Add root to q1
q1.enqueue(this.root);
// auxiliary temp variable
var temp = null;
var level = 1;
var count = 1;
process.stdout.write("\n Middle Element Of Level");
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1.isEmpty() || !q2.isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!q1.isEmpty())
{
// Get top node of q1 queue
temp = q1.peek();
if (level == count)
{
// Display the corresponding level node
process.stdout.write("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q2.enqueue(temp.left);
}
if (temp.right != null)
{
q2.enqueue(temp.right);
}
// Remove top element of q1 queue
q1.dequeue();
count++;
}
if (count < level)
{
process.stdout.write("\n Level " + level + " : None ");
}
// Change level
level++;
count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!q2.isEmpty())
{
// Get top node of q2 queue
temp = q2.peek();
if (level == count)
{
// Display the corresponding level node
process.stdout.write("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q1.enqueue(temp.left);
}
if (temp.right != null)
{
q1.enqueue(temp.right);
}
// Remove top element of q2 queue
q2.dequeue();
count++;
}
if (count < level && count > 1)
{
process.stdout.write("\n Level " + level + " : None ");
}
count = 1;
// Change level
level++;
}
}
}
function main()
{
var tree = new BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
tree.root = new TreeNode(-6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(1);
tree.root.left.right.right = new TreeNode(-4);
tree.root.left.right.right.right = new TreeNode(7);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(6);
tree.root.right = new TreeNode(-7);
tree.root.right.left = new TreeNode(1);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(9);
tree.root.right.right.right.right = new TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
tree.printLevelNode();
}
main();
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
# Python 3 program for
# Print node at corresponding level in binary tree
# Binary Tree node
class TreeNode :
def __init__(self, data) :
# Set node value
self.data = data
self.left = None
self.right = None
# Queue Node
class QNode :
def __init__(self, n) :
self.n = n
self.next = None
# Define custom queue class
class MyQueue :
def __init__(self) :
self.front = None
self.rear = None
self.size = 0
# Add a new node at last of queue
def enqueue(self, n) :
node = QNode(n)
if (self.front == None) :
# When first node of queue
self.front = node
else :
# Add node at last level
self.rear.next = node
self.size += 1
self.rear = node
# Delete front node of queue
def dequeue(self) :
if (self.front != None) :
if (self.rear == self.front) :
self.rear = None
self.front = None
else :
self.front = self.front.next
self.size -= 1
def isSize(self) :
return self.size
def isEmpty(self) :
if (self.isSize() == 0) :
return True
return False
def peek(self) :
if (self.isSize() == 0) :
return None
else :
return self.front.n
# Define Binary Tree
class BinaryTree :
def __init__(self) :
self.root = None
# Print corresponding node of each level when exist
def printLevelNode(self) :
if (self.root == None) :
# When tree is empty
return
# Auxiliary queue which is capable to contain a tree level nodes
q1 = MyQueue()
q2 = MyQueue()
# Add root to q1
q1.enqueue(self.root)
# auxiliary temp variable
temp = None
level = 1
count = 1
print("\n Middle Element Of Level", end = "")
# This loop execute until auxiliary queue q1 and q2 are not empty
while (not q1.isEmpty() or not q2.isEmpty()) :
# Execute loop until q1 queue are not empty
# And store the next level node in q2 queue
while (not q1.isEmpty()) :
# Get top node of q1 queue
temp = q1.peek()
if (level == count) :
# Display the corresponding level node
print("\n Level ", level ," : ", temp.data, end = "")
if (temp.left != None) :
q2.enqueue(temp.left)
if (temp.right != None) :
q2.enqueue(temp.right)
# Remove top element of q1 queue
q1.dequeue()
count += 1
if (count < level) :
print("\n Level ", level ," : None ", end = "")
# Change level
level += 1
count = 1
# Execute loop until q2 queue are not empty
# And store the next level node in q1 queue
while (not q2.isEmpty()) :
# Get top node of q2 queue
temp = q2.peek()
if (level == count) :
# Display the corresponding level node
print("\n Level ", level ," : ", temp.data, end = "")
if (temp.left != None) :
q1.enqueue(temp.left)
if (temp.right != None) :
q1.enqueue(temp.right)
# Remove top element of q2 queue
q2.dequeue()
count += 1
if (count < level and count > 1) :
print("\n Level ", level ," : None ", end = "")
count = 1
# Change level
level += 1
def main() :
tree = BinaryTree()
# Create Binary Tree
# -----------------
# -6
# / \
# 4 -7
# / \ / \
# 2 3 1 12
# / / \ / \
# 6 10 -4 5 9
# / \ \
# 1 7 8
tree.root = TreeNode(-6)
tree.root.left = TreeNode(4)
tree.root.left.right = TreeNode(3)
tree.root.left.right.left = TreeNode(10)
tree.root.left.right.left.left = TreeNode(1)
tree.root.left.right.right = TreeNode(-4)
tree.root.left.right.right.right = TreeNode(7)
tree.root.left.left = TreeNode(2)
tree.root.left.left.left = TreeNode(6)
tree.root.right = TreeNode(-7)
tree.root.right.left = TreeNode(1)
tree.root.right.right = TreeNode(12)
tree.root.right.right.left = TreeNode(5)
tree.root.right.right.right = TreeNode(9)
tree.root.right.right.right.right = TreeNode(8)
# -6 Level 1 : [-6] First node -6
# / \
# 4 -7 Level 2 : [4,-7] Second node -7
# / \ / \
# 2 3 1 12 Level 3 : [2,3,1,12] Third node 1
# / / \ / \
# 6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
# / \ \
# 1 7 8 Level 5 : [1,7,8] fifth node None
tree.printLevelNode()
if __name__ == "__main__": main()
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
# Ruby program for
# Print node at corresponding level in binary tree
# Binary Tree node
class TreeNode
# Define the accessor and reader of class TreeNode
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
# Queue Node
class QNode
# Define the accessor and reader of class QNode
attr_reader :n, :next
attr_accessor :n, :next
def initialize(n)
self.n = n
self.next = nil
end
end
# Define custom queue class
class MyQueue
# Define the accessor and reader of class MyQueue
attr_reader :front, :rear, :size
attr_accessor :front, :rear, :size
def initialize()
self.front = nil
self.rear = nil
self.size = 0
end
# Add a new node at last of queue
def enqueue(n)
node = QNode.new(n)
if (self.front == nil)
# When first node of queue
self.front = node
else
# Add node at last level
self.rear.next = node
end
self.size += 1
self.rear = node
end
# Delete front node of queue
def dequeue()
if (self.front != nil)
if (self.rear == self.front)
self.rear = nil
self.front = nil
else
self.front = self.front.next
end
self.size -= 1
end
end
def isSize()
return self.size
end
def isEmpty()
if (self.isSize() == 0)
return true
end
return false
end
def peek()
if (self.isSize() == 0)
return nil
else
return self.front.n
end
end
end
# Define Binary Tree
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_reader :root
attr_accessor :root
def initialize()
self.root = nil
end
# Print corresponding node of each level when exist
def printLevelNode()
if (self.root == nil)
# When tree is empty
return
end
# Auxiliary queue which is capable to contain a tree level nodes
q1 = MyQueue.new()
q2 = MyQueue.new()
# Add root to q1
q1.enqueue(self.root)
# auxiliary temp variable
temp = nil
level = 1
count = 1
print("\n Middle Element Of Level")
# This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1.isEmpty() || !q2.isEmpty())
# Execute loop until q1 queue are not empty
# And store the next level node in q2 queue
while (!q1.isEmpty())
# Get top node of q1 queue
temp = q1.peek()
if (level == count)
# Display the corresponding level node
print("\n Level ", level ," : ", temp.data)
end
if (temp.left != nil)
q2.enqueue(temp.left)
end
if (temp.right != nil)
q2.enqueue(temp.right)
end
# Remove top element of q1 queue
q1.dequeue()
count += 1
end
if (count < level)
print("\n Level ", level ," : None ")
end
# Change level
level += 1
count = 1
# Execute loop until q2 queue are not empty
# And store the next level node in q1 queue
while (!q2.isEmpty())
# Get top node of q2 queue
temp = q2.peek()
if (level == count)
# Display the corresponding level node
print("\n Level ", level ," : ", temp.data)
end
if (temp.left != nil)
q1.enqueue(temp.left)
end
if (temp.right != nil)
q1.enqueue(temp.right)
end
# Remove top element of q2 queue
q2.dequeue()
count += 1
end
if (count < level && count > 1)
print("\n Level ", level ," : None ")
end
count = 1
# Change level
level += 1
end
end
end
def main()
tree = BinaryTree.new()
# Create Binary Tree
# -----------------
# -6
# / \
# 4 -7
# / \ / \
# 2 3 1 12
# / / \ / \
# 6 10 -4 5 9
# / \ \
# 1 7 8
tree.root = TreeNode.new(-6)
tree.root.left = TreeNode.new(4)
tree.root.left.right = TreeNode.new(3)
tree.root.left.right.left = TreeNode.new(10)
tree.root.left.right.left.left = TreeNode.new(1)
tree.root.left.right.right = TreeNode.new(-4)
tree.root.left.right.right.right = TreeNode.new(7)
tree.root.left.left = TreeNode.new(2)
tree.root.left.left.left = TreeNode.new(6)
tree.root.right = TreeNode.new(-7)
tree.root.right.left = TreeNode.new(1)
tree.root.right.right = TreeNode.new(12)
tree.root.right.right.left = TreeNode.new(5)
tree.root.right.right.right = TreeNode.new(9)
tree.root.right.right.right.right = TreeNode.new(8)
# -6 Level 1 : [-6] First node -6
# / \
# 4 -7 Level 2 : [4,-7] Second node -7
# / \ / \
# 2 3 1 12 Level 3 : [2,3,1,12] Third node 1
# / / \ / \
# 6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
# / \ \
# 1 7 8 Level 5 : [1,7,8] fifth node None
tree.printLevelNode()
end
main()
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
// Scala program for
// Print node at corresponding level in binary tree
// Binary Tree node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
def this(data: Int)
{
// Set node value
this(data,null,null);
}
}
// Queue Node
class QNode(var n: TreeNode , var next: QNode)
{
def this(n: TreeNode)
{
this(n,null);
}
}
//Define custom queue class
class MyQueue(var front: QNode , var rear: QNode , var size: Int)
{
def this()
{
this(null,null,0);
}
// Add a new node at last of queue
def enqueue(n: TreeNode): Unit = {
var node: QNode = new QNode(n);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear.next = node;
}
this.size += 1;
this.rear = node;
}
// Delete front node of queue
def dequeue(): Unit = {
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front.next;
}
this.size -= 1;
}
}
def isSize(): Int = {
return this.size;
}
def isEmpty(): Boolean = {
if (this.isSize() == 0)
{
return true;
}
return false;
}
def peek(): TreeNode = {
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front.n;
}
}
}
// Define Binary Tree
class BinaryTree(var root: TreeNode)
{
def this()
{
this(null);
}
// Print corresponding node of each level when exist
def printLevelNode(): Unit = {
if (this.root == null)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
var q1: MyQueue = new MyQueue();
var q2: MyQueue = new MyQueue();
// Add root to q1
q1.enqueue(this.root);
// auxiliary temp variable
var temp: TreeNode = null;
var level: Int = 1;
var count: Int = 1;
print("\n Middle Element Of Level");
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1.isEmpty() || !q2.isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!q1.isEmpty())
{
// Get top node of q1 queue
temp = q1.peek();
if (level == count)
{
// Display the corresponding level node
print("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q2.enqueue(temp.left);
}
if (temp.right != null)
{
q2.enqueue(temp.right);
}
// Remove top element of q1 queue
q1.dequeue();
count += 1;
}
if (count < level)
{
print("\n Level " + level + " : None ");
}
// Change level
level += 1;
count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!q2.isEmpty())
{
// Get top node of q2 queue
temp = q2.peek();
if (level == count)
{
// Display the corresponding level node
print("\n Level " + level + " : " + temp.data);
}
if (temp.left != null)
{
q1.enqueue(temp.left);
}
if (temp.right != null)
{
q1.enqueue(temp.right);
}
// Remove top element of q2 queue
q2.dequeue();
count += 1;
}
if (count < level && count > 1)
{
print("\n Level " + level + " : None ");
}
count = 1;
// Change level
level += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
tree.root = new TreeNode(-6);
tree.root.left = new TreeNode(4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(1);
tree.root.left.right.right = new TreeNode(-4);
tree.root.left.right.right.right = new TreeNode(7);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(6);
tree.root.right = new TreeNode(-7);
tree.root.right.left = new TreeNode(1);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(9);
tree.root.right.right.right.right = new TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
tree.printLevelNode();
}
}
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
// Swift 4 program for
// Print node at corresponding level in binary tree
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode? ;
var right: TreeNode? ;
init(_ data: Int)
{
// Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
// Queue Node
class QNode
{
var n: TreeNode? ;
var next: QNode? ;
init(_ n: TreeNode? )
{
self.n = n;
self.next = nil;
}
}
// Define custom queue class
class MyQueue
{
var front: QNode? ;
var rear: QNode? ;
var size: Int;
init()
{
self.front = nil;
self.rear = nil;
self.size = 0;
}
// Add a new node at last of queue
func enqueue(_ n: TreeNode? )
{
let node: QNode = QNode(n);
if (self.front == nil)
{
// When first node of queue
self.front = node;
}
else
{
// Add node at last level
self.rear!.next = node;
}
self.size += 1;
self.rear = node;
}
// Delete front node of queue
func dequeue()
{
if (self.front != nil)
{
if (self.rear === self.front)
{
self.rear = nil;
self.front = nil;
}
else
{
self.front = self.front!.next;
}
self.size -= 1;
}
}
func isSize()->Int
{
return self.size;
}
func isEmpty()->Bool
{
if (self.isSize() == 0)
{
return true;
}
return false;
}
func peek()->TreeNode?
{
if (self.isSize() == 0)
{
return nil;
}
else
{
return self.front!.n;
}
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode? ;
init()
{
self.root = nil;
}
// Print corresponding node of each level when exist
func printLevelNode()
{
if (self.root == nil)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
let q1: MyQueue = MyQueue();
let q2: MyQueue = MyQueue();
// Add root to q1
q1.enqueue(self.root);
// auxiliary temp variable
var temp: TreeNode? = nil;
var level: Int = 1;
var count: Int = 1;
print("\n Middle Element Of Level", terminator: "");
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1.isEmpty() || !q2.isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!q1.isEmpty())
{
// Get top node of q1 queue
temp = q1.peek();
if (level == count)
{
// Display the corresponding level node
print("\n Level ", level ," : ", temp!.data, terminator: "");
}
if (temp!.left != nil)
{
q2.enqueue(temp!.left);
}
if (temp!.right != nil)
{
q2.enqueue(temp!.right);
}
// Remove top element of q1 queue
q1.dequeue();
count += 1;
}
if (count < level)
{
print("\n Level ", level ," : None ", terminator: "");
}
// Change level
level += 1;
count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!q2.isEmpty())
{
// Get top node of q2 queue
temp = q2.peek();
if (level == count)
{
// Display the corresponding level node
print("\n Level ", level ," : ", temp!.data, terminator: "");
}
if (temp!.left != nil)
{
q1.enqueue(temp!.left);
}
if (temp!.right != nil)
{
q1.enqueue(temp!.right);
}
// Remove top element of q2 queue
q2.dequeue();
count += 1;
}
if (count < level && count > 1)
{
print("\n Level ", level ," : None ", terminator: "");
}
count = 1;
// Change level
level += 1;
}
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
tree.root = TreeNode(-6);
tree.root!.left = TreeNode(4);
tree.root!.left!.right = TreeNode(3);
tree.root!.left!.right!.left = TreeNode(10);
tree.root!.left!.right!.left!.left = TreeNode(1);
tree.root!.left!.right!.right = TreeNode(-4);
tree.root!.left!.right!.right!.right = TreeNode(7);
tree.root!.left!.left = TreeNode(2);
tree.root!.left!.left!.left = TreeNode(6);
tree.root!.right = TreeNode(-7);
tree.root!.right!.left = TreeNode(1);
tree.root!.right!.right = TreeNode(12);
tree.root!.right!.right!.left = TreeNode(5);
tree.root!.right!.right!.right = TreeNode(9);
tree.root!.right!.right!.right!.right = TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
tree.printLevelNode();
}
main();
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
// Kotlin program for
// Print node at corresponding level in binary tree
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Queue Node
class QNode
{
var n: TreeNode ? ;
var next: QNode ? ;
constructor(n: TreeNode ? )
{
this.n = n;
this.next = null;
}
}
//Define custom queue class
class MyQueue
{
var front: QNode ? ;
var rear: QNode ? ;
var size: Int;
constructor()
{
this.front = null;
this.rear = null;
this.size = 0;
}
// Add a new node at last of queue
fun enqueue(n: TreeNode ? ): Unit
{
val node: QNode = QNode(n);
if (this.front == null)
{
// When first node of queue
this.front = node;
}
else
{
// Add node at last level
this.rear?.next = node;
}
this.size += 1;
this.rear = node;
}
// Delete front node of queue
fun dequeue(): Unit
{
if (this.front != null)
{
if (this.rear == this.front)
{
this.rear = null;
this.front = null;
}
else
{
this.front = this.front?.next;
}
this.size -= 1;
}
}
fun isSize(): Int
{
return this.size;
}
fun isEmpty(): Boolean
{
if (this.isSize() == 0)
{
return true;
}
return false;
}
fun peek(): TreeNode ?
{
if (this.isSize() == 0)
{
return null;
}
else
{
return this.front?.n;
}
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode ? ;
constructor()
{
this.root = null;
}
// Print corresponding node of each level when exist
fun printLevelNode(): Unit
{
if (this.root == null)
{
// When tree is empty
return;
}
// Auxiliary queue which is capable to contain a tree level nodes
val q1: MyQueue = MyQueue();
val q2: MyQueue = MyQueue();
// Add root to q1
q1.enqueue(this.root);
// auxiliary temp variable
var temp: TreeNode ? ;
var level: Int = 1;
var count: Int = 1;
print("\n Middle Element Of Level");
// This loop execute until auxiliary queue q1 and q2 are not empty
while (!q1.isEmpty() || !q2.isEmpty())
{
// Execute loop until q1 queue are not empty
// And store the next level node in q2 queue
while (!q1.isEmpty())
{
// Get top node of q1 queue
temp = q1.peek();
if (level == count)
{
// Display the corresponding level node
print("\n Level " + level + " : " + temp!!.data);
}
if (temp?.left != null)
{
q2.enqueue(temp.left);
}
if (temp?.right != null)
{
q2.enqueue(temp.right);
}
// Remove top element of q1 queue
q1.dequeue();
count += 1;
}
if (count < level)
{
print("\n Level " + level + " : None ");
}
// Change level
level += 1;
count = 1;
// Execute loop until q2 queue are not empty
// And store the next level node in q1 queue
while (!q2.isEmpty())
{
// Get top node of q2 queue
temp = q2.peek();
if (level == count)
{
// Display the corresponding level node
print("\n Level " + level + " : " + temp!!.data);
}
if (temp?.left != null)
{
q1.enqueue(temp.left);
}
if (temp?.right != null)
{
q1.enqueue(temp.right);
}
// Remove top element of q2 queue
q2.dequeue();
count += 1;
}
if (count < level && count > 1)
{
print("\n Level " + level + " : None ");
}
count = 1;
// Change level
level += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: BinaryTree = BinaryTree();
/*
Create Binary Tree
-----------------
-6
/ \
4 -7
/ \ / \
2 3 1 12
/ / \ / \
6 10 -4 5 9
/ \ \
1 7 8
*/
tree.root = TreeNode(-6);
tree.root?.left = TreeNode(4);
tree.root?.left?.right = TreeNode(3);
tree.root?.left?.right?.left = TreeNode(10);
tree.root?.left?.right?.left?.left = TreeNode(1);
tree.root?.left?.right?.right = TreeNode(-4);
tree.root?.left?.right?.right?.right = TreeNode(7);
tree.root?.left?.left = TreeNode(2);
tree.root?.left?.left?.left = TreeNode(6);
tree.root?.right = TreeNode(-7);
tree.root?.right?.left = TreeNode(1);
tree.root?.right?.right = TreeNode(12);
tree.root?.right?.right?.left = TreeNode(5);
tree.root?.right?.right?.right = TreeNode(9);
tree.root?.right?.right?.right?.right = TreeNode(8);
/*
-6 Level 1 : [-6] First node -6
/ \
4 -7 Level 2 : [4,-7] Second node -7
/ \ / \
2 3 1 12 Level 3 : [2,3,1,12] Third node 1
/ / \ / \
6 10 -4 5 9 Level 4 : [6,10,-4,5,9] Fourth node 5
/ \ \
1 7 8 Level 5 : [1,7,8] fifth node None
*/
tree.printLevelNode();
}
input
Middle Element Of Level
Level 1 : -6
Level 2 : -7
Level 3 : 1
Level 4 : 5
Level 5 : None
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