An n-ary tree in computer science is a collection of nodes normally represented hierarchically in the following fashion.
- The tree starts at the root node.
- Each node of the tree holds a list of references to its child nodes.
- The number of children a node has is less than or equal to n.
A typical representation of n-ary tree uses an array of n references (or pointers) to store children (Note that n is an upper bound on number of children). Can we do better? the idea of Left-Child Right- Sibling representation is to store only two pointers in every node.
Left-Child Right Sibling Representation
It is a different representation of an n-ary tree where instead of holding a reference to each and every child node, a node holds just two references, first a reference to it’s first child, and the other to it’s immediate next sibling. This new transformation not only removes the need of advance knowledge of the number of children a node has, but also limits the number of references to a maximum of two, thereby making it so much easier to code. One thing to note is that in the previous representation a link between two nodes denoted a parent-child relationship whereas in this representation a link between two nodes may denote a parent-child relationship or a sibling-sibling relationship.
Advantages :
1. This representation saves up memory by limiting the maximum number of references required per node to two.
2. It is easier to code.
Disadvantages :
1. Basic operations like searching/insertion/deletion tend to take a longer time because in order to find the appropriate position we would have to traverse through all the siblings of the node to be searched/inserted/deleted (in the worst case).
The image on the left is the normal representation of a 6-ary tree and the one on the right is it’s corresponding Left-Child Right-Sibling representation.

Image source : https://en.wikipedia.org/wiki/Left-child_right-sibling_binary_tree
An Example Problem :
Now let’s see a problem and try to solve it using both the discussed representations for clarity.
Given a family tree. Find the kth child of some member X in the tree.
The user inputs two things.
1. A character P (representing the parent whose child is to be found)
2. An integer k (representing the child number)
The problem in itself looks pretty easy. The only issue here is that the maximum number of children a node can have is unspecified which makes it rather tricky to construct the tree.
Example:
Consider the following family tree.

Input : A 2
Output : C
In this case, the user wishes to know A's
second child which according to the figure
is C.
Input : F 3
Output : K
Similar to the first case, the user wishes
to know F's third child which is K.
Method 1 (Storing n pointers with every node):
In this method, we assume the maximum number of children a node can have and proceed further. The only (obvious) problem with this method is the upper bound on the number of children. If the value is too low, then the code would fail for certain cases and if the value is too high, then a huge amount of memory is unnecessarily wasted.
If the programmer beforehand knows the structure of the tree, then the upper bound can be set to the maximum number of children a node has in that particular structure. But even in that case, there will be some memory wastage (all nodes may not necessarily have the same number of children, some may even have less. Example: Leaf nodes have no children ).
C++
#include <iostream>
using namespace std;
const int N = 10;
class Node
{
public :
char val;
Node * child[N];
Node( char P)
{
val = P;
for ( int i=0; i<MAX; i++)
child[i] = NULL;
}
};
void printKthChild(Node *root, char P, int k)
{
if (root->val == P)
{
if (root->child[k-1] == NULL)
cout << "Error : Does not exist\n" ;
else
cout << root->child[k-1]->val << endl;
}
for ( int i=0; i<N; i++)
if (root->child[i] != NULL)
printKthChild(root->child[i], P, k);
}
int main()
{
Node *root = new Node( 'A' );
root->child[0] = new Node( 'B' );
root->child[1] = new Node( 'C' );
root->child[2] = new Node( 'D' );
root->child[3] = new Node( 'E' );
root->child[0]->child[0] = new Node( 'F' );
root->child[0]->child[1] = new Node( 'G' );
root->child[2]->child[0] = new Node( 'H' );
root->child[0]->child[0]->child[0] = new Node( 'I' );
root->child[0]->child[0]->child[1] = new Node( 'J' );
root->child[0]->child[0]->child[2] = new Node( 'K' );
root->child[2]->child[0]->child[0] = new Node( 'L' );
root->child[2]->child[0]->child[1] = new Node( 'M' );
char P = 'F' ;
cout << "F's second child is : " ;
printKthChild(root, P, 2);
P = 'A' ;
cout << "A's seventh child is : " ;
printKthChild(root, P, 7);
return 0;
}
|
Java
class GFG
{
static int N = 10 ;
static class Node
{
char val;
Node[] child = new Node[N];
Node( char P)
{
val = P;
for ( int i = 0 ; i < N; i++)
child[i] = null ;
}
};
static void printKthChild(Node root,
char P, int k)
{
if (root.val == P)
{
if (root.child[k - 1 ] == null )
System.out.print( "Error : Does not exist\n" );
else
System.out.print(root.child[k - 1 ].val + "\n" );
}
for ( int i = 0 ; i < N; i++)
if (root.child[i] != null )
printKthChild(root.child[i], P, k);
}
public static void main(String[] args)
{
Node root = new Node( 'A' );
root.child[ 0 ] = new Node( 'B' );
root.child[ 1 ] = new Node( 'C' );
root.child[ 2 ] = new Node( 'D' );
root.child[ 3 ] = new Node( 'E' );
root.child[ 0 ].child[ 0 ] = new Node( 'F' );
root.child[ 0 ].child[ 1 ] = new Node( 'G' );
root.child[ 2 ].child[ 0 ] = new Node( 'H' );
root.child[ 0 ].child[ 0 ].child[ 0 ] = new Node( 'I' );
root.child[ 0 ].child[ 0 ].child[ 1 ] = new Node( 'J' );
root.child[ 0 ].child[ 0 ].child[ 2 ] = new Node( 'K' );
root.child[ 2 ].child[ 0 ].child[ 0 ] = new Node( 'L' );
root.child[ 2 ].child[ 0 ].child[ 1 ] = new Node( 'M' );
char P = 'F' ;
System.out.print( "F's second child is : " );
printKthChild(root, P, 2 );
P = 'A' ;
System.out.print( "A's seventh child is : " );
printKthChild(root, P, 7 );
}
}
|
Python3
N = 10
class Node:
def __init__( self , P):
self .val = P
self .child = []
for i in range ( 10 ):
self .child.append( None )
def printKthChild(root, P, k):
if (root.val = = P):
if (root.child[k - 1 ] = = None ):
print ( "Error : Does not exist" )
else :
print ( root.child[k - 1 ].val )
for i in range (N) :
if (root.child[i] ! = None ):
printKthChild(root.child[i], P, k)
root = Node( 'A' )
root.child[ 0 ] = Node( 'B' )
root.child[ 1 ] = Node( 'C' )
root.child[ 2 ] = Node( 'D' )
root.child[ 3 ] = Node( 'E' )
root.child[ 0 ].child[ 0 ] = Node( 'F' )
root.child[ 0 ].child[ 1 ] = Node( 'G' )
root.child[ 2 ].child[ 0 ] = Node( 'H' )
root.child[ 0 ].child[ 0 ].child[ 0 ] = Node( 'I' )
root.child[ 0 ].child[ 0 ].child[ 1 ] = Node( 'J' )
root.child[ 0 ].child[ 0 ].child[ 2 ] = Node( 'K' )
root.child[ 2 ].child[ 0 ].child[ 0 ] = Node( 'L' )
root.child[ 2 ].child[ 0 ].child[ 1 ] = Node( 'M' )
P = 'F'
print ( "F's second child is : " )
printKthChild(root, P, 2 )
P = 'A'
print ( "A's seventh child is : " )
printKthChild(root, P, 7 )
|
C#
using System;
class GFG
{
static int N = 10;
class Node
{
public char val;
public Node[] child = new Node[N];
public Node( char P)
{
val = P;
for ( int i = 0; i < N; i++)
child[i] = null ;
}
};
static void printKthChild(Node root,
char P, int k)
{
if (root.val == P)
{
if (root.child[k - 1] == null )
Console.Write( "Error : Does not exist\n" );
else
Console.Write(root.child[k - 1].val + "\n" );
}
for ( int i = 0; i < N; i++)
if (root.child[i] != null )
printKthChild(root.child[i], P, k);
}
public static void Main(String[] args)
{
Node root = new Node( 'A' );
root.child[0] = new Node( 'B' );
root.child[1] = new Node( 'C' );
root.child[2] = new Node( 'D' );
root.child[3] = new Node( 'E' );
root.child[0].child[0] = new Node( 'F' );
root.child[0].child[1] = new Node( 'G' );
root.child[2].child[0] = new Node( 'H' );
root.child[0].child[0].child[0] = new Node( 'I' );
root.child[0].child[0].child[1] = new Node( 'J' );
root.child[0].child[0].child[2] = new Node( 'K' );
root.child[2].child[0].child[0] = new Node( 'L' );
root.child[2].child[0].child[1] = new Node( 'M' );
char P = 'F' ;
Console.Write( "F's second child is : " );
printKthChild(root, P, 2);
P = 'A' ;
Console.Write( "A's seventh child is : " );
printKthChild(root, P, 7);
}
}
|
Javascript
<script>
var N = 10;
class Node
{
constructor(P)
{
this .val = P;
this .child = Array(N).fill( null );
}
};
function printKthChild(root, P, k)
{
if (root.val == P)
{
if (root.child[k - 1] == null )
document.write( "Error : Does not exist<br>" );
else
document.write(root.child[k - 1].val + "<br>" );
}
for ( var i = 0; i < N; i++)
if (root.child[i] != null )
printKthChild(root.child[i], P, k);
}
var root = new Node( 'A' );
root.child[0] = new Node( 'B' );
root.child[1] = new Node( 'C' );
root.child[2] = new Node( 'D' );
root.child[3] = new Node( 'E' );
root.child[0].child[0] = new Node( 'F' );
root.child[0].child[1] = new Node( 'G' );
root.child[2].child[0] = new Node( 'H' );
root.child[0].child[0].child[0] = new Node( 'I' );
root.child[0].child[0].child[1] = new Node( 'J' );
root.child[0].child[0].child[2] = new Node( 'K' );
root.child[2].child[0].child[0] = new Node( 'L' );
root.child[2].child[0].child[1] = new Node( 'M' );
var P = 'F ';
document.write("F' s second child is : ");
printKthChild(root, P, 2);
P = 'A' ;
document.write( "A's seventh child is : " );
printKthChild(root, P, 7);
</script>
|
Output:
F's second child is : J
A's seventh child is : Error : Does not exist
Time Complexity: O(N^h) where N is the maximum number of children for each node and h is the height of the tree.
Auxiliary Space: O(N^h)
In the above tree, had there been a node which had say, 15 children, then this code would have given a Segmentation fault.
Method 2 : (Left-Child Right-Sibling Representation)
In this method, we change the structure of the family tree. In the standard tree, each parent node is connected to all of its children. Here as discussed above, instead of having each node store pointers to all of its children, a node will store pointer to just one of its child. Apart from this the node will also store a pointer to its immediate right sibling.
The image below is the Left-Child Right-Sibling equivalent of the example used above.

C++
#include <iostream>
using namespace std;
class Node
{
public :
char val;
Node *child;
Node *next;
Node( char P)
{
val = P;
child = NULL;
next = NULL;
}
};
void printKthChild(Node *root, char P, int k)
{
if (root == NULL)
return ;
if (root->val == P)
{
Node *t = root->child;
int i = 1;
while (t != NULL && i < k)
{
t = t->next;
i++;
}
if (t == NULL)
cout << "Error : Does not exist\n" ;
else
cout << t->val << " " << endl;
return ;
}
printKthChild(root->child, P, k);
printKthChild(root->next, P, k);
}
int main()
{
Node *root = new Node( 'A' );
root->child = new Node( 'B' );
root->child->next = new Node( 'C' );
root->child->next->next = new Node( 'D' );
root->child->next->next->next = new Node( 'E' );
root->child->child = new Node( 'F' );
root->child->child->next = new Node( 'G' );
root->child->next->next->child = new Node( 'H' );
root->child->next->next->child->child = new Node( 'L' );
root->child->next->next->child->child->next = new Node( 'M' );
root->child->child->child = new Node( 'I' );
root->child->child->child->next = new Node( 'J' );
root->child->child->child->next->next = new Node( 'K' );
char P = 'F' ;
cout << "F's second child is : " ;
printKthChild(root, P, 2);
P = 'A' ;
cout << "A's seventh child is : " ;
printKthChild(root, P, 7);
return 0;
}
|
Java
class GFG
{
static class Node
{
char val;
Node child;
Node next;
Node( char P)
{
val = P;
child = null ;
next = null ;
}
};
static void printKthChild(Node root, char P, int k)
{
if (root == null )
return ;
if (root.val == P)
{
Node t = root.child;
int i = 1 ;
while (t != null && i < k)
{
t = t.next;
i++;
}
if (t == null )
System.out.print( "Error : Does not exist\n" );
else
System.out.print(t.val + " " + "\n" );
return ;
}
printKthChild(root.child, P, k);
printKthChild(root.next, P, k);
}
public static void main(String[] args)
{
Node root = new Node( 'A' );
root.child = new Node( 'B' );
root.child.next = new Node( 'C' );
root.child.next.next = new Node( 'D' );
root.child.next.next.next = new Node( 'E' );
root.child.child = new Node( 'F' );
root.child.child.next = new Node( 'G' );
root.child.next.next.child = new Node( 'H' );
root.child.next.next.child.child = new Node( 'L' );
root.child.next.next.child.child.next = new Node( 'M' );
root.child.child.child = new Node( 'I' );
root.child.child.child.next = new Node( 'J' );
root.child.child.child.next.next = new Node( 'K' );
char P = 'F' ;
System.out.print( "F's second child is : " );
printKthChild(root, P, 2 );
P = 'A' ;
System.out.print( "A's seventh child is : " );
printKthChild(root, P, 7 );
}
}
|
Python3
class Node:
def __init__( self ,P):
self .val = P
self .child = None
self . next = None
def printKthChild(root, P, k):
if (root = = None ):
return
if (root.val = = P):
t = root.child
i = 1
while (t ! = None and i < k):
t = t. next
i + = 1
if (t = = None ):
print ( "Error : Does not exist" )
else :
print (t.val)
return
printKthChild(root.child, P, k)
printKthChild(root. next , P, k)
root = Node( 'A' )
root.child = Node( 'B' )
root.child. next = Node( 'C' )
root.child. next . next = Node( 'D' )
root.child. next . next . next = Node( 'E' )
root.child.child = Node( 'F' )
root.child.child. next = Node( 'G' )
root.child. next . next .child = Node( 'H' )
root.child. next . next .child.child = Node( 'L' )
root.child. next . next .child.child. next = Node( 'M' )
root.child.child.child = Node( 'I' )
root.child.child.child. next = Node( 'J' )
root.child.child.child. next . next = Node( 'K' )
P = 'F'
print ( "F's second child is :" ,end = " " )
printKthChild(root, P, 2 )
P = 'A'
print ( "A's seventh child is :" ,end = " " )
printKthChild(root, P, 7 )
|
C#
using System;
class GFG
{
public class Node
{
public char val;
public Node child;
public Node next;
public Node( char P)
{
val = P;
child = null ;
next = null ;
}
};
static void printKthChild(Node root,
char P, int k)
{
if (root == null )
return ;
if (root.val == P)
{
Node t = root.child;
int i = 1;
while (t != null && i < k)
{
t = t.next;
i++;
}
if (t == null )
Console.Write( "Error : Does not exist\n" );
else
Console.Write(t.val + " " + "\n" );
return ;
}
printKthChild(root.child, P, k);
printKthChild(root.next, P, k);
}
public static void Main(String[] args)
{
Node root = new Node( 'A' );
root.child = new Node( 'B' );
root.child.next = new Node( 'C' );
root.child.next.next = new Node( 'D' );
root.child.next.next.next = new Node( 'E' );
root.child.child = new Node( 'F' );
root.child.child.next = new Node( 'G' );
root.child.next.next.child = new Node( 'H' );
root.child.next.next.child.child = new Node( 'L' );
root.child.next.next.child.child.next = new Node( 'M' );
root.child.child.child = new Node( 'I' );
root.child.child.child.next = new Node( 'J' );
root.child.child.child.next.next = new Node( 'K' );
char P = 'F' ;
Console.Write( "F's second child is : " );
printKthChild(root, P, 2);
P = 'A' ;
Console.Write( "A's seventh child is : " );
printKthChild(root, P, 7);
}
}
|
Javascript
<script>
class Node
{
constructor(P)
{
this .val = P;
this .child = null ;
this .next = null ;
}
}
function printKthChild(root, P, k)
{
if (root == null )
return ;
if (root.val == P)
{
let t = root.child;
let i = 1;
while (t != null && i < k)
{
t = t.next;
i++;
}
if (t == null )
document.write( "Error : Does not exist<br>" );
else
document.write(t.val + " " + "<br>" );
return ;
}
printKthChild(root.child, P, k);
printKthChild(root.next, P, k);
}
let root = new Node( 'A' );
root.child = new Node( 'B' );
root.child.next = new Node( 'C' );
root.child.next.next = new Node( 'D' );
root.child.next.next.next = new Node( 'E' );
root.child.child = new Node( 'F' );
root.child.child.next = new Node( 'G' );
root.child.next.next.child = new Node( 'H' );
root.child.next.next.child.child = new Node( 'L' );
root.child.next.next.child.child.next = new Node( 'M' );
root.child.child.child = new Node( 'I' );
root.child.child.child.next = new Node( 'J' );
root.child.child.child.next.next = new Node( 'K' );
let P = 'F ';
document.write("F' s second child is : ");
printKthChild(root, P, 2);
P = 'A' ;
document.write( "A's seventh child is : " );
printKthChild(root, P, 7);
</script>
|
Output:
F's second child is : J
A's seventh child is : Error : Does not exist
Time Complexity: O(N) where N is the total number of nodes in the tree.
Auxiliary Space: O(N)
Related Article :
Creating a tree with Left-Child Right-Sibling Representation
This article is contributed by Akhil Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.