The N-ary treedata structure is a complex binary tree data structure that allows us to have multiple children at each node, similar to the binary tree data structure, but with n number of children. This structure is slightly more complex than the prevalent binary trees. The only difference between an N-ary and a binary tree is in their shapes. In an N-ary, we can add and remove leaves (and therefore branches) from the root of the system during its construction.

A famous example of an N-ary tree would be London’s famous London Eye Ferris wheel.

N-ary Trees hold certain advantages over Binary Trees, namely that it takes up significantly less space when there is no more room to grow vertically in a Binary Tree. This also allows for linear storage of data rather than the tree-like structure used in Binary Trees, making it perfect for database files where you would like to save as much space as possible without sacrificing too much speed or efficiency.

General Idea

N-ary trees are a variety of binary trees. They differ by having a single node at the top which holds multiple children. These children may have their children, themselves being n-ary trees with one "level" of depth less than their parents. Thus, at any level, the maximum number of children a node can have is n.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

What is N-ary tree?

An n-ary tree is a tree data structure where each node can have at most n children. Unlike binary trees, which have at most two children per node, n-ary trees allow for a variable number of children. They are used in scenarios where each node may have multiple possible paths or branches, such as in hierarchical data structures or decision trees.

Types of N-ary Tree

Here is a list of some types of N-ary trees:

Full N-ary Tree

Complete N-ary Tree

Perfect N-ary Tree

Balanced N-ary Tree

M-ary Tree

B-Tree

Trie (Prefix Tree)

K-ary Tree

Example and Explanation

Let us consider the following graph.

In the example, we can see that there is one root node with some children. Here we have decided to keep our n value as 3. So, the number of children each node has is less than or equal to 3. This tree can also be called a 3-Ary tree.

N-Ary Tree Approach

Since trees are not native data types for any programming language, we use constructors to make our tree. The language of our choice is Java, as we will be using the List, Linked List, and Queue functions under the util package.

Our constructor will look like this:

public static class NAryTree{
int data;
List<NAryTree> children = new LinkedList<>();
NAryTree(int data){
this.data = data;
}
NAryTree(int data,List<NAryTree> child){
this.data = data;
children = child;
}
}

Here we have used the util package functions- List and LinkedList to create our tree data structure.

To print our tree, we have four ways. They are:

Inorder Traversal

Preorder Traversal

Postorder Traversal

Level Order Traversal

To learn more about these traversal techniques, click here Tree Traversal Techniques to read a fantastic article about the same.

For printing our tree, we take the help of Queue. We add the child nodes one by one and then print them level-wise.

public static void main(String[] args) { NAryTree root = new NAryTree(10); //root; level 0

root.children.add(new NAryTree(20)); //1st child node of root root.children.add(new NAryTree(30)); //2nd child node of root root.children.add(new NAryTree(40)); //3rd child node of root

root.children.get(0).children.add(new NAryTree(50)); //1st child of 1st child node root.children.get(0).children.add(new NAryTree(60)); //2nd child of 1st child node root.children.get(0).children.add(new NAryTree(70)); //3rd child of 1st child node

root.children.get(1).children.add(new NAryTree(80)); //1st child of 2nd child node root.children.get(1).children.add(new NAryTree(90)); //2nd child of 2nd child node root.children.get(1).children.add(new NAryTree(100)); //3rd child of 2nd child node

root.children.get(2).children.add(new NAryTree(110)); //1st child of 3rd child node

printNAryTree(root); } }

Output

10
20 30 40
50 60 70 80 90 100 110

Complexity Analysis

Time Complexity

In the given implementation, while insertion, we visit each node exactly once, thus the time complexity is,T(n) = O(N), where N is the number of nodes.

Space Complexity

In the given implementation, we are just storing the tree. Thus, Space Complexity = O(N), where N is the size of the tree.

A "5-ary tree" is an N-ary tree in which each node can have up to five children. It's a specific type of N-ary tree where each node can have a maximum of five descendants or sub-trees.

Is every binary search tree an N-ary tree?

No, not every binary search tree (BST) is an N-ary tree. A binary search tree is a specific type of binary tree where each node has at most two children, and it follows a specific ordering property where values in the left subtree are smaller and values in the right subtree are larger than the node's value. N-ary trees, on the other hand, can have more than two children per node, making them more flexible in terms of branching.

What are the three types of binary tree?

Three common types of binary trees include the Full Binary Tree, where nodes have either 0 or 2 children; the Complete Binary Tree, where all levels are filled, possibly except the last, filled from left to right; and the Balanced Binary Tree, where the height difference of left and right subtrees is at most one, ensuring efficient operations.

What is the difference between a binary tree and an m-ary tree?

A binary tree restricts nodes to have at most two children (left and right), while an m-ary tree permits nodes to have up to m children, where m is any positive integer greater than or equal to 2, offering greater flexibility in representing hierarchical structures.

Conclusion

To summarize this article, we learned about the N-Ary tree Data Structure. We saw its example, approach, and implementation. We also studied the time and space complexities and covered a few FAQs.