Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every node stores address of its children and the very first node’s address will be stored in a separate pointer called root.
The Generic trees are the N-ary trees which have the following properties:
1. Many children at every node.
2. The number of nodes for each node is not known in advance.
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:
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.
For storing the address of children in a node we can use an array or linked list. But we will face some issues with both of them.
- In Linked list, we can not randomly access any child’s address. So it will be expensive.
- In array, we can randomly access the address of any child, but we can store only fixed number of children’s addresses in it.
We can use Dynamic Arrays for storing the address of children’s address. We can randomly access any child’s address and the size of the vector is also not fixed.
First child / Next sibling representation
In the first child/next sibling representation, the steps taken are:
At each node-link the children of the 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 parents 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:
- Memory efficient – No extra links are required, hence a 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.
- Many algorithms can be expressed more easily because it is just a binary tree.
- Each node is of fixed size no auxiliary array or vector is required.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Replace every node with depth in N-ary Generic Tree
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Height of a generic tree from parent array
- Write Code to Determine if Two Trees are Identical
- Foldable Binary Trees
- Print Common Nodes in Two Binary Search Trees
- Find all possible binary trees with given Inorder Traversal
- Iterative function to check if two trees are identical
- Number of full binary trees such that each node is product of its children
- Find first non matching leaves in two binary trees
- Enumeration of Binary Trees
- Iterative method to check if two trees are mirror of each other
- Count Balanced Binary Trees of Height h
- Merge Two Binary Trees by doing Node Sum (Recursive and Iterative)
- Number of Binary Trees for given Preorder Sequence length
- Check if two trees are Mirror | Set 2
- Dynamic Programming on Trees | Set-1
- Dynamic Programming on Trees | Set 2
- AA Trees | Set 1 (Introduction)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : priyankashrm7302