Skip to main content

Convert a binary tree into a circular doubly linked list in php

Php program for Convert a binary tree into a circular doubly linked list. Here more information.

<?php
/* 
  Php program for
  Convert binary tree to circular doubly linked list
*/
// 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;
	}
}
class BinaryTree
{
	public $root;
	public	function __construct()
	{
		$this->root = NULL;
	}
	// Reversibly converting a given tree to
	// Circular doubly linked list
	public	function changeLink($node, $l, $r)
	{
		if ($node != NULL)
		{
			// Visit left subtree with left and right node
			$this->changeLink($node->left, $l, $node);
			// Visit right subtree with left and right 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)
		{
			// When empty tree
			return;
		}
		// Change node link
		$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()
	{
		// Get first node
		$temp = $this->root;
		echo "\n Circular List element from front to rear\n";
		while ($temp != NULL)
		{
			// Display node value
			echo "  ".strval($temp->data);
			// Visit to right node
			$temp = $temp->right;
			if ($temp == $this->root)
			{
				// Stop execution
				$temp = NULL;
			}
		}
		echo "\n Circular List element from rear to front\n";
		// Get last node
		$temp = $this->root->left;
		while ($temp != NULL)
		{
			// Display node value
			echo "  ",$temp->data;
			// Visit to left node
			$temp = $temp->left;
			if ($temp == $this->root->left)
			{
				// Stop execution
				$temp = NULL;
			}
		}
	}
	public static function main($args)
	{
		// Create a binary tree
		$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);
		// Convert to circular doubly linked list
		$tree->convertToCll();
		// Display element
		$tree->displayData();
	}
}
BinaryTree::main(array());

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




Comment

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