Left-Right representation of a binary tree is standard representation where every node has a pointer to left child and another pointer to right child.
Down-Right representation is an alternate representation where every node has a pointer to left (or first) child and another pointer to next sibling. So siblings at every level are connected from left to right.
Given a binary tree in left-right representation as below
1
/ \
2 3
/ \
4 5
/ / \
6 7 8
Convert the structure of the tree to down-right representation like the below tree.
1
|
2 – 3
|
4 — 5
| |
6 7 – 8
The conversion should happen in-place, i.e., left child pointer should be used as down pointer and right child pointer should be used as right sibling pointer.
We strongly recommend to minimize your browser and try this yourself.
The idea is to first convert left and right children, then convert the root. Following is C++ implementation of the idea.
Algorithm:
- Define a struct for the binary tree node with integer key, and left and right child pointers.
- Define a recursive function convert that takes the root of the binary tree as input.
- In the convert function, check if the root is NULL. If it is, return.
- Recursively call convert on the left child of the root.
- Recursively call convert on the right child of the root.
- If the left child of the root is NULL, set the left child to be the right child.
- Otherwise, set the right child of the left child to be the right child.
- Set the right child of the root to be NULL.
- Define a utility function downRightTraversal that takes the root of the binary tree as input and traverses the tree in the down-right order.
- In the downRightTraversal function, if the root is not NULL, print the key of the root, then recursively call downRightTraversal on the right child of the root, and then recursively call downRightTraversal on the left child of the root.
- Define a utility function newNode that takes an integer key as input and returns a new binary tree node with that key and left and right child pointers set to NULL.
- In the main function, create a binary tree with the same structure as the one shown in the comment above the tree diagram in the code.
- Call the convert function on the root of the binary tree.
- Call the downRightTraversal function on the root of the binary tree to print the tree in down-right order.
- Return 0 to indicate successful program execution.
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
int key;
struct node *left, *right;
};
void convert(node *root)
{
if (root == NULL) return ;
convert(root->left);
convert(root->right);
if (root->left == NULL)
root->left = root->right;
else
root->left->right = root->right;
root->right = NULL;
}
void downRightTraversal(node *root)
{
if (root != NULL)
{
cout << root->key << " " ;
downRightTraversal(root->right);
downRightTraversal(root->left);
}
}
node* newNode( int key)
{
node *temp = new node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}
int main()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->right->left = newNode(4);
root->right->right = newNode(5);
root->right->left->left = newNode(6);
root->right->right->left = newNode(7);
root->right->right->right = newNode(8);
convert(root);
cout << "Traversal of the tree converted to down-right form\n" ;
downRightTraversal(root);
return 0;
}
|
Java
class GFG
{
static class node
{
int key;
node left, right;
node( int key)
{
this .key = key;
this .left = null ;
this .right = null ;
}
}
static void convert(node root)
{
if (root == null ) return ;
convert(root.left);
convert(root.right);
if (root.left == null )
root.left = root.right;
else
root.left.right = root.right;
root.right = null ;
}
static void downRightTraversal(node root)
{
if (root != null )
{
System.out.print(root.key + " " );
downRightTraversal(root.right);
downRightTraversal(root.left);
}
}
static node newNode( int key)
{
node temp = new node( 0 );
temp.key = key;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void main(String[] args)
{
node root = new node( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.right.left = newNode( 4 );
root.right.right = newNode( 5 );
root.right.left.left = newNode( 6 );
root.right.right.left = newNode( 7 );
root.right.right.right = newNode( 8 );
convert(root);
System.out.println( "Traversal of the tree " +
"converted to down-right form" );
downRightTraversal(root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def convert(root):
if (root = = None ):
return
convert(root.left)
convert(root.right)
if (root.left = = None ):
root.left = root.right
else :
root.left.right = root.right
root.right = None
def downRightTraversal(root):
if (root ! = None ):
print ( root.key, end = " " )
downRightTraversal(root.right)
downRightTraversal(root.left)
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.right.left = newNode( 4 )
root.right.right = newNode( 5 )
root.right.left.left = newNode( 6 )
root.right.right.left = newNode( 7 )
root.right.right.right = newNode( 8 )
convert(root)
print ( "Traversal of the tree converted" ,
"to down-right form" )
downRightTraversal(root)
|
C#
using System;
class GFG
{
public class node
{
public int key;
public node left, right;
public node( int key)
{
this .key = key;
this .left = null ;
this .right = null ;
}
}
public static void convert(node root)
{
if (root == null )
{
return ;
}
convert(root.left);
convert(root.right);
if (root.left == null )
{
root.left = root.right;
}
else
{
root.left.right = root.right;
}
root.right = null ;
}
public static void downRightTraversal(node root)
{
if (root != null )
{
Console.Write(root.key + " " );
downRightTraversal(root.right);
downRightTraversal(root.left);
}
}
public static node newNode( int key)
{
node temp = new node(0);
temp.key = key;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void Main( string [] args)
{
node root = new node(1);
root.left = newNode(2);
root.right = newNode(3);
root.right.left = newNode(4);
root.right.right = newNode(5);
root.right.left.left = newNode(6);
root.right.right.left = newNode(7);
root.right.right.right = newNode(8);
convert(root);
Console.WriteLine( "Traversal of the tree " +
"converted to down-right form" );
downRightTraversal(root);
}
}
|
Javascript
<script>
class node
{
constructor(key)
{
this .key = key;
this .left = null ;
this .right = null ;
}
}
function convert(root)
{
if (root == null )
{
return ;
}
convert(root.left);
convert(root.right);
if (root.left == null )
{
root.left = root.right;
}
else
{
root.left.right = root.right;
}
root.right = null ;
}
function downRightTraversal(root)
{
if (root != null )
{
document.write(root.key + " " );
downRightTraversal(root.right);
downRightTraversal(root.left);
}
}
function newNode(key)
{
var temp = new node(0);
temp.key = key;
temp.left = null ;
temp.right = null ;
return temp;
}
var root = new node(1);
root.left = newNode(2);
root.right = newNode(3);
root.right.left = newNode(4);
root.right.right = newNode(5);
root.right.left.left = newNode(6);
root.right.right.left = newNode(7);
root.right.right.right = newNode(8);
convert(root);
document.write( "Traversal of the tree " +
"converted to down-right form<br>" );
downRightTraversal(root);
</script>
|
OutputTraversal of the tree converted to down-right form
1 2 3 4 5 7 8 6
Time Complexity: O(n)
Space Complexity: O(n)