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++ 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
"
``` `; ` ` ` `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; ` `} ` |

Output:

F's second child is : J A's seventh child is : Error : Does not exist

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++ 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
"
``` `; ` ` ` `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; ` `} ` |

Output:

F's second child is : J A's seventh child is : Error : Does not exist

**Related Article : **

Creating a tree with Left-Child Right-Sibling Representation

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## leave a comment

## 0 Comments