# Generic Trees(N-array Trees)

The Generic trees are the n-ary trees which have the following properties:

- Many children at every node.
- The number of nodes for each node is not known in advance.

**Example:**

To represent the above tree, we have to consider the worst case, that is the node with maximum children (in above example, 6 children) and allocate that many pointers for each node.

The node representation based on this method can be written as:

`//Node declaration ` `struct` `Node{ ` ` ` `int` `data; ` ` ` `struct` `Node *firstchild; ` ` ` `struct` `Node *secondchild; ` ` ` `struct` `Node *thirdchild; ` ` ` `struct` `Node *fourthchild; ` ` ` `struct` `Node *fifthchild; ` ` ` `struct` `Node *sixthchild; ` `} ` |

*chevron_right*

*filter_none*

**Disadvantages of the above representation are:**

**Memory Wastage**– All the pointers are not required in all the cases. Hence, there is lot of memory wastage.**Unknown number of children**– The number of children for each node is not known in advance.

**Better Approach:** First child / Next sibling representation

In first child/next sibling representation, the steps taken are:

- At each node link the children of same parent(siblings) from left to right.
- Remove the links from parent to all children except the first child.

Since we have a link between children, we do not need extra links from parent to all the children. This representation allows us to traverse all the elements by starting at the first child of the parent.

The node declaration for first child / next sibling representation can be written as:

`//Node declaration ` `struct` `Node{ ` ` ` `int` `data; ` ` ` `struct` `Node *firstChild; ` ` ` `struct` `Node *nextSibling; ` `} ` |

*chevron_right*

*filter_none*

**Advantages:**

- Memory efficient – No extra links are required, hence lot of memory is saved.
- Treated as binary trees – Since we are able to convert any generic tree to binary representation, we can treat all generic trees with a first child/next sibling representation as binary trees.Instead of left and right pointers, we just use firstChild and nextSibling.

Height of generic tree from parent array

Generic tree – level order traversal

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Height of a generic tree from parent array
- Isomorphism in N-ary Trees
- AA Trees | Set 1 (Introduction)
- B*-Trees implementation in C++
- Dynamic Programming on Trees | Set 2
- Some Basic Theorems on Trees
- Dynamic Programming on Trees | Set-1
- Check if two trees are Mirror | Set 2
- 2-3 Trees | (Search and Insert)
- Combinatorics on ordered trees
- Disjoint Set Union on trees | Set 2
- Check if two trees have same structure
- Disjoint Set Union on trees | Set 1
- Foldable Binary Trees
- m-WAY Search Trees | Set-1 ( Searching )
- Red-Black Trees | Top-Down Insertion
- Check if two trees are Mirror
- DP on Trees | Set-3 ( Diameter of N-ary Tree )
- Enumeration of Binary Trees
- Check if all levels of two trees are anagrams or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.