VOOZH about

URL: https://www.geeksforgeeks.org/dsa/left-child-right-sibling-representation-tree/

⇱ Left-Child Right-Sibling Representation of Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Left-Child Right-Sibling Representation of Tree

Last Updated : 23 Jul, 2025

An n-ary tree in computer science is a collection of nodes normally represented hierarchically in the following fashion.  

  1. The tree starts at the root node.
  2. Each node of the tree holds a list of references to its child nodes.
  3. 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


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.
 

πŸ‘ Untitled Diagram

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 ).

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.
 

πŸ‘ new

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


 

Comment