Flatten binary tree nodes in level order from

Flatten binary tree in level order from

Here given code implementation process.

/*
  C Program 
  Flatten binary tree nodes in level order from
*/
#include <stdio.h>

#include <stdlib.h>

//Binary Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
//Define Custom queue
struct MyQueue
{
	struct Node *element;
	struct MyQueue *next;
};
//This is creating a binary tree node and return this
struct Node *insert(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;
}
//Create a MyQueue element and return this reference
struct MyQueue *queue_node(struct Node *tree_node)
{
	struct MyQueue *MyQueue_node = (struct MyQueue *) malloc(sizeof(struct MyQueue));
	if (MyQueue_node != NULL)
	{
		//set pointer values
		MyQueue_node->element = tree_node;
		MyQueue_node->next = NULL;
	}
	else
	{
		printf("Memory Overflow\n");
		exit(0); //Terminate program execution
	}
	return MyQueue_node;
}
//Remove a MyQueue elements
void dequeue(struct MyQueue **head)
{
	if ( *head != NULL)
	{
		//Get a deleted node
		struct MyQueue *remove = *head;
		//Visit to next node of MyQueue
		*head = remove->next;
		//unlink tree element
		remove->element = NULL;
		//unlink next node
		remove->next = NULL;
		//free node
		free(remove);
		remove = NULL;
	}
}
//Transform tree nodes into its level order
void flatten_level_order(struct Node *node)
{
	if (node != NULL)
	{
		//Make MyQueue variables
		struct MyQueue *head = NULL;
		struct MyQueue *tail = NULL;
		//Get first node of tree
		struct Node *auxiliary = node;
		//Add first node into MyQueue
		head = queue_node(node);
		//set tail to first node
		tail = head;
		//iterate the MyQueue elements
		while (head != NULL)
		{
			//Get current head element of MyQueue
			auxiliary = head->element;
			if (auxiliary->left != NULL)
			{
				//Add new left child node
				tail->next = queue_node(auxiliary->left);
				tail = tail->next;
			}
			if (auxiliary->right != NULL)
			{
				//Add new right child node
				tail->next = queue_node(auxiliary->right);
				tail = tail->next;
			}
			//unlink left subtree
			auxiliary->left = NULL;
			//Remove element of MyQueue
			head->element = NULL;
			dequeue( &head);
			if (head != NULL)
			{
				auxiliary->right = head->element;
			}
			else
			{
				auxiliary->right = NULL;
			}
		}
		tail = NULL;
	}
	else
	{
		printf("Empty Linked List\n");
	}
}
//Display flatten elements of tree
void show_element(struct Node *root)
{
	if (root == NULL)
	{
		printf("\n Empty Tree\n");
		return;
	}
	printf("\n Flatten Tree Node is :");
	struct Node *temp = root;
	//Iterate tree elements
	while (temp != NULL)
	{
		//Display tree node
		printf("  %d", temp->data);
		//Visit to next node
		temp = temp->right;
	}
}
int main()
{
	struct Node *root = NULL;
	/* 
	  Construct Binary Tree
	  ---------------------
	           1
	         /   \
	        2     3
	       /     / \
	      4     5   6
	     /     / \   \
	    7     8   9   11
	*/
	//Add nodes
	root = insert(1);
	root->left = insert(2);
	root->right = insert(3);
	root->right->right = insert(6);
	root->right->right->right = insert(11);
	root->right->left = insert(5);
	root->left->left = insert(4);
	root->left->left->left = insert(7);
	root->right->left->left = insert(8);
	root->right->left->right = insert(9);
	//Transform tree to level order
	flatten_level_order(root);
	show_element(root);
	return 0;
}

Output

 Flatten Tree Node is :  1  2  3  4  5  6  7  8  9  11
/* 
  Java Program 
  Flatten binary tree nodes in level order from
*/
//Node of Binary Tree
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;
	}
}
//Define Custom queue
class MyQueue
{
	public Node element;
	public MyQueue next;
	public MyQueue(Node element)
	{
		this.element = element;
		this.next = null;
	}
}
class BinaryTree
{
	public Node root;
	public MyQueue head;
	public MyQueue tail;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
		//Set queue is empty
		this.head = null;
		this.tail = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	public void preorder(Node node)
	{
		if (node != null)
		{
			//Print node value
			System.out.print("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	//Create queue node
	public void enqueue(Node element)
	{
		MyQueue new_node = new MyQueue(element);
		if (this.head == null)
		{
			this.head = new_node;
			this.tail = new_node;
		}
		else
		{
			this.tail.next = new_node;
			this.tail = new_node;
		}
	}
	//Remove a dequeue element
	public void dequeue()
	{
		if (this.head != null)
		{
			MyQueue temp = this.head;
			if (this.head == this.tail)
			{
				this.tail = null;
			}
			this.head = this.head.next;
			//remove node
			temp.element = null;
			temp.next = null;
			temp = null;
		}
	}
	//Display flatten elements of tree
	public void show_element()
	{
		if (this.root == null)
		{
			System.out.print("\n Empty Tree\n");
			return;
		}
		System.out.print("Flatten Tree Node in preorder : \n");
		//Start to root node
		Node temp = this.root;
		//Iterate tree elements
		while (temp != null)
		{
			//Display node value
			System.out.print("  " + temp.data);
			//visit to next node
			temp = temp.right;
		}
		System.out.print("\n");
	}
	//Transform tree nodes into its level order
	public void flatten_level_order()
	{
		if (this.root != null)
		{
			//Get first node of tree
			Node auxiliary = this.root;
			//Add first node into MyQueue
			this.enqueue(this.root);
			//iterate the MyQueue elements
			while (this.head != null)
			{
				//Get current head element of MyQueue
				auxiliary = this.head.element;
				if (auxiliary.left != null)
				{
					//Add new left child node
					this.enqueue(auxiliary.left);
				}
				if (auxiliary.right != null)
				{
					//Add new right child node
					this.enqueue(auxiliary.right);
				}
				//unlink left subtree
				auxiliary.left = null;
				this.dequeue();
				if (this.head != null)
				{
					auxiliary.right = this.head.element;
				}
				else
				{
					auxiliary.right = null;
				}
			}
		}
		else
		{
			System.out.print("Empty Linked List\n");
		}
	}
	public static void main(String[] args)
	{
		//Make object of Binary Tree
		BinaryTree obj = new BinaryTree();
		/*  
		Construct Binary Tree
		---------------------
		       1
		     /   \
		    2     3
		   /     / \
		  4     5   6
		 /     / \   \
		7     8   9   11
		*/
		//Add nodes
		obj.root = new Node(1);
		obj.root.left = new Node(2);
		obj.root.right = new Node(3);
		obj.root.right.right = new Node(6);
		obj.root.right.right.right = new Node(11);
		obj.root.right.left = new Node(5);
		obj.root.left.left = new Node(4);
		obj.root.left.left.left = new Node(7);
		obj.root.right.left.left = new Node(8);
		obj.root.right.left.right = new Node(9);
		//Transform tree to level order
		obj.flatten_level_order();
		obj.show_element();
	}
}

Output

Flatten Tree Node in preorder :
  1  2  3  4  5  6  7  8  9  11
//Include header file
#include <iostream>

using namespace std;
/*
  C++ Program 
  Flatten binary tree nodes in level order from
*/

//Node of Binary Tree
class Node
{
	public: int data;
	Node *left;
	Node *right;
	Node(int data)
	{
		//Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
//Define Custom queue
class MyQueue
{
	public: Node *element;
	MyQueue *next;
	MyQueue(Node *element)
	{
		this->element = element;
		this->next = NULL;
	}
};
class BinaryTree
{
	public: Node *root;
	MyQueue *head;
	MyQueue *tail;
	BinaryTree()
	{
		//Set initial tree root to null
		this->root = NULL;
		//Set queue is empty
		this->head = NULL;
		this->tail = NULL;
	}
	//Recursive function
	//Display preorder view of binary tree
	void preorder(Node *node)
	{
		if (node != NULL)
		{
			//Print node value
			cout << "  " << node->data;
			this->preorder(node->left);
			this->preorder(node->right);
		}
	}
	//Create queue node
	void enqueue(Node *element)
	{
		MyQueue *new_node = new MyQueue(element);
		if (this->head == NULL)
		{
			this->head = new_node;
			this->tail = new_node;
		}
		else
		{
			this->tail->next = new_node;
			this->tail = new_node;
		}
	}
	//Remove a dequeue element
	void dequeue()
	{
		if (this->head != NULL)
		{
			MyQueue *temp = this->head;
			if (this->head == this->tail)
			{
				this->tail = NULL;
			}
			this->head = this->head->next;
			//remove node
			temp->element = NULL;
			temp->next = NULL;
			temp = NULL;
		}
	}
	//Display flatten elements of tree
	void show_element()
	{
		if (this->root == NULL)
		{
			cout << "\n Empty Tree\n";
			return;
		}
		cout << "Flatten Tree Node in preorder : \n";
		//Start to root node
		Node *temp = this->root;
		//Iterate tree elements
		while (temp != NULL)
		{
			//Display node value
			cout << "  " << temp->data;
			//visit to next node
			temp = temp->right;
		}
		cout << "\n";
	}
	//Transform tree nodes into its level order
	void flatten_level_order()
	{
		if (this->root != NULL)
		{
			//Get first node of tree
			Node *auxiliary = this->root;
			//Add first node into MyQueue
			this->enqueue(this->root);
			//iterate the MyQueue elements
			while (this->head != NULL)
			{
				//Get current head element of MyQueue
				auxiliary = this->head->element;
				if (auxiliary->left != NULL)
				{
					//Add new left child node
					this->enqueue(auxiliary->left);
				}
				if (auxiliary->right != NULL)
				{
					//Add new right child node
					this->enqueue(auxiliary->right);
				}
				//unlink left subtree
				auxiliary->left = NULL;
				this->dequeue();
				if (this->head != NULL)
				{
					auxiliary->right = this->head->element;
				}
				else
				{
					auxiliary->right = NULL;
				}
			}
		}
		else
		{
			cout << "Empty Linked List\n";
		}
	}
};
int main()
{
	//Make object of Binary Tree
	BinaryTree obj = BinaryTree();
	/*
			Construct Binary Tree
			---------------------
			       1
			     /   \
			    2     3
			   /     / \
			  4     5   6
			 /     / \   \
			7     8   9   11
			*/
	//Add nodes
	obj.root = new Node(1);
	obj.root->left = new Node(2);
	obj.root->right = new Node(3);
	obj.root->right->right = new Node(6);
	obj.root->right->right->right = new Node(11);
	obj.root->right->left = new Node(5);
	obj.root->left->left = new Node(4);
	obj.root->left->left->left = new Node(7);
	obj.root->right->left->left = new Node(8);
	obj.root->right->left->right = new Node(9);
	//Transform tree to level order
	obj.flatten_level_order();
	obj.show_element();
	return 0;
}

Output

Flatten Tree Node in preorder :
  1  2  3  4  5  6  7  8  9  11
<?php
/* 
  Php Program 
  Flatten binary tree nodes in level order from
*/
//Node of Binary Tree
class Node
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
//Define Custom queue
class MyQueue
{
	public $element;
	public $next;

	function __construct($element)
	{
		$this->element = $element;
		$this->next = null;
	}
}
class BinaryTree
{
	public $root;
	public $head;
	public $tail;

	function __construct()
	{
		//Set initial tree root to null
		$this->root = null;
		//Set queue is empty
		$this->head = null;
		$this->tail = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	public	function preorder($node)
	{
		if ($node != null)
		{
			//Print node value
			echo "  ". $node->data;
			$this->preorder($node->left);
			$this->preorder($node->right);
		}
	}
	//Create queue node
	public	function enqueue($element)
	{
		$new_node = new MyQueue($element);
		if ($this->head == null)
		{
			$this->head = $new_node;
			$this->tail = $new_node;
		}
		else
		{
			$this->tail->next = $new_node;
			$this->tail = $new_node;
		}
	}
	//Remove a dequeue element
	public	function dequeue()
	{
		if ($this->head != null)
		{
			$temp = $this->head;
			if ($this->head == $this->tail)
			{
				$this->tail = null;
			}
			$this->head = $this->head->next;
			//remove node
			$temp->element = null;
			$temp->next = null;
			$temp = null;
		}
	}
	//Display flatten elements of tree
	public	function show_element()
	{
		if ($this->root == null)
		{
			echo "\n Empty Tree\n";
			return;
		}
		echo "Flatten Tree Node in preorder : \n";
		//Start to root node
		$temp = $this->root;
		//Iterate tree elements
		while ($temp != null)
		{
			//Display node value
			echo "  ". $temp->data;
			//visit to next node
			$temp = $temp->right;
		}
		echo "\n";
	}
	//Transform tree nodes into its level order
	public	function flatten_level_order()
	{
		if ($this->root != null)
		{
			//Get first node of tree
			$auxiliary = $this->root;
			//Add first node into MyQueue
			$this->enqueue($this->root);
			//iterate the MyQueue elements
			while ($this->head != null)
			{
				//Get current head element of MyQueue
				$auxiliary = $this->head->element;
				if ($auxiliary->left != null)
				{
					//Add new left child node
					$this->enqueue($auxiliary->left);
				}
				if ($auxiliary->right != null)
				{
					//Add new right child node
					$this->enqueue($auxiliary->right);
				}
				//unlink left subtree
				$auxiliary->left = null;
				$this->dequeue();
				if ($this->head != null)
				{
					$auxiliary->right = $this->head->element;
				}
				else
				{
					$auxiliary->right = null;
				}
			}
		}
		else
		{
			echo "Empty Linked List\n";
		}
	}
}

function main()
{
	//Make object of Binary Tree
	$obj = new BinaryTree();
	/*  
			Construct Binary Tree
			---------------------
			       1
			     /   \
			    2     3
			   /     / \
			  4     5   6
			 /     / \   \
			7     8   9   11
			*/
	//Add nodes
	$obj->root = new Node(1);
	$obj->root->left = new Node(2);
	$obj->root->right = new Node(3);
	$obj->root->right->right = new Node(6);
	$obj->root->right->right->right = new Node(11);
	$obj->root->right->left = new Node(5);
	$obj->root->left->left = new Node(4);
	$obj->root->left->left->left = new Node(7);
	$obj->root->right->left->left = new Node(8);
	$obj->root->right->left->right = new Node(9);
	//Transform tree to level order
	$obj->flatten_level_order();
	$obj->show_element();
}
main();

Output

Flatten Tree Node in preorder :
  1  2  3  4  5  6  7  8  9  11
//Include namespace system
using System;

/* 
  C# Program 
  Flatten binary tree nodes in level order from
*/

//Node of Binary Tree
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;
	}
}
//Define Custom queue
class MyQueue
{
	public Node element;
	public MyQueue next;
	public MyQueue(Node element)
	{
		this.element = element;
		this.next = null;
	}
}
class BinaryTree
{
	public Node root;
	public MyQueue head;
	public MyQueue tail;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
		//Set queue is empty
		this.head = null;
		this.tail = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	public void preorder(Node node)
	{
		if (node != null)
		{
			//Print node value
			Console.Write("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	//Create queue node
	public void enqueue(Node element)
	{
		MyQueue new_node = new MyQueue(element);
		if (this.head == null)
		{
			this.head = new_node;
			this.tail = new_node;
		}
		else
		{
			this.tail.next = new_node;
			this.tail = new_node;
		}
	}
	//Remove a dequeue element
	public void dequeue()
	{
		if (this.head != null)
		{
			MyQueue temp = this.head;
			if (this.head == this.tail)
			{
				this.tail = null;
			}
			this.head = this.head.next;
			//remove node
			temp.element = null;
			temp.next = null;
			temp = null;
		}
	}
	//Display flatten elements of tree
	public void show_element()
	{
		if (this.root == null)
		{
			Console.Write("\n Empty Tree\n");
			return;
		}
		Console.Write("Flatten Tree Node in preorder : \n");
		//Start to root node
		Node temp = this.root;
		//Iterate tree elements
		while (temp != null)
		{
			//Display node value
			Console.Write("  " + temp.data);
			//visit to next node
			temp = temp.right;
		}
		Console.Write("\n");
	}
	//Transform tree nodes into its level order
	public void flatten_level_order()
	{
		if (this.root != null)
		{
			//Get first node of tree
			Node auxiliary = this.root;
			//Add first node into MyQueue
			this.enqueue(this.root);
			//iterate the MyQueue elements
			while (this.head != null)
			{
				//Get current head element of MyQueue
				auxiliary = this.head.element;
				if (auxiliary.left != null)
				{
					//Add new left child node
					this.enqueue(auxiliary.left);
				}
				if (auxiliary.right != null)
				{
					//Add new right child node
					this.enqueue(auxiliary.right);
				}
				//unlink left subtree
				auxiliary.left = null;
				this.dequeue();
				if (this.head != null)
				{
					auxiliary.right = this.head.element;
				}
				else
				{
					auxiliary.right = null;
				}
			}
		}
		else
		{
			Console.Write("Empty Linked List\n");
		}
	}
	public static void Main(String[] args)
	{
		//Make object of Binary Tree
		BinaryTree obj = new BinaryTree();
		/*  
				Construct Binary Tree
				---------------------
				       1
				     /   \
				    2     3
				   /     / \
				  4     5   6
				 /     / \   \
				7     8   9   11
				*/
		//Add nodes
		obj.root = new Node(1);
		obj.root.left = new Node(2);
		obj.root.right = new Node(3);
		obj.root.right.right = new Node(6);
		obj.root.right.right.right = new Node(11);
		obj.root.right.left = new Node(5);
		obj.root.left.left = new Node(4);
		obj.root.left.left.left = new Node(7);
		obj.root.right.left.left = new Node(8);
		obj.root.right.left.right = new Node(9);
		//Transform tree to level order
		obj.flatten_level_order();
		obj.show_element();
	}
}

Output

Flatten Tree Node in preorder :
  1  2  3  4  5  6  7  8  9  11
/* 
  Node Js Program 
  Flatten binary tree nodes in level order from
*/

//Node of Binary Tree
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
//Define Custom queue
class MyQueue
{
	constructor(element)
	{
		this.element = element;
		this.next = null;
	}
}
class BinaryTree
{
	constructor()
	{
		//Set initial tree root to null
		this.root = null;
		//Set queue is empty
		this.head = null;
		this.tail = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	preorder(node)
	{
		if (node != null)
		{
			//Print node value
			process.stdout.write("  " + node.data);
			this.preorder(node.left);
			this.preorder(node.right);
		}
	}
	//Create queue node
	enqueue(element)
	{
		var new_node = new MyQueue(element);
		if (this.head == null)
		{
			this.head = new_node;
			this.tail = new_node;
		}
		else
		{
			this.tail.next = new_node;
			this.tail = new_node;
		}
	}
	//Remove a dequeue element
	dequeue()
	{
		if (this.head != null)
		{
			var temp = this.head;
			if (this.head == this.tail)
			{
				this.tail = null;
			}
			this.head = this.head.next;
			//remove node
			temp.element = null;
			temp.next = null;
			temp = null;
		}
	}
	//Display flatten elements of tree
	show_element()
	{
		if (this.root == null)
		{
			process.stdout.write("\n Empty Tree\n");
			return;
		}
		process.stdout.write("Flatten Tree Node in preorder : \n");
		//Start to root node
		var temp = this.root;
		//Iterate tree elements
		while (temp != null)
		{
			//Display node value
			process.stdout.write("  " + temp.data);
			//visit to next node
			temp = temp.right;
		}
		process.stdout.write("\n");
	}
	//Transform tree nodes into its level order
	flatten_level_order()
	{
		if (this.root != null)
		{
			//Get first node of tree
			var auxiliary = this.root;
			//Add first node into MyQueue
			this.enqueue(this.root);
			//iterate the MyQueue elements
			while (this.head != null)
			{
				//Get current head element of MyQueue
				auxiliary = this.head.element;
				if (auxiliary.left != null)
				{
					//Add new left child node
					this.enqueue(auxiliary.left);
				}
				if (auxiliary.right != null)
				{
					//Add new right child node
					this.enqueue(auxiliary.right);
				}
				//unlink left subtree
				auxiliary.left = null;
				this.dequeue();
				if (this.head != null)
				{
					auxiliary.right = this.head.element;
				}
				else
				{
					auxiliary.right = null;
				}
			}
		}
		else
		{
			process.stdout.write("Empty Linked List\n");
		}
	}
}

function main()
{
	//Make object of Binary Tree
	var obj = new BinaryTree();
	/*  
			Construct Binary Tree
			---------------------
			       1
			     /   \
			    2     3
			   /     / \
			  4     5   6
			 /     / \   \
			7     8   9   11
			*/
	//Add nodes
	obj.root = new Node(1);
	obj.root.left = new Node(2);
	obj.root.right = new Node(3);
	obj.root.right.right = new Node(6);
	obj.root.right.right.right = new Node(11);
	obj.root.right.left = new Node(5);
	obj.root.left.left = new Node(4);
	obj.root.left.left.left = new Node(7);
	obj.root.right.left.left = new Node(8);
	obj.root.right.left.right = new Node(9);
	//Transform tree to level order
	obj.flatten_level_order();
	obj.show_element();
}
main();

Output

Flatten Tree Node in preorder :
  1  2  3  4  5  6  7  8  9  11
#   Python 3 Program 
#   Flatten binary tree nodes in level order from

# Node of Binary Tree
class Node :
	
	def __init__(self, data) :
		# Set node value
		self.data = data
		self.left = None
		self.right = None
	

# Define Custom queue
class MyQueue :
	
	def __init__(self, element) :
		self.element = element
		self.next = None
	

class BinaryTree :
	
	def __init__(self) :
		# Set initial tree root to null
		self.root = None
		# Set queue is empty
		self.head = None
		self.tail = None
	
	# Recursive function
	# Display preorder view of binary tree
	def preorder(self, node) :
		if (node != None) :
			# Print node value
			print("  ", node.data, end = "")
			self.preorder(node.left)
			self.preorder(node.right)
		
	
	# Create queue node
	def enqueue(self, element) :
		new_node = MyQueue(element)
		if (self.head == None) :
			self.head = new_node
			self.tail = new_node
		else :
			self.tail.next = new_node
			self.tail = new_node
		
	
	# Remove a dequeue element
	def dequeue(self) :
		if (self.head != None) :
			temp = self.head
			if (self.head == self.tail) :
				self.tail = None
			
			self.head = self.head.next
			# remove node
			temp.element = None
			temp.next = None
			temp = None
		
	
	# Display flatten elements of tree
	def show_element(self) :
		if (self.root == None) :
			print("\n Empty Tree\n", end = "")
			return
		
		print("Flatten Tree Node in preorder : \n", end = "")
		# Start to root node
		temp = self.root
		# Iterate tree elements
		while (temp != None) :
			# Display node value
			print("  ", temp.data, end = "")
			# visit to next node
			temp = temp.right
		
		print("\n", end = "")
	
	# Transform tree nodes into its level order
	def flatten_level_order(self) :
		if (self.root != None) :
			# Get first node of tree
			auxiliary = self.root
			# Add first node into MyQueue
			self.enqueue(self.root)
			# iterate the MyQueue elements
			while (self.head != None) :
				# Get current head element of MyQueue
				auxiliary = self.head.element
				if (auxiliary.left != None) :
					# Add new left child node
					self.enqueue(auxiliary.left)
				
				if (auxiliary.right != None) :
					# Add new right child node
					self.enqueue(auxiliary.right)
				
				# unlink left subtree
				auxiliary.left = None
				self.dequeue()
				if (self.head != None) :
					auxiliary.right = self.head.element
				else :
					auxiliary.right = None
				
			
		else :
			print("Empty Linked List\n", end = "")
		
	

def main() :
	# Make object of Binary Tree
	obj = BinaryTree()
	#   
	# 		Construct Binary Tree
	# 		---------------------
	# 		       1
	# 		     /   \
	# 		    2     3
	# 		   /     / \
	# 		  4     5   6
	# 		 /     / \   \
	# 		7     8   9   11
	# 		
	
	# Add nodes
	obj.root = Node(1)
	obj.root.left = Node(2)
	obj.root.right = Node(3)
	obj.root.right.right = Node(6)
	obj.root.right.right.right = Node(11)
	obj.root.right.left = Node(5)
	obj.root.left.left = Node(4)
	obj.root.left.left.left = Node(7)
	obj.root.right.left.left = Node(8)
	obj.root.right.left.right = Node(9)
	# Transform tree to level order
	obj.flatten_level_order()
	obj.show_element()

if __name__ == "__main__": main()

Output

Flatten Tree Node in preorder :
   1   2   3   4   5   6   7   8   9   11
#   Ruby Program 
#   Flatten binary tree nodes in level order from

# Node of Binary Tree
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
# Define Custom queue
class MyQueue  
	# Define the accessor and reader of class MyQueue  
	attr_reader :element, :next
	attr_accessor :element, :next
 
	
	def initialize(element) 
		self.element = element
		self.next = nil
	end
end
class BinaryTree  
	# Define the accessor and reader of class BinaryTree  
	attr_reader :root, :head, :tail
	attr_accessor :root, :head, :tail
 
	
	def initialize() 
		# Set initial tree root to null
		self.root = nil
		# Set queue is empty
		self.head = nil
		self.tail = nil
	end
    
	# Recursive function
	# Display preorder view of binary tree
	def preorder(node) 
		if (node != nil) 
			# Print node value
			print("  ", node.data)
			self.preorder(node.left)
			self.preorder(node.right)
		end
	end
    
	# Create queue node
	def enqueue(element) 
		new_node = MyQueue.new(element)
		if (self.head == nil) 
			self.head = new_node
			self.tail = new_node
		else 
			self.tail.next = new_node
			self.tail = new_node
		end
	end
    
	# Remove a dequeue element
	def dequeue() 
		if (self.head != nil) 
			temp = self.head
			if (self.head == self.tail) 
				self.tail = nil
			end
			self.head = self.head.next
			# remove node
			temp.element = nil
			temp.next = nil
			temp = nil
		end
	end
    
	# Display flatten elements of tree
	def show_element() 
		if (self.root == nil) 
			print("\n Empty Tree\n")
			return
		end
		print("Flatten Tree Node in preorder : \n")
		# Start to root node
		temp = self.root
		# Iterate tree elements
		while (temp != nil) 
			# Display node value
			print("  ", temp.data)
			# visit to next node
			temp = temp.right
		end
		print("\n")
	end
    
	# Transform tree nodes into its level order
	def flatten_level_order() 
		if (self.root != nil) 
			# Get first node of tree
			auxiliary = self.root
			# Add first node into MyQueue
			self.enqueue(self.root)
			# iterate the MyQueue elements
			while (self.head != nil) 
				# Get current head element of MyQueue
				auxiliary = self.head.element
				if (auxiliary.left != nil) 
					# Add new left child node
					self.enqueue(auxiliary.left)
				end
				if (auxiliary.right != nil) 
					# Add new right child node
					self.enqueue(auxiliary.right)
				end
				# unlink left subtree
				auxiliary.left = nil
				self.dequeue()
				if (self.head != nil) 
					auxiliary.right = self.head.element
				else 
					auxiliary.right = nil
				end
			end
		else 
			print("Empty Linked List\n")
		end
	end
end
def main() 
	# Make object of Binary Tree
	obj = BinaryTree.new()
	#   
	# 		Construct Binary Tree
	# 		---------------------
	# 		       1
	# 		     /   \
	# 		    2     3
	# 		   /     / \
	# 		  4     5   6
	# 		 /     / \   \
	# 		7     8   9   11
	# 		
	
	# Add nodes
	obj.root = Node.new(1)
	obj.root.left = Node.new(2)
	obj.root.right = Node.new(3)
	obj.root.right.right = Node.new(6)
	obj.root.right.right.right = Node.new(11)
	obj.root.right.left = Node.new(5)
	obj.root.left.left = Node.new(4)
	obj.root.left.left.left = Node.new(7)
	obj.root.right.left.left = Node.new(8)
	obj.root.right.left.right = Node.new(9)
	# Transform tree to level order
	obj.flatten_level_order()
	obj.show_element()
end
main()

Output

Flatten Tree Node in preorder : 
  1  2  3  4  5  6  7  8  9  11
/* 
  Scala Program 
  Flatten binary tree nodes in level order from
*/

//Node of Binary Tree
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
//Define Custom queue
class MyQueue(var element: Node,
	var next: MyQueue)
{
	def this(element: Node)
	{
		this(element, null);
	}
}
class BinaryTree(var root: Node,
	var head: MyQueue,
		var tail: MyQueue)
{
	def this()
	{
		this(null, null, null);
	}
	//Recursive function
	//Display preorder view of binary tree
	def preorder(node: Node): Unit = {
		if (node != null)
		{
			//Print node value
			print("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	//Create queue node
	def enqueue(element: Node): Unit = {
		var new_node: MyQueue = new MyQueue(element);
		if (this.head == null)
		{
			this.head = new_node;
			this.tail = new_node;
		}
		else
		{
			this.tail.next = new_node;
			this.tail = new_node;
		}
	}
	//Remove a dequeue element
	def dequeue(): Unit = {
		if (this.head != null)
		{
			var temp: MyQueue = this.head;
			if (this.head == this.tail)
			{
				this.tail = null;
			}
			this.head = this.head.next;
			//remove node
			temp.element = null;
			temp.next = null;
			temp = null;
		}
	}
	//Display flatten elements of tree
	def show_element(): Unit = {
		if (this.root == null)
		{
			print("\n Empty Tree\n");
			return;
		}
		print("Flatten Tree Node in preorder : \n");
		//Start to root node
		var temp: Node = this.root;
		//Iterate tree elements
		while (temp != null)
		{
			//Display node value
			print("  " + temp.data);
			//visit to next node
			temp = temp.right;
		}
		print("\n");
	}
	//Transform tree nodes into its level order
	def flatten_level_order(): Unit = {
		if (this.root != null)
		{
			//Get first node of tree
			var auxiliary: Node = this.root;
			//Add first node into MyQueue
			this.enqueue(this.root);
			//iterate the MyQueue elements
			while (this.head != null)
			{
				//Get current head element of MyQueue
				auxiliary = this.head.element;
				if (auxiliary.left != null)
				{
					//Add new left child node
					this.enqueue(auxiliary.left);
				}
				if (auxiliary.right != null)
				{
					//Add new right child node
					this.enqueue(auxiliary.right);
				}
				//unlink left subtree
				auxiliary.left = null;
				this.dequeue();
				if (this.head != null)
				{
					auxiliary.right = this.head.element;
				}
				else
				{
					auxiliary.right = null;
				}
			}
		}
		else
		{
			print("Empty Linked List\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		//Make object of Binary Tree
		var obj: BinaryTree = new BinaryTree();
		/*  
				Construct Binary Tree
				---------------------
				       1
				     /   \
				    2     3
				   /     / \
				  4     5   6
				 /     / \   \
				7     8   9   11
				*/
		//Add nodes
		obj.root = new Node(1);
		obj.root.left = new Node(2);
		obj.root.right = new Node(3);
		obj.root.right.right = new Node(6);
		obj.root.right.right.right = new Node(11);
		obj.root.right.left = new Node(5);
		obj.root.left.left = new Node(4);
		obj.root.left.left.left = new Node(7);
		obj.root.right.left.left = new Node(8);
		obj.root.right.left.right = new Node(9);
		//Transform tree to level order
		obj.flatten_level_order();
		obj.show_element();
	}
}

Output

Flatten Tree Node in preorder :
  1  2  3  4  5  6  7  8  9  11
/* 
  Swift 4 Program 
  Flatten binary tree nodes in level order from
*/

//Node of Binary Tree
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;
	}
}
//Define Custom queue
class MyQueue
{
	var element: Node? ;
	var next: MyQueue? ;
	init(_ element: Node? )
	{
		self.element = element;
		self.next = nil;
	}
}
class BinaryTree
{
	var root: Node? ;
	var head: MyQueue? ;
	var tail: MyQueue? ;
	init()
	{
		//Set initial tree root to null
		self.root = nil;
		//Set queue is empty
		self.head = nil;
		self.tail = nil;
	}
	//Recursive function
	//Display preorder view of binary tree
	func preorder(_ node: Node? )
	{
		if (node != nil)
		{
			//Print node value
			print("  ", node!.data, terminator: "");
			self.preorder(node!.left);
			self.preorder(node!.right);
		}
	}
	//Create queue node
	func enqueue(_ element: Node? )
	{
		let new_node: MyQueue? = MyQueue(element);
		if (self.head == nil)
		{
			self.head = new_node;
			self.tail = new_node;
		}
		else
		{
			self.tail!.next = new_node;
			self.tail = new_node;
		}
	}
	//Remove a dequeue element
	func dequeue()
	{
		if (self.head != nil)
		{
			var temp: MyQueue? = self.head;
			if (self.head === self.tail)
			{
				self.tail = nil;
			}
			self.head = self.head!.next;
			//remove node
			temp!.element = nil;
			temp!.next = nil;
			temp = nil;
		}
	}
	//Display flatten elements of tree
	func show_element()
	{
		if (self.root == nil)
		{
			print("\n Empty Tree\n", terminator: "");
			return;
		}
		print("Flatten Tree Node in preorder : \n", terminator: "");
		//Start to root node
		var temp: Node? = self.root;
		//Iterate tree elements
		while (temp != nil)
		{
			//Display node value
			print("  ", temp!.data, terminator: "");
			//visit to next node
			temp = temp!.right;
		}
		print("\n", terminator: "");
	}
	//Transform tree nodes into its level order
	func flatten_level_order()
	{
		if (self.root != nil)
		{
			//Get first node of tree
			var auxiliary: Node? = self.root;
			//Add first node into MyQueue
			self.enqueue(self.root);
			//iterate the MyQueue elements
			while (self.head != nil)
			{
				//Get current head element of MyQueue
				auxiliary = self.head!.element;
				if (auxiliary!.left != nil)
				{
					//Add new left child node
					self.enqueue(auxiliary!.left);
				}
				if (auxiliary!.right != nil)
				{
					//Add new right child node
					self.enqueue(auxiliary!.right);
				}
				//unlink left subtree
				auxiliary!.left = nil;
				self.dequeue();
				if (self.head != nil)
				{
					auxiliary!.right = self.head!.element;
				}
				else
				{
					auxiliary!.right = nil;
				}
			}
		}
		else
		{
			print("Empty Linked List\n", terminator: "");
		}
	}
}
func main()
{
	//Make object of Binary Tree
	let obj: BinaryTree = BinaryTree();
	/*  
			Construct Binary Tree
			---------------------
			       1
			     /   \
			    2     3
			   /     / \
			  4     5   6
			 /     / \   \
			7     8   9   11
			*/
	//Add nodes
	obj.root = Node(1);
	obj.root!.left = Node(2);
	obj.root!.right = Node(3);
	obj.root!.right!.right = Node(6);
	obj.root!.right!.right!.right = Node(11);
	obj.root!.right!.left = Node(5);
	obj.root!.left!.left = Node(4);
	obj.root!.left!.left!.left = Node(7);
	obj.root!.right!.left!.left = Node(8);
	obj.root!.right!.left!.right = Node(9);
	//Transform tree to level order
	obj.flatten_level_order();
	obj.show_element();
}
main();

Output

Flatten Tree Node in preorder :
   1   2   3   4   5   6   7   8   9   11

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