Inorder traversal of an N-ary Tree

Given an N-ary tree containing, the task is to print the inorder traversal of the tree.

Examples:

Input: N = 3

Output: 5 6 2 7 3 1 4



Input: N = 3

Output: 2 5 3 1 4 6

Approach: The inorder traversal of an N-ary tree is defined as visiting all the children except the last then the root and finally the last child recursively.

  • Recursively visit the first child.
  • Recursively visit the second child.
  • …..
  • Recursively visit the second last child.
  • Print the data in the node.
  • Recursively visit the last child.
  • Repeat the above steps till all the nodes are visited.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
  
// Class for the node of the tree
struct Node 
{
    int data;
  
    // List of children
    struct Node **children;
      
    int length;
      
    Node()
    {
        length = 0;
        data = 0; 
    }
  
    Node(int n, int data_)
    {
        children = (Node**)malloc(sizeof(Node*)*n);
        length = n;
        data = data_;
    }
};
  
// Function to print the inorder traversal
// of the n-ary tree
void inorder(Node *node)
{
    if (node == NULL)
        return;
  
    // Total children count
    int total = node->length;
      
    // All the children except the last
    for (int i = 0; i < total - 1; i++)
        inorder(node->children[i]);
  
    // Print the current node's data
    cout<< node->data << " ";
  
    // Last child
    inorder(node->children[total - 1]);
}
  
// Driver code
int main()
{
  
    /* Create the following tree 
            1
            / | \
        2 3 4
        / | \
        5 6 7
    */
    int n = 3;
    Node* root = new Node(n, 1);
    root->children[0] = new Node(n, 2);
    root->children[1] = new Node(n, 3);
    root->children[2] = new Node(n, 4);
    root->children[0]->children[0] = new Node(n, 5);
    root->children[0]->children[1] = new Node(n, 6);
    root->children[0]->children[2] = new Node(n, 7);
  
    inorder(root);
    return 0;
}
  
// This code is Contributed by Arnab Kundu

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Class for the node of the tree
    static class Node {
        int data;
  
        // List of children
        Node children[];
  
        Node(int n, int data)
        {
            children = new Node[n];
            this.data = data;
        }
    }
  
    // Function to print the inorder traversal
    // of the n-ary tree
    static void inorder(Node node)
    {
        if (node == null)
            return;
  
        // Total children count
        int total = node.children.length;
        // All the children except the last
        for (int i = 0; i < total - 1; i++)
            inorder(node.children[i]);
  
        // Print the current node's data
        System.out.print("" + node.data + " ");
  
        // Last child
        inorder(node.children[total - 1]);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        /* Create the following tree 
                   1
                /  |  \
               2   3   4
             / | \
            5  6  7
        */
        int n = 3;
        Node root = new Node(n, 1);
        root.children[0] = new Node(n, 2);
        root.children[1] = new Node(n, 3);
        root.children[2] = new Node(n, 4);
        root.children[0].children[0] = new Node(n, 5);
        root.children[0].children[1] = new Node(n, 6);
        root.children[0].children[2] = new Node(n, 7);
  
        inorder(root);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
  
    // Class for the node of the tree 
    public class Node 
    
        public int data; 
  
        // List of children 
        public Node []children; 
  
        public Node(int n, int data) 
        
            children = new Node[n]; 
            this.data = data; 
        
    
  
    // Function to print the inorder traversal 
    // of the n-ary tree 
    static void inorder(Node node) 
    
        if (node == null
            return
  
        // Total children count 
        int total = node.children.Length; 
          
        // All the children except the last 
        for (int i = 0; i < total - 1; i++) 
            inorder(node.children[i]); 
  
        // Print the current node's data 
        Console.Write("" + node.data + " "); 
  
        // Last child 
        inorder(node.children[total - 1]); 
    
  
    // Driver code 
    public static void Main() 
    
  
        /* Create the following tree 
                
                / | \ 
            2 3 4 
            / | \ 
            5 6 7 
        */
        int n = 3; 
        Node root = new Node(n, 1); 
        root.children[0] = new Node(n, 2); 
        root.children[1] = new Node(n, 3); 
        root.children[2] = new Node(n, 4); 
        root.children[0].children[0] = new Node(n, 5); 
        root.children[0].children[1] = new Node(n, 6); 
        root.children[0].children[2] = new Node(n, 7); 
  
        inorder(root); 
    
  
// This code is contributed by AnkitRai01

chevron_right


Output:

5 6 2 7 3 1 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, andrew1234