Convert binary tree to circular doubly linked list

Here given code implementation process.

/*
    C Program 
    Convert binary tree to circular doubly linked list
*/
#include <stdio.h>
#include <stdlib.h>

// Tree Node
struct TreeNode
{
	int data;
	struct TreeNode *left;
	struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
	struct TreeNode *root;
};
// Create new tree
struct BinaryTree *newTree()
{
	// Create dynamic node
	struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
	if (tree != NULL)
	{
		tree->root = NULL;
	}
	else
	{
		printf("Memory Overflow to Create tree Tree\n");
	}
	//return new tree
	return tree;
}
// returns a new node of tree
struct TreeNode *newNode(int data)
{
	// Create dynamic node
	struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
	if (node != NULL)
	{
		//Set data and pointer values
		node->data = data;
		node->left = NULL;
		node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return node;
}
// Reversibly converting a given tree to threaded binary tree
void changeLink(struct TreeNode *node, struct TreeNode *l, struct TreeNode *r)
{
	if (node != NULL)
	{
		changeLink(node->left, l, node);
		changeLink(node->right, node, r);
		if (node->left == NULL)
		{
			// Set new leaf side node
			node->left = l;
			if (l != NULL)
			{
				// Because circular doubly linked list
				// So setup right side node
				l->right = node;
			}
		}
		if (node->right == NULL)
		{
			node->right = r;
			if (r != NULL)
			{
				// So setup left side node
				r->left = node;
			}
		}
	}
}
// Handles the request of convert binary tree into circular doubly linked list
void convertToCll(struct BinaryTree *tree)
{
	if (tree->root == NULL)
	{
		return;
	}
	changeLink(tree->root, NULL, NULL);
	struct TreeNode *temp = tree->root;
	// Setup first new node
	while (temp->left != NULL)
	{
		temp = temp->left;
	}
	// Make new root
	tree->root = temp;
	// Find last node
	while (temp->right != NULL)
	{
		temp = temp->right;
	}
	// Make circular linked list
	temp->right = tree->root;
	tree->root->left = temp;
}
// Display circular linked list elements
void displayData(struct BinaryTree *tree)
{
	struct TreeNode *temp = tree->root;
	printf("\n Circular List element from front to rear \n");
	while (temp != NULL)
	{
		printf("  %d", temp->data);
		temp = temp->right;
		if (temp == tree->root)
		{
			temp = NULL;
		}
	}
	printf("\n Circular List element from rear to front \n");
	temp = tree->root->left;
	while (temp != NULL)
	{
		printf("  %d", temp->data);
		temp = temp->left;
		if (temp == tree->root->left)
		{
			temp = NULL;
		}
	}
}
int main()
{
	// Define tree
	struct BinaryTree *tree = newTree();
	/*
	        -1
	       /   \
	      2     8
	     / \   / \
	    3  -3 6   5
	       /   \   \
	     -7     4  -6
	     /     / \
	    9     1  -2  
	         /
	        7 
	-----------------------
	      Binary Tree
	-----------------------
	*/
	tree->root = newNode(-1);
	tree->root->left = newNode(2);
	tree->root->right = newNode(8);
	tree->root->left->left = newNode(3);
	tree->root->left->right = newNode(-3);
	tree->root->left->right->left = newNode(-7);
	tree->root->left->right->left->left = newNode(9);
	tree->root->right->left = newNode(6);
	tree->root->right->right = newNode(5);
	tree->root->right->left->right = newNode(4);
	tree->root->right->right->right = newNode(-6);
	tree->root->right->left->right->left = newNode(1);
	tree->root->right->left->right->right = newNode(-2);
	tree->root->right->left->right->left->left = newNode(7);
	convertToCll(tree);
	displayData(tree);
	return 0;
}

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
/*
  Java Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Reversibly converting a given tree to threaded binary tree
	public void changeLink(TreeNode node, TreeNode l, TreeNode r)
	{
		if (node != null)
		{
			changeLink(node.left, l, node);
			changeLink(node.right, node, r);
			if (node.left == null)
			{
				// Set new leaf side node
				node.left = l;
				if (l != null)
				{
					// Because circular doubly linked list
					// So setup right side node
					l.right = node;
				}
			}
			if (node.right == null)
			{
				node.right = r;
				if (r != null)
				{
					// So setup left side node
					r.left = node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	public void convertToCll()
	{
		if (this.root == null)
		{
			return;
		}
		changeLink(this.root, null, null);
		TreeNode temp = this.root;
		// Setup first new node
		while (temp.left != null)
		{
			temp = temp.left;
		}
		// Make new root
		this.root = temp;
		// Find last node
		while (temp.right != null)
		{
			temp = temp.right;
		}
		// Make circular linked list
		temp.right = this.root;
		this.root.left = temp;
	}
	// Display circular linked list elements
	public void displayData()
	{
		TreeNode temp = this.root;
		System.out.print("\n Circular List element from front to rear \n");
		while (temp != null)
		{
			System.out.print("  " + temp.data);
			temp = temp.right;
			if (temp == this.root)
			{
				temp = null;
			}
		}
		System.out.print("\n Circular List element from rear to front \n");
		temp = this.root.left;
		while (temp != null)
		{
			System.out.print("  " + temp.data);
			temp = temp.left;
			if (temp == this.root.left)
			{
				temp = null;
			}
		}
	}
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		        -1
		       /   \
		      2     8
		     / \   / \
		    3  -3 6   5
		       /   \   \
		     -7     4  -6
		     /     / \
		    9     1  -2  
		         /
		        7 
		-----------------------
		      Binary Tree
		-----------------------
		*/
		tree.root = new TreeNode(-1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(-3);
		tree.root.left.right.left = new TreeNode(-7);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left.right = new TreeNode(4);
		tree.root.right.right.right = new TreeNode(-6);
		tree.root.right.left.right.left = new TreeNode(1);
		tree.root.right.left.right.right = new TreeNode(-2);
		tree.root.right.left.right.left.left = new TreeNode(7);
		tree.convertToCll();
		tree.displayData();
	}
}

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
class TreeNode
{
	public: int data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int data)
	{
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public: TreeNode *root;
	BinaryTree()
	{
		this->root = NULL;
	}
	// Reversibly converting a given tree to threaded binary tree
	void changeLink(TreeNode *node, TreeNode *l, TreeNode *r)
	{
		if (node != NULL)
		{
			this->changeLink(node->left, l, node);
			this->changeLink(node->right, node, r);
			if (node->left == NULL)
			{
				// Set new leaf side node
				node->left = l;
				if (l != NULL)
				{
					// Because circular doubly linked list
					// So setup right side node
					l->right = node;
				}
			}
			if (node->right == NULL)
			{
				node->right = r;
				if (r != NULL)
				{
					// So setup left side node
					r->left = node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	void convertToCll()
	{
		if (this->root == NULL)
		{
			return;
		}
		this->changeLink(this->root, NULL, NULL);
		TreeNode *temp = this->root;
		// Setup first new node
		while (temp->left != NULL)
		{
			temp = temp->left;
		}
		// Make new root
		this->root = temp;
		// Find last node
		while (temp->right != NULL)
		{
			temp = temp->right;
		}
		// Make circular linked list
		temp->right = this->root;
		this->root->left = temp;
	}
	// Display circular linked list elements
	void displayData()
	{
		TreeNode *temp = this->root;
		cout << "\n Circular List element from front to rear \n";
		while (temp != NULL)
		{
			cout << "  " << temp->data;
			temp = temp->right;
			if (temp == this->root)
			{
				temp = NULL;
			}
		}
		cout << "\n Circular List element from rear to front \n";
		temp = this->root->left;
		while (temp != NULL)
		{
			cout << "  " << temp->data;
			temp = temp->left;
			if (temp == this->root->left)
			{
				temp = NULL;
			}
		}
	}
};
int main()
{
	BinaryTree tree = BinaryTree();
	/*
	        -1
	       /   \
	      2     8
	     / \   / \
	    3  -3 6   5
	       /   \   \
	     -7     4  -6
	     /     / \
	    9     1  -2  
	         /
	        7 
	-----------------------
	      Binary Tree
	-----------------------
	*/
	tree.root = new TreeNode(-1);
	tree.root->left = new TreeNode(2);
	tree.root->right = new TreeNode(8);
	tree.root->left->left = new TreeNode(3);
	tree.root->left->right = new TreeNode(-3);
	tree.root->left->right->left = new TreeNode(-7);
	tree.root->left->right->left->left = new TreeNode(9);
	tree.root->right->left = new TreeNode(6);
	tree.root->right->right = new TreeNode(5);
	tree.root->right->left->right = new TreeNode(4);
	tree.root->right->right->right = new TreeNode(-6);
	tree.root->right->left->right->left = new TreeNode(1);
	tree.root->right->left->right->right = new TreeNode(-2);
	tree.root->right->left->right->left->left = new TreeNode(7);
	tree.convertToCll();
	tree.displayData();
	return 0;
}

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
// Include namespace system
using System;
/*
  C# Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
public class TreeNode
{
	public int data;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public TreeNode root;
	public BinaryTree()
	{
		this.root = null;
	}
	// Reversibly converting a given tree to threaded binary tree
	public void changeLink(TreeNode node, TreeNode l, TreeNode r)
	{
		if (node != null)
		{
			changeLink(node.left, l, node);
			changeLink(node.right, node, r);
			if (node.left == null)
			{
				// Set new leaf side node
				node.left = l;
				if (l != null)
				{
					// Because circular doubly linked list
					// So setup right side node
					l.right = node;
				}
			}
			if (node.right == null)
			{
				node.right = r;
				if (r != null)
				{
					// So setup left side node
					r.left = node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	public void convertToCll()
	{
		if (this.root == null)
		{
			return;
		}
		changeLink(this.root, null, null);
		TreeNode temp = this.root;
		// Setup first new node
		while (temp.left != null)
		{
			temp = temp.left;
		}
		// Make new root
		this.root = temp;
		// Find last node
		while (temp.right != null)
		{
			temp = temp.right;
		}
		// Make circular linked list
		temp.right = this.root;
		this.root.left = temp;
	}
	// Display circular linked list elements
	public void displayData()
	{
		TreeNode temp = this.root;
		Console.Write("\n Circular List element from front to rear \n");
		while (temp != null)
		{
			Console.Write("  " + temp.data);
			temp = temp.right;
			if (temp == this.root)
			{
				temp = null;
			}
		}
		Console.Write("\n Circular List element from rear to front \n");
		temp = this.root.left;
		while (temp != null)
		{
			Console.Write("  " + temp.data);
			temp = temp.left;
			if (temp == this.root.left)
			{
				temp = null;
			}
		}
	}
	public static void Main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		        -1
		       /   \
		      2     8
		     / \   / \
		    3  -3 6   5
		       /   \   \
		     -7     4  -6
		     /     / \
		    9     1  -2  
		         /
		        7 
		-----------------------
		      Binary Tree
		-----------------------
		*/
		tree.root = new TreeNode(-1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(-3);
		tree.root.left.right.left = new TreeNode(-7);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left.right = new TreeNode(4);
		tree.root.right.right.right = new TreeNode(-6);
		tree.root.right.left.right.left = new TreeNode(1);
		tree.root.right.left.right.right = new TreeNode(-2);
		tree.root.right.left.right.left.left = new TreeNode(7);
		tree.convertToCll();
		tree.displayData();
	}
}

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
<?php
/*
  Php Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
class TreeNode
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinaryTree
{
	public $root;

	function __construct()
	{
		$this->root = null;
	}
	// Reversibly converting a given tree to threaded binary tree
	public	function changeLink($node, $l, $r)
	{
		if ($node != null)
		{
			$this->changeLink($node->left, $l, $node);
			$this->changeLink($node->right, $node, $r);
			if ($node->left == null)
			{
				// Set new leaf side node
				$node->left = $l;
				if ($l != null)
				{
					// Because circular doubly linked list
					// So setup right side node
					$l->right = $node;
				}
			}
			if ($node->right == null)
			{
				$node->right = $r;
				if ($r != null)
				{
					// So setup left side node
					$r->left = $node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	public	function convertToCll()
	{
		if ($this->root == null)
		{
			return;
		}
		$this->changeLink($this->root, null, null);
		$temp = $this->root;
		// Setup first new node
		while ($temp->left != null)
		{
			$temp = $temp->left;
		}
		// Make new root
		$this->root = $temp;
		// Find last node
		while ($temp->right != null)
		{
			$temp = $temp->right;
		}
		// Make circular linked list
		$temp->right = $this->root;
		$this->root->left = $temp;
	}
	// Display circular linked list elements
	public	function displayData()
	{
		$temp = $this->root;
		echo "\n Circular List element from front to rear \n";
		while ($temp != null)
		{
			echo "  ". $temp->data;
			$temp = $temp->right;
			if ($temp == $this->root)
			{
				$temp = null;
			}
		}
		echo "\n Circular List element from rear to front \n";
		$temp = $this->root->left;
		while ($temp != null)
		{
			echo "  ". $temp->data;
			$temp = $temp->left;
			if ($temp == $this->root->left)
			{
				$temp = null;
			}
		}
	}
}

function main()
{
	$tree = new BinaryTree();
	/*
	        -1
	       /   \
	      2     8
	     / \   / \
	    3  -3 6   5
	       /   \   \
	     -7     4  -6
	     /     / \
	    9     1  -2  
	         /
	        7 
	-----------------------
	      Binary Tree
	-----------------------
	*/
	$tree->root = new TreeNode(-1);
	$tree->root->left = new TreeNode(2);
	$tree->root->right = new TreeNode(8);
	$tree->root->left->left = new TreeNode(3);
	$tree->root->left->right = new TreeNode(-3);
	$tree->root->left->right->left = new TreeNode(-7);
	$tree->root->left->right->left->left = new TreeNode(9);
	$tree->root->right->left = new TreeNode(6);
	$tree->root->right->right = new TreeNode(5);
	$tree->root->right->left->right = new TreeNode(4);
	$tree->root->right->right->right = new TreeNode(-6);
	$tree->root->right->left->right->left = new TreeNode(1);
	$tree->root->right->left->right->right = new TreeNode(-2);
	$tree->root->right->left->right->left->left = new TreeNode(7);
	$tree->convertToCll();
	$tree->displayData();
}
main();

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
/*
  Node Js Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
class TreeNode
{
	constructor(data)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		this.root = null;
	}
	// Reversibly converting a given tree to threaded binary tree
	changeLink(node, l, r)
	{
		if (node != null)
		{
			this.changeLink(node.left, l, node);
			this.changeLink(node.right, node, r);
			if (node.left == null)
			{
				// Set new leaf side node
				node.left = l;
				if (l != null)
				{
					// Because circular doubly linked list
					// So setup right side node
					l.right = node;
				}
			}
			if (node.right == null)
			{
				node.right = r;
				if (r != null)
				{
					// So setup left side node
					r.left = node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	convertToCll()
	{
		if (this.root == null)
		{
			return;
		}
		this.changeLink(this.root, null, null);
		var temp = this.root;
		// Setup first new node
		while (temp.left != null)
		{
			temp = temp.left;
		}
		// Make new root
		this.root = temp;
		// Find last node
		while (temp.right != null)
		{
			temp = temp.right;
		}
		// Make circular linked list
		temp.right = this.root;
		this.root.left = temp;
	}
	// Display circular linked list elements
	displayData()
	{
		var temp = this.root;
		process.stdout.write("\n Circular List element from front to rear \n");
		while (temp != null)
		{
			process.stdout.write("  " + temp.data);
			temp = temp.right;
			if (temp == this.root)
			{
				temp = null;
			}
		}
		process.stdout.write("\n Circular List element from rear to front \n");
		temp = this.root.left;
		while (temp != null)
		{
			process.stdout.write("  " + temp.data);
			temp = temp.left;
			if (temp == this.root.left)
			{
				temp = null;
			}
		}
	}
}

function main()
{
	var tree = new BinaryTree();
	/*
	        -1
	       /   \
	      2     8
	     / \   / \
	    3  -3 6   5
	       /   \   \
	     -7     4  -6
	     /     / \
	    9     1  -2  
	         /
	        7 
	-----------------------
	      Binary Tree
	-----------------------
	*/
	tree.root = new TreeNode(-1);
	tree.root.left = new TreeNode(2);
	tree.root.right = new TreeNode(8);
	tree.root.left.left = new TreeNode(3);
	tree.root.left.right = new TreeNode(-3);
	tree.root.left.right.left = new TreeNode(-7);
	tree.root.left.right.left.left = new TreeNode(9);
	tree.root.right.left = new TreeNode(6);
	tree.root.right.right = new TreeNode(5);
	tree.root.right.left.right = new TreeNode(4);
	tree.root.right.right.right = new TreeNode(-6);
	tree.root.right.left.right.left = new TreeNode(1);
	tree.root.right.left.right.right = new TreeNode(-2);
	tree.root.right.left.right.left.left = new TreeNode(7);
	tree.convertToCll();
	tree.displayData();
}
main();

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
#   Python 3 Program for
#   Convert binary tree to circular doubly linked list

#  Tree Node
class TreeNode :
	
	def __init__(self, data) :
		self.data = data
		self.left = None
		self.right = None
	

class BinaryTree :
	
	def __init__(self) :
		self.root = None
	
	#  Reversibly converting a given tree to threaded binary tree
	def changeLink(self, node, l, r) :
		if (node != None) :
			self.changeLink(node.left, l, node)
			self.changeLink(node.right, node, r)
			if (node.left == None) :
				#  Set new leaf side node
				node.left = l
				if (l != None) :
					#  Because circular doubly linked list
					#  So setup right side node
					l.right = node
				
			
			if (node.right == None) :
				node.right = r
				if (r != None) :
					#  So setup left side node
					r.left = node
				
			
		
	
	#  Handles the request of convert binary tree into circular doubly linked list
	def convertToCll(self) :
		if (self.root == None) :
			return
		
		self.changeLink(self.root, None, None)
		temp = self.root
		#  Setup first new node
		while (temp.left != None) :
			temp = temp.left
		
		#  Make new root
		self.root = temp
		#  Find last node
		while (temp.right != None) :
			temp = temp.right
		
		#  Make circular linked list
		temp.right = self.root
		self.root.left = temp
	
	#  Display circular linked list elements
	def displayData(self) :
		temp = self.root
		print("\n Circular List element from front to rear ")
		while (temp != None) :
			print("  ", temp.data, end = "")
			temp = temp.right
			if (temp == self.root) :
				temp = None
			
		
		print("\n Circular List element from rear to front ")
		temp = self.root.left
		while (temp != None) :
			print("  ", temp.data, end = "")
			temp = temp.left
			if (temp == self.root.left) :
				temp = None
			
		
	

def main() :
	tree = BinaryTree()
	# 
	#         -1
	#        /   \
	#       2     8
	#      / \   / \
	#     3  -3 6   5
	#        /   \   \
	#      -7     4  -6
	#      /     / \
	#     9     1  -2  
	#          /
	#         7 
	# -----------------------
	#       Binary Tree
	# -----------------------
	
	tree.root = TreeNode(-1)
	tree.root.left = TreeNode(2)
	tree.root.right = TreeNode(8)
	tree.root.left.left = TreeNode(3)
	tree.root.left.right = TreeNode(-3)
	tree.root.left.right.left = TreeNode(-7)
	tree.root.left.right.left.left = TreeNode(9)
	tree.root.right.left = TreeNode(6)
	tree.root.right.right = TreeNode(5)
	tree.root.right.left.right = TreeNode(4)
	tree.root.right.right.right = TreeNode(-6)
	tree.root.right.left.right.left = TreeNode(1)
	tree.root.right.left.right.right = TreeNode(-2)
	tree.root.right.left.right.left.left = TreeNode(7)
	tree.convertToCll()
	tree.displayData()

if __name__ == "__main__": main()

Output

 Circular List element from front to rear
   3   2   9   -7   -3   -1   6   7   1   4   -2   8   5   -6
 Circular List element from rear to front
   -6   5   8   -2   4   1   7   6   -1   -3   -7   9   2   3
#   Ruby Program for
#   Convert binary tree to circular doubly linked list

#  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) 
		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() 
		self.root = nil
	end

	#  Reversibly converting a given tree to threaded binary tree
	def changeLink(node, l, r) 
		if (node != nil) 
			self.changeLink(node.left, l, node)
			self.changeLink(node.right, node, r)
			if (node.left == nil) 
				#  Set new leaf side node
				node.left = l
				if (l != nil) 
					#  Because circular doubly linked list
					#  So setup right side node
					l.right = node
				end

			end

			if (node.right == nil) 
				node.right = r
				if (r != nil) 
					#  So setup left side node
					r.left = node
				end

			end

		end

	end

	#  Handles the request of convert binary tree into circular doubly linked list
	def convertToCll() 
		if (self.root == nil) 
			return
		end

		self.changeLink(self.root, nil, nil)
		temp = self.root
		#  Setup first new node
		while (temp.left != nil) 
			temp = temp.left
		end

		#  Make new root
		self.root = temp
		#  Find last node
		while (temp.right != nil) 
			temp = temp.right
		end

		#  Make circular linked list
		temp.right = self.root
		self.root.left = temp
	end

	#  Display circular linked list elements
	def displayData() 
		temp = self.root
		print("\n Circular List element from front to rear \n")
		while (temp != nil) 
			print("  ", temp.data)
			temp = temp.right
			if (temp == self.root) 
				temp = nil
			end

		end

		print("\n Circular List element from rear to front \n")
		temp = self.root.left
		while (temp != nil) 
			print("  ", temp.data)
			temp = temp.left
			if (temp == self.root.left) 
				temp = nil
			end

		end

	end

end

def main() 
	tree = BinaryTree.new()
	# 
	#         -1
	#        /   \
	#       2     8
	#      / \   / \
	#     3  -3 6   5
	#        /   \   \
	#      -7     4  -6
	#      /     / \
	#     9     1  -2  
	#          /
	#         7 
	# -----------------------
	#       Binary Tree
	# -----------------------
	
	tree.root = TreeNode.new(-1)
	tree.root.left = TreeNode.new(2)
	tree.root.right = TreeNode.new(8)
	tree.root.left.left = TreeNode.new(3)
	tree.root.left.right = TreeNode.new(-3)
	tree.root.left.right.left = TreeNode.new(-7)
	tree.root.left.right.left.left = TreeNode.new(9)
	tree.root.right.left = TreeNode.new(6)
	tree.root.right.right = TreeNode.new(5)
	tree.root.right.left.right = TreeNode.new(4)
	tree.root.right.right.right = TreeNode.new(-6)
	tree.root.right.left.right.left = TreeNode.new(1)
	tree.root.right.left.right.right = TreeNode.new(-2)
	tree.root.right.left.right.left.left = TreeNode.new(7)
	tree.convertToCll()
	tree.displayData()
end

main()

Output

 Circular List element from front to rear 
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front 
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
/*
  Scala Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinaryTree(var root: TreeNode)
{
	def this()
	{
		this(null);
	}
	// Reversibly converting a given tree to threaded binary tree
	def changeLink(node: TreeNode, l: TreeNode, r: TreeNode): Unit = {
		if (node != null)
		{
			this.changeLink(node.left, l, node);
			this.changeLink(node.right, node, r);
			if (node.left == null)
			{
				// Set new leaf side node
				node.left = l;
				if (l != null)
				{
					// Because circular doubly linked list
					// So setup right side node
					l.right = node;
				}
			}
			if (node.right == null)
			{
				node.right = r;
				if (r != null)
				{
					// So setup left side node
					r.left = node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	def convertToCll(): Unit = {
		if (this.root == null)
		{
			return;
		}
		this.changeLink(this.root, null, null);
		var temp: TreeNode = this.root;
		// Setup first new node
		while (temp.left != null)
		{
			temp = temp.left;
		}
		// Make new root
		this.root = temp;
		// Find last node
		while (temp.right != null)
		{
			temp = temp.right;
		}
		// Make circular linked list
		temp.right = this.root;
		this.root.left = temp;
	}
	// Display circular linked list elements
	def displayData(): Unit = {
		var temp: TreeNode = this.root;
		print("\n Circular List element from front to rear \n");
		while (temp != null)
		{
			print("  " + temp.data);
			temp = temp.right;
			if (temp == this.root)
			{
				temp = null;
			}
		}
		print("\n Circular List element from rear to front \n");
		temp = this.root.left;
		while (temp != null)
		{
			print("  " + temp.data);
			temp = temp.left;
			if (temp == this.root.left)
			{
				temp = null;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var tree: BinaryTree = new BinaryTree();
		/*
		        -1
		       /   \
		      2     8
		     / \   / \
		    3  -3 6   5
		       /   \   \
		     -7     4  -6
		     /     / \
		    9     1  -2  
		         /
		        7 
		-----------------------
		      Binary Tree
		-----------------------
		*/
		tree.root = new TreeNode(-1);
		tree.root.left = new TreeNode(2);
		tree.root.right = new TreeNode(8);
		tree.root.left.left = new TreeNode(3);
		tree.root.left.right = new TreeNode(-3);
		tree.root.left.right.left = new TreeNode(-7);
		tree.root.left.right.left.left = new TreeNode(9);
		tree.root.right.left = new TreeNode(6);
		tree.root.right.right = new TreeNode(5);
		tree.root.right.left.right = new TreeNode(4);
		tree.root.right.right.right = new TreeNode(-6);
		tree.root.right.left.right.left = new TreeNode(1);
		tree.root.right.left.right.right = new TreeNode(-2);
		tree.root.right.left.right.left.left = new TreeNode(7);
		tree.convertToCll();
		tree.displayData();
	}
}

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3
/*
  Swift 4 Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	init()
	{
		self.root = nil;
	}
	// Reversibly converting a given tree to threaded binary tree
	func changeLink(_ node: TreeNode? , _ l : TreeNode? , _ r : TreeNode? )
	{
		if (node  != nil)
		{
			self.changeLink(node!.left, l, node);
			self.changeLink(node!.right, node, r);
			if (node!.left == nil)
			{
				// Set new leaf side node
				node!.left = l;
				if (l  != nil)
				{
					// Because circular doubly linked list
					// So setup right side node
					l!.right = node;
				}
			}
			if (node!.right == nil)
			{
				node!.right = r;
				if (r  != nil)
				{
					// So setup left side node
					r!.left = node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	func convertToCll()
	{
		if (self.root == nil)
		{
			return;
		}
		self.changeLink(self.root, nil, nil);
		var temp: TreeNode? = self.root;
		// Setup first new node
		while (temp!.left  != nil)
		{
			temp = temp!.left;
		}
		// Make new root
		self.root = temp;
		// Find last node
		while (temp!.right  != nil)
		{
			temp = temp!.right;
		}
		// Make circular linked list
		temp!.right = self.root;
		self.root!.left = temp;
	}
	// Display circular linked list elements
	func displayData()
	{
		var temp: TreeNode? = self.root;
		print("\n Circular List element from front to rear ");
		while (temp  != nil)
		{
			print("  ", temp!.data, terminator: "");
			temp = temp!.right;
			if (temp === self.root)
			{
				temp = nil;
			}
		}
		print("\n Circular List element from rear to front ");
		temp = self.root!.left;
		while (temp  != nil)
		{
			print("  ", temp!.data, terminator: "");
			temp = temp!.left;
			if (temp === self.root!.left)
			{
				temp = nil;
			}
		}
	}
}
func main()
{
	let tree: BinaryTree = BinaryTree();
	/*
	        -1
	       /   \
	      2     8
	     / \   / \
	    3  -3 6   5
	       /   \   \
	     -7     4  -6
	     /     / \
	    9     1  -2  
	         /
	        7 
	-----------------------
	      Binary Tree
	-----------------------
	*/
	tree.root = TreeNode(-1);
	tree.root!.left = TreeNode(2);
	tree.root!.right = TreeNode(8);
	tree.root!.left!.left = TreeNode(3);
	tree.root!.left!.right = TreeNode(-3);
	tree.root!.left!.right!.left = TreeNode(-7);
	tree.root!.left!.right!.left!.left = TreeNode(9);
	tree.root!.right!.left = TreeNode(6);
	tree.root!.right!.right = TreeNode(5);
	tree.root!.right!.left!.right = TreeNode(4);
	tree.root!.right!.right!.right = TreeNode(-6);
	tree.root!.right!.left!.right!.left = TreeNode(1);
	tree.root!.right!.left!.right!.right = TreeNode(-2);
	tree.root!.right!.left!.right!.left!.left = TreeNode(7);
	tree.convertToCll();
	tree.displayData();
}
main();

Output

 Circular List element from front to rear
   3   2   9   -7   -3   -1   6   7   1   4   -2   8   5   -6
 Circular List element from rear to front
   -6   5   8   -2   4   1   7   6   -1   -3   -7   9   2   3
/*
  Kotlin Program for
  Convert binary tree to circular doubly linked list
*/
// Tree Node
class TreeNode
{
	var data: Int;
	var left: TreeNode ? ;
	var right: TreeNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	var root: TreeNode ? ;
	constructor()
	{
		this.root = null;
	}
	// Reversibly converting a given tree to threaded binary tree
	fun changeLink(node: TreeNode ? , l : TreeNode ? , r : TreeNode ? ): Unit
	{
		if (node != null)
		{
			this.changeLink(node.left, l, node);
			this.changeLink(node.right, node, r);
			if (node.left == null)
			{
				// Set new leaf side node
				node.left = l;
				if (l != null)
				{
					// Because circular doubly linked list
					// So setup right side node
					l.right = node;
				}
			}
			if (node.right == null)
			{
				node.right = r;
				if (r != null)
				{
					// So setup left side node
					r.left = node;
				}
			}
		}
	}
	// Handles the request of convert binary tree into circular doubly linked list
	fun convertToCll(): Unit
	{
		if (this.root == null)
		{
			return;
		}
		this.changeLink(this.root, null, null);
		var temp: TreeNode ? = this.root;
		// Setup first new node
		while (temp?.left != null)
		{
			temp = temp.left;
		}
		// Make new root
		this.root = temp;
		// Find last node
		while (temp?.right != null)
		{
			temp = temp.right;
		}
		// Make circular linked list
		temp?.right = this.root;
		this.root?.left = temp;
	}
	// Display circular linked list elements
	fun displayData(): Unit
	{
		var temp: TreeNode ? = this.root;
		print("\n Circular List element from front to rear \n");
		while (temp != null)
		{
			print("  " + temp.data);
			temp = temp.right;
			if (temp == this.root)
			{
				temp = null;
			}
		}
		print("\n Circular List element from rear to front \n");
		temp = this.root?.left;
		while (temp != null)
		{
			print("  " + temp.data);
			temp = temp.left;
			if (temp == this.root?.left)
			{
				temp = null;
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var tree: BinaryTree = BinaryTree();
	/*
	        -1
	       /   \
	      2     8
	     / \   / \
	    3  -3 6   5
	       /   \   \
	     -7     4  -6
	     /     / \
	    9     1  -2  
	         /
	        7 
	-----------------------
	      Binary Tree
	-----------------------
	*/
	tree.root = TreeNode(-1);
	tree.root?.left = TreeNode(2);
	tree.root?.right = TreeNode(8);
	tree.root?.left?.left = TreeNode(3);
	tree.root?.left?.right = TreeNode(-3);
	tree.root?.left?.right?.left = TreeNode(-7);
	tree.root?.left?.right?.left?.left = TreeNode(9);
	tree.root?.right?.left = TreeNode(6);
	tree.root?.right?.right = TreeNode(5);
	tree.root?.right?.left?.right = TreeNode(4);
	tree.root?.right?.right?.right = TreeNode(-6);
	tree.root?.right?.left?.right?.left = TreeNode(1);
	tree.root?.right?.left?.right?.right = TreeNode(-2);
	tree.root?.right?.left?.right?.left?.left = TreeNode(7);
	tree.convertToCll();
	tree.displayData();
}

Output

 Circular List element from front to rear
  3  2  9  -7  -3  -1  6  7  1  4  -2  8  5  -6
 Circular List element from rear to front
  -6  5  8  -2  4  1  7  6  -1  -3  -7  9  2  3


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved