Write a program that converts a given tree to its Double tree. To create Double tree of the given tree, create a new duplicate for each node, and insert the duplicate as the left child of the original node.
So the tree…
2
/ \
1 3
is changed to…
2
/ \
2 3
/ /
1 3
/
1
And the tree
1
/ \
2 3
/ \
4 5
is changed to
1
/ \
1 3
/ /
2 3
/ \
2 5
/ /
4 5
/
4
Recursive approach: : Recursively convert the tree to double tree in postorder fashion. For each node, first convert the left subtree of the node, then right subtree, finally create a duplicate node of the node and fix the left child of the node and left child of left child.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node* left;
node* right;
};
node* newNode( int data);
void doubleTree(node* Node)
{
node* oldLeft;
if (Node == NULL) return ;
doubleTree(Node->left);
doubleTree(Node->right);
oldLeft = Node->left;
Node->left = newNode(Node->data);
Node->left->left = oldLeft;
}
node* newNode( int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
void printInorder(node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
cout << node->data << " " ;
printInorder(node->right);
}
int main()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << "Inorder traversal of the original tree is \n" ;
printInorder(root);
doubleTree(root);
cout << "\nInorder traversal of the double tree is \n" ;
printInorder(root);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode( int data);
void doubleTree( struct node* node)
{
struct node* oldLeft;
if (node==NULL) return ;
doubleTree(node->left);
doubleTree(node->right);
oldLeft = node->left;
node->left = newNode(node->data);
node->left->left = oldLeft;
}
struct node* newNode( int data)
{
struct node* node = ( struct node*)
malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
void printInorder( struct node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
printf ( "%d " , node->data);
printInorder(node->right);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf ( "Inorder traversal of the original tree is \n" );
printInorder(root);
doubleTree(root);
printf ( "\n Inorder traversal of the double tree is \n" );
printInorder(root);
getchar ();
return 0;
}
|
Java
class Node
{
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree
{
Node root;
void doubleTree(Node node)
{
Node oldleft;
if (node == null )
return ;
doubleTree(node.left);
doubleTree(node.right);
oldleft = node.left;
node.left = new Node(node.data);
node.left.left = oldleft;
}
void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 1 );
tree.root.left = new Node( 2 );
tree.root.right = new Node( 3 );
tree.root.left.left = new Node( 4 );
tree.root.left.right = new Node( 5 );
System.out.println( "Original tree is : " );
tree.printInorder(tree.root);
tree.doubleTree(tree.root);
System.out.println( "" );
System.out.println( "Inorder traversal of double tree is : " );
tree.printInorder(tree.root);
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self .left = None
self .right = None
def doubleTree(node) :
if node = = None :
return
doubleTree(node.left)
doubleTree(node.right)
oldleft = node.left
node.left = Node(node.data)
node.left.left = oldleft
def printInorder(node) :
if node = = None :
return
printInorder(node.left)
print (node.data,end = ' ' )
printInorder(node.right)
if __name__ = = '__main__' :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
print ( "Original tree is : " )
printInorder(root)
doubleTree(root)
print ()
print ( "Inorder traversal of double tree is : " )
printInorder(root)
|
C#
using System;
class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
public class BinaryTree
{
Node root;
void doubleTree(Node node)
{
Node oldleft;
if (node == null )
return ;
doubleTree(node.left);
doubleTree(node.right);
oldleft = node.left;
node.left = new Node(node.data);
node.left.left = oldleft;
}
void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
Console.Write(node.data + " " );
printInorder(node.right);
}
public static void Main(String []args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
Console.WriteLine( "Original tree is : " );
tree.printInorder(tree.root);
tree.doubleTree(tree.root);
Console.WriteLine( "" );
Console.WriteLine( "Inorder traversal of " +
"double tree is : " );
tree.printInorder(tree.root);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
var root;
function doubleTree(node)
{
var oldleft;
if (node == null )
return ;
doubleTree(node.left);
doubleTree(node.right);
oldleft = node.left;
node.left = new Node(node.data);
node.left.left = oldleft;
}
function printInorder(node)
{
if (node == null )
return ;
printInorder(node.left);
document.write(node.data + " " );
printInorder(node.right);
}
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
document.write( "Original tree is :<br/> " );
printInorder(root);
doubleTree(root);
document.write( "<br/>" );
document.write( "Inorder traversal of double tree is : <br/>" );
printInorder(root);
</script>
|
Output
Inorder traversal of the original tree is
4 2 5 1 3
Inorder traversal of the double tree is
4 4 2 2 5 5 1 1 3 3
Time Complexity: O(n) where n is the number of nodes in the tree.
Auxiliary Space: O(log(n))
Iterative approach:
The idea is to use a stack to iteratively traverse the binary tree in a depth-first manner. For each node encountered during the traversal, a new node is created as a duplicate of the current node, and the original left child of the current node is set as the left child of the new node. The new node is then set as the left child of the current node. This results in a binary tree that is a double tree of the original tree.
Follow the below steps to implement the above idea:
- If the given root is None, return.
- Create an empty stack and push the root onto the stack.
- While the stack is not empty, do the following:
a. Pop the top node from the stack.
b. If the node has a right child, push the right child onto the stack.
c. If the node has a left child, push the left child onto the stack.
d. Create a duplicate node of the current node and set it as the left child of the current node.
e. Set the original left child of the current node as the left child of the duplicate node.
- The tree is now converted to its double tree.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* left;
Node* right;
Node( int data)
{
this ->data = data;
left = right = NULL;
}
};
void doubleTree(Node* root)
{
if (!root) {
return ;
}
stack<Node*> s;
s.push(root);
while (!s.empty()) {
Node* node = s.top();
s.pop();
if (node->right) {
s.push(node->right);
}
if (node->left) {
s.push(node->left);
}
Node* old_left = node->left;
node->left = new Node(node->data);
node->left->left = old_left;
}
}
void printInorder(Node* root)
{
if (!root) {
return ;
}
printInorder(root->left);
cout << root->data << " " ;
printInorder(root->right);
}
int main()
{
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
cout << "Original tree is : " << endl;
printInorder(root);
cout << endl;
doubleTree(root);
cout << "Inorder traversal of double tree is : "
<< endl;
printInorder(root);
return 0;
}
|
Java
import java.util.Stack;
class Node {
public int data;
public Node left;
public Node right;
public Node( int data) {
this .data = data;
left = right = null ;
}
}
class GFG{
public static void doubleTree(Node root) {
if (root == null ) {
return ;
}
Stack<Node> s = new Stack<Node>();
s.push(root);
while (!s.empty()) {
Node node = s.pop();
if (node.right != null ) {
s.push(node.right);
}
if (node.left != null ) {
s.push(node.left);
}
Node old_left = node.left;
node.left = new Node(node.data);
node.left.left = old_left;
}
}
public static void printInorder(Node root) {
if (root == null ) {
return ;
}
printInorder(root.left);
System.out.print(root.data + " " );
printInorder(root.right);
}
public static void main(String[] args) {
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.left.left = new Node( 4 );
root.left.right = new Node( 5 );
System.out.println( "Original tree is : " );
printInorder(root);
System.out.println();
doubleTree(root);
System.out.println( "Inorder traversal of double tree is : " );
printInorder(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def doubleTree(root):
if not root:
return
stack = [root]
while stack:
node = stack.pop()
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
old_left = node.left
node.left = Node(node.data)
node.left.left = old_left
def printInorder(root):
if not root:
return
printInorder(root.left)
print (root.data, end = ' ' )
printInorder(root.right)
if __name__ = = '__main__' :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
print ( "Original tree is : " )
printInorder(root)
print ()
doubleTree(root)
print ( "Inorder traversal of double tree is : " )
printInorder(root)
|
C#
using System;
using System.Collections.Generic;
class Node
{
public int Data { get ; set ; }
public Node Left { get ; set ; }
public Node Right { get ; set ; }
public Node( int data)
{
Data = data;
Left = Right = null ;
}
}
class Program
{
static void DoubleTree(Node root)
{
if (root == null )
{
return ;
}
Stack<Node> stack = new Stack<Node>();
stack.Push(root);
while (stack.Count > 0)
{
Node node = stack.Pop();
if (node.Right != null )
{
stack.Push(node.Right);
}
if (node.Left != null )
{
stack.Push(node.Left);
}
Node oldLeft = node.Left;
node.Left = new Node(node.Data);
node.Left.Left = oldLeft;
}
}
static void PrintInorder(Node root)
{
if (root == null )
{
return ;
}
PrintInorder(root.Left);
Console.Write(root.Data + " " );
PrintInorder(root.Right);
}
static void Main()
{
Node root = new Node(1);
root.Left = new Node(2);
root.Right = new Node(3);
root.Left.Left = new Node(4);
root.Left.Right = new Node(5);
Console.WriteLine( "Original tree is : " );
PrintInorder(root);
Console.WriteLine();
DoubleTree(root);
Console.WriteLine( "Inorder traversal of double tree is : " );
PrintInorder(root);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function doubleTree(root) {
if (!root) {
return ;
}
const stack = [root];
while (stack.length > 0) {
const node = stack.pop();
if (node.right) {
stack.push(node.right);
}
if (node.left) {
stack.push(node.left);
}
const oldLeft = node.left;
node.left = new Node(node.data);
node.left.left = oldLeft;
}
}
function printInorder(root) {
if (!root) {
return ;
}
printInorder(root.left);
process.stdout.write(root.data + " " );
printInorder(root.right);
}
if (require.main === module) {
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
console.log( "Original tree is : " );
printInorder(root);
console.log();
doubleTree(root);
console.log( "Inorder traversal of double tree is : " );
printInorder(root);
}
|
Output
Original tree is :
4 2 5 1 3
Inorder traversal of double tree is :
4 4 2 2 5 5 1 1 3 3
Time Complexity: O(n), where n is the number of nodes in the tree
Auxiliary Space: O(h), where h is the height of the tree
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
Last Updated :
20 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...