Left-Child Right-Sibling Representation of Tree
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.
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.
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++
// C++ program to find k-th child of a given // node using typical representation that uses // an array of pointers. #include <iostream> using namespace std; // Maximum number of children 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; } }; // Traverses given n-ary tree to find K-th // child of P. void printKthChild(Node *root, char P, int k) { // If P is current root if (root->val == P) { if (root->child[k-1] == NULL) cout << "Error : Does not exist\n" ; else cout << root->child[k-1]->val << endl; } // If P lies in a subtree for ( int i=0; i<N; i++) if (root->child[i] != NULL) printKthChild(root->child[i], P, k); } // Driver code 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' ); // Print F's 2nd child 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
// Java program to find k-th child // of a given node using typical // representation that uses // an array of pointers. class GFG { // Maximum number of children 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 ; } }; // Traverses given n-ary tree to // find K-th child of P. static void printKthChild(Node root, char P, int k) { // If P is current root 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" ); } // If P lies in a subtree for ( int i = 0 ; i < N; i++) if (root.child[i] != null ) printKthChild(root.child[i], P, k); } // Driver code 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' ); // Print F's 2nd child 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 ); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to find k-th child of a given # node using typical representation that uses # an array of pointers. # Maximum number of children N = 10 class Node: def __init__( self , P): self .val = P self .child = [] for i in range ( 10 ): self .child.append( None ) # Traverses given n-ary tree to find K-th # child of P. def printKthChild(root, P, k): # If P is current root if (root.val = = P): if (root.child[k - 1 ] = = None ): print ( "Error : Does not exist" ) else : print ( root.child[k - 1 ].val ) # If P lies in a subtree for i in range (N) : if (root.child[i] ! = None ): printKthChild(root.child[i], P, k) # Driver code 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' ) # Print F's 2nd child P = 'F' print ( "F's second child is : " ) printKthChild(root, P, 2 ) P = 'A' print ( "A's seventh child is : " ) printKthChild(root, P, 7 ) # This code is contributed by Arnab Kundu |
C#
// C# program to find k-th child // of a given node using typical // representation that uses // an array of pointers. using System; class GFG { // Maximum number of children 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 ; } }; // Traverses given n-ary tree to // find K-th child of P. static void printKthChild(Node root, char P, int k) { // If P is current root 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" ); } // If P lies in a subtree for ( int i = 0; i < N; i++) if (root.child[i] != null ) printKthChild(root.child[i], P, k); } // Driver code 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' ); // Print F's 2nd child 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); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // Javascript program to find k-th child of a // given node using typical representation that // uses an array of pointers.Maximum number of children var N = 10; class Node { constructor(P) { this .val = P; this .child = Array(N).fill( null ); } }; // Traverses given n-ary tree to // find K-th child of P. function printKthChild(root, P, k) { // If P is current root 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>" ); } // If P lies in a subtree for ( var i = 0; i < N; i++) if (root.child[i] != null ) printKthChild(root.child[i], P, k); } // Driver code 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' ); // Print F's 2nd child 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); // This code is contributed by noob2000 </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.
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++
// C++ program to find k-th child of a given // Node using typical representation that uses // an array of pointers. #include <iostream> using namespace std; // A Node to represent left child right sibling // representation. class Node { public : char val; Node *child; Node *next; Node( char P) { val = P; child = NULL; next = NULL; } }; // Traverses given n-ary tree to find K-th // child of P. void printKthChild(Node *root, char P, int k) { if (root == NULL) return ; // If P is present at root itself if (root->val == P) { // Traverse children of root starting // from left child 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); } // Driver code 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' ); // Print F's 2nd child 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
// Java program to find k-th child of a given // Node using typical representation that uses // an array of pointers. class GFG { // A Node to represent left child // right sibling representation. static class Node { char val; Node child; Node next; Node( char P) { val = P; child = null ; next = null ; } }; // Traverses given n-ary tree to find K-th // child of P. static void printKthChild(Node root, char P, int k) { if (root == null ) return ; // If P is present at root itself if (root.val == P) { // Traverse children of root starting // from left child 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); } // Driver code 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' ); // Print F's 2nd child 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 ); } } // This code is contributed by 29AjayKumar |
Python3
# Python program to find k-th child of a given # Node using typical representation that uses # an array of pointers. # A Node to represent left child # right sibling representation. class Node: def __init__( self ,P): self .val = P self .child = None self . next = None # Traverses given n-ary tree to find K-th # child of P. def printKthChild(root, P, k): if (root = = None ): return # If P is present at root itself if (root.val = = P): # Traverse children of root starting # from left child 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) # Driver code 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' ) # Print F's 2nd child 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 ) # this code is contributed by shinjanpatra |
C#
// C# program to find k-th child of a given // Node using typical representation that uses // an array of pointers. using System; class GFG { // A Node to represent left child // right sibling representation. public class Node { public char val; public Node child; public Node next; public Node( char P) { val = P; child = null ; next = null ; } }; // Traverses given n-ary tree to find K-th // child of P. static void printKthChild(Node root, char P, int k) { if (root == null ) return ; // If P is present at root itself if (root.val == P) { // Traverse children of root starting // from left child 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); } // Driver code 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' ); // Print F's 2nd child 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); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // Javascript program to find k-th child of a given // Node using typical representation that uses // an array of pointers. // A Node to represent left child // right sibling representation. class Node { constructor(P) { this .val = P; this .child = null ; this .next = null ; } } // Traverses given n-ary tree to find K-th // child of P. function printKthChild(root, P, k) { if (root == null ) return ; // If P is present at root itself if (root.val == P) { // Traverse children of root starting // from left child 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); } // Driver code 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' ); // Print F's 2nd child 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); // This code is contributed by avanitrachhadiya2155 </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
Contact Us