Open In App

Number of ways to traverse an N-ary tree

Last Updated : 08 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an n-ary tree, count the number of ways to traverse an n-ary (or a Directed Acyclic Graph) tree starting from the root vertex

Example:

Suppose we have a given N-ary tree as shown below:

maryintial

Now we have to find the number of ways of traversing the whole tree starting from the root vertex. There can be many such ways. Some of them are listed below.

  • N->M->K->J->B->F->D->E->C->H->I->L->A (kind-of depth-first traversal)
  • A->B->F->D->E->K->J->G->C->H->I->N->M->L (level order traversal) 

Note: The count of all ways to traverse is the product of factorials of the number of children of each node. Refer to the below figure for a clear understanding:

marynew

Here,
‘A’ has four children, so 4! permutations possible
‘B’ has two children, so 2! permutations possible
‘F’ has no children, so 0! permutations possible
….. 
And so on
Hence all such ways are- 4 ! * 2 ! * 0 ! * 1 ! * 3 ! * 2 ! * 0 ! * 0 ! * 0 ! * 0 ! * 1 ! * 0 ! * 0 ! = 576 way
That’s a huge number of ways and among them, only few prove to be useful, like- inorder, level-order, preorder, and postorder (arranged according to the popularity of these traversals)

Follow the below steps to solve the problem:

  • Initialize ways equal to one and create a queue of type Node and push root into it
  • While the queue is not empty
    • Initialize integer p equal to the front Node of the queue
    • Set ways equal to ways * x!, where x is the count of p’s children
    • Enqueue all the children of the Node p
  • Return ways

Below is the implementation of the above approach:

C++




// C++ program to find the number of ways to traverse a
// n-ary tree starting from the root node
#include <bits/stdc++.h>
using namespace std;
 
// Structure of a node of an n-ary tree
struct Node {
    char key;
    vector<Node*> child;
};
 
// Utility function to create a new tree node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    return temp;
}
 
// Utility Function to find factorial of given number
int factorial(int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
 
// Function to calculate the number of ways of traversing
// the n-ary starting from root.
// This function is just a modified breadth-first search.
// We can use a depth-first search too.
int calculateWays(Node* root)
{
    int ways = 1; // Initialize result
 
    // If the tree is empty there is no way of traversing
    // the tree.
    if (root == NULL)
        return 0;
 
    // Create a queue and enqueue root to it.
    queue<Node*> q;
    q.push(root);
 
    // Level order traversal.
    while (!q.empty()) {
        // Dequeue an item from queue and print it
        Node* p = q.front();
        q.pop();
 
        // The number of ways is the product of
        // factorials of number of children of each node.
        ways = ways * (factorial(p->child.size()));
 
        // Enqueue all childrent of the dequeued item
        for (int i = 0; i < p->child.size(); i++)
            q.push(p->child[i]);
    }
 
    return (ways);
}
 
// Driver code
int main()
{
    /*   Let us create below tree
     *           A
     *         / /  \  \
     *       B  F   D  E
     *      / \     |  /|\
     *     K  J    G  C H I
     *      /\            \
     *    N   M            L
     */
 
    Node* root = newNode('A');
    (root->child).push_back(newNode('B'));
    (root->child).push_back(newNode('F'));
    (root->child).push_back(newNode('D'));
    (root->child).push_back(newNode('E'));
    (root->child[0]->child).push_back(newNode('K'));
    (root->child[0]->child).push_back(newNode('J'));
    (root->child[2]->child).push_back(newNode('G'));
    (root->child[3]->child).push_back(newNode('C'));
    (root->child[3]->child).push_back(newNode('H'));
    (root->child[3]->child).push_back(newNode('I'));
    (root->child[0]->child[0]->child)
        .push_back(newNode('N'));
    (root->child[0]->child[0]->child)
        .push_back(newNode('M'));
    (root->child[3]->child[2]->child)
        .push_back(newNode('L'));
 
    cout << calculateWays(root);
    ;
 
    return 0;
}


Java




//Java code for the above approach
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class GFG {
    // Structure of a node of an n-ary tree
    static class Node {
        char key;
        ArrayList<Node> child;
        public Node(char key)
        {
            this.key = key;
            child = new ArrayList<>();
        }
    }
 
    // Utility function to create a new tree node
    static Node newNode(char key) { return new Node(key); }
 
    // Utility Function to find factorial of given number
    static int factorial(int n)
    {
        if (n == 0) {
            return 1;
        }
        return n * factorial(n - 1);
    }
 
    // Function to calculate the number of ways of
    // traversing the n-ary starting from root. This
    // function is just a modified breadth-first search. We
    // can use a depth-first search too.
    static int calculateWays(Node root)
    {
        int ways = 1; // Initialize result
 
        // If the tree is empty there is no way of
        // traversing the tree.
        if (root == null) {
            return 0;
        }
 
        // Create a queue and enqueue root to it.
        Queue<Node> q = new LinkedList<>();
        q.add(root);
 
        // Level order traversal.
        while (!q.isEmpty()) {
            // Dequeue an item from queue and print it
            Node p = q.poll();
 
            // The number of ways is the product of
            // factorials of number of children of each
            // node.
            ways = ways * (factorial(p.child.size()));
 
            // Enqueue all children of the dequeued item
            for (int i = 0; i < p.child.size(); i++) {
                q.add(p.child.get(i));
            }
        }
 
        return (ways);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        /*   Let us create below tree
         *           A
         *         / /  \  \
         *       B  F   D  E
         *      / \     |  /|\
         *     K  J    G  C H I
         *      /\            \
         *    N   M            L
         */
 
        Node root = newNode('A');
        root.child.add(newNode('B'));
        root.child.add(newNode('F'));
        root.child.add(newNode('D'));
        root.child.add(newNode('E'));
        root.child.get(0).child.add(newNode('K'));
        root.child.get(0).child.add(newNode('J'));
        root.child.get(2).child.add(newNode('G'));
        root.child.get(3).child.add(newNode('C'));
        root.child.get(3).child.add(newNode('H'));
        root.child.get(3).child.add(newNode('I'));
        root.child.get(0).child.get(0).child.add(
            newNode('N'));
        root.child.get(0).child.get(0).child.add(
            newNode('M'));
        root.child.get(3).child.get(2).child.add(
            newNode('L'));
 
        System.out.println(calculateWays(root));
    }
}


Python3




# Python3 program to find the
# number of ways to traverse a
# n-ary tree starting from the root node
 
 
class Node:
    def __init__(self, key):
        self.child = []
        self.key = key
 
# Utility function to create a new tree node
 
 
def newNode(key):
    temp = Node(key)
    return temp
 
# Utility Function to find factorial of given number
 
 
def factorial(n):
 
    if (n == 0):
        return 1
 
    return n*factorial(n-1)
 
# Function to calculate the number of ways of traversing
# the n-ary starting from root.
# self function is just a modified breadth-first search.
# We can use a depth-first search too.
 
 
def calculateWays(root):
 
    ways = 1  # Initialize result
 
    # If the tree is empty there is no way of traversing
    # the tree.
    if (root == None):
        return 0
 
    # Create a queue and enqueue root to it.
    q = []
    q.append(root)
 
    # Level order traversal.
    while (len(q) > 0):
        # Dequeue an item from queue and print it
        p = q[0]
        q = q[1:]
 
        # The number of ways is the product of
        # factorials of number of children of each node.
        ways = ways*(factorial(len(p.child)))
 
        # Enqueue all childrent of the dequeued item
        for i in range(len(p.child)):
            q.append(p.child[i])
 
    return(ways)
 
#  Let us create below tree
# *         A
# *         / / \ \
# *     B F D E
# *     / \     | /|\
# *     K J G C H I
# *     /\         \
# * N M         L
 
 
root = newNode('A')
(root.child).append(newNode('B'))
(root.child).append(newNode('F'))
(root.child).append(newNode('D'))
(root.child).append(newNode('E'))
(root.child[0].child).append(newNode('K'))
(root.child[0].child).append(newNode('J'))
(root.child[2].child).append(newNode('G'))
(root.child[3].child).append(newNode('C'))
(root.child[3].child).append(newNode('H'))
(root.child[3].child).append(newNode('I'))
(root.child[0].child[0].child).append(newNode('N'))
(root.child[0].child[0].child).append(newNode('M'))
(root.child[3].child[2].child).append(newNode('L'))
 
print(calculateWays(root))


C#




//C# code for the above approach
using System;
using System.Collections.Generic;
 
class Node
{
    public char Key { get; set; }
    public List<Node> Children { get; set; }
 
    public Node(char key)
    {
        Key = key;
        Children = new List<Node>();
    }
}
 
class Program
{
     // Utility Function to find factorial of given number
    static int Factorial(int n)
    {
        if (n == 0)
        {
            return 1;
        }
 
        return n * Factorial(n - 1);
    }
    // Function to calculate the number of ways of
    // traversing the n-ary starting from root. This
    // function is just a modified breadth-first search. We
    // can use a depth-first search too.
    static int CalculateWays(Node root)
    {
        int ways = 1;  // Initialize result
 
        // If the tree is empty there is no way of
        // traversing the tree.
        if (root == null)
        {
            return 0;
        }
 
        Queue<Node> queue = new Queue<Node>();
        queue.Enqueue(root);
 
        while (queue.Count != 0)
        {
            Node current = queue.Dequeue();
            ways *= Factorial(current.Children.Count);
 
            foreach (Node child in current.Children)
            {
                queue.Enqueue(child);
            }
        }
 
        return ways;
    }
    // Driver code
    static void Main(string[] args)
    {
        Node root = new Node('A');
        root.Children.Add(new Node('B'));
        root.Children.Add(new Node('F'));
        root.Children.Add(new Node('D'));
        root.Children.Add(new Node('E'));
        root.Children[0].Children.Add(new Node('K'));
        root.Children[0].Children.Add(new Node('J'));
        root.Children[2].Children.Add(new Node('G'));
        root.Children[3].Children.Add(new Node('C'));
        root.Children[3].Children.Add(new Node('H'));
        root.Children[3].Children.Add(new Node('I'));
        root.Children[0].Children[0].Children.Add(new Node('N'));
        root.Children[0].Children[0].Children.Add(new Node('M'));
        root.Children[3].Children[2].Children.Add(new Node('L'));
 
        Console.WriteLine(CalculateWays(root));
    }
}


Javascript




<script>
 
    // JavaScript program to find the
    // number of ways to traverse a
    // n-ary tree starting from the root node
     
    class Node
    {
        constructor(key) {
           this.child = [];
           this.key = key;
        }
    }
     
    // Utility function to create a new tree node
    function newNode(key)
    {
       let temp = new Node(key);
       return temp;
    }
 
    // Utility Function to find factorial of given number
    function factorial(n)
    {
       if (n == 0)
         return 1;
       return n*factorial(n-1);
    }
 
    // Function to calculate the number of ways of traversing
    // the n-ary starting from root.
    // This function is just a modified breadth-first search.
    // We can use a depth-first search too.
    function calculateWays(root)
    {
       let ways = 1;  // Initialize result
 
       // If the tree is empty there is no way of traversing
       // the tree.
       if (root == null)
          return 0;
 
       // Create a queue and enqueue root to it.
       let q = [];
       q.push(root);
 
       // Level order traversal.
       while (q.length > 0)
       {
             // Dequeue an item from queue and print it
             let p = q[0];
             q.shift();
 
             // The number of ways is the product of
             // factorials of number of children of each node.
             ways = ways*(factorial(p.child.length));
 
             // Enqueue all childrent of the dequeued item
             for (let i=0; i< p.child.length; i++)
                q.push(p.child[i]);
        }
 
       return(ways);
    }
     
    /*   Let us create below tree
   *           A
   *         / /  \  \
   *       B  F   D  E
   *      / \     |  /|\
   *     K  J    G  C H I
   *      /\            \
   *    N   M            L
   */
   
   let root = newNode('A');
   (root.child).push(newNode('B'));
   (root.child).push(newNode('F'));
   (root.child).push(newNode('D'));
   (root.child).push(newNode('E'));
   (root.child[0].child).push(newNode('K'));
   (root.child[0].child).push(newNode('J'));
   (root.child[2].child).push(newNode('G'));
   (root.child[3].child).push(newNode('C'));
   (root.child[3].child).push(newNode('H'));
   (root.child[3].child).push(newNode('I'));
   (root.child[0].child[0].child).push(newNode('N'));
   (root.child[0].child[0].child).push(newNode('M'));
   (root.child[3].child[2].child).push(newNode('L'));
   
   document.write(calculateWays(root));
     
</script>


Output

576

Time Complexity: O(N2). We can optimize the solution to work in O(N) time by per-computing factorials of all numbers from 1 to N
Auxiliary Space: O(N)

Common Pitfalls: 
Since products of factorials can tend to grow very huge, so it may overflow. It is preferable to use data types like- unsigned long long int in C/C++, as the number of ways can never be a negative number. In Java and Python, there are Big Integers to take care of overflows



Previous Article
Next Article

Similar Reads

Count of ways to traverse a Matrix according to given conditions
Given an integer N which represents an N x N Square Matrix, the task is to print the number of ways to move from top left to the bottom right of the Square Matrix by following the conditions: If the current position of the pointer is at the edges of the Square Matrix, then the next move can either be a vertical or a horizontal movement, any number
13 min read
Count ways to traverse all N cities when each city is traversed K times
For N given cities, find how many ways a driver can traverse through them so that each town is visited exactly K times. From each city he can go to every other city (except the city which is already in it). Example: Input: N = 3, K = 1Output: 6Explanation: The possible paths are 1 -&gt; 2 -&gt; 31 -&gt; 3 -&gt; 22 -&gt; 1 -&gt; 32 -&gt; 3 -&gt; 13
15+ min read
Count of ways to traverse a Matrix and return to origin in K steps
Given three integers N, M and K, where N and M are the dimensions of the matrix and K is the maximum possible steps, the task is to count the number ways to start from (0, 0) and return traversing the matrix by using K steps only. Note: In each step, one can move up, down, left, right, or stay at the current position. The answer could be large, so
12 min read
Build a segment tree for N-ary rooted tree
Prerequisite: Segment tree and depth first search.In this article, an approach to convert an N-ary rooted tree( a tree with more than 2 children) into a segment tree is discussed which is used to perform a range update queries. Why do we need a segment tree when we already have an n-ary rooted tree? Many times, a situation occurs where the same ope
15+ min read
Check if the given n-ary tree is a binary tree
Given an n-ary tree, the task is to check whether the given tree is binary or not. Examples: Input: A / \ B C / \ \ D E F Output: Yes Input: A / | \ B C D \ F Output: No Approach: Every node in a binary tree can have at most 2 children. So, for every node of the given tree, count the number of children and if for any node the count exceeds 2 then p
6 min read
Remove all leaf nodes from a Generic Tree or N-ary Tree
Given a Generic tree, the task is to delete the leaf nodes from the tree. Examples: Input: 5 / / \ \ 1 2 3 8 / / \ \ 15 4 5 6 Output: 5 : 1 2 3 1 : 2 : 3 : Explanation: Deleted leafs are: 8, 15, 4, 5, 6 Input: 8 / | \ 9 7 2 / | \ | / / | \ \ 4 5 6 10 11 1 2 2 3 Output: 8: 9 7 2 9: 7: 2: Approach: Follow the steps given below to solve the problem Co
9 min read
Count of nodes in given N-ary tree such that their subtree is a Binary Tree
Given an N-ary tree root, the task is to find the count of nodes such that their subtree is a binary tree. Example: Input: Tree in the image below Output: 11Explanation: The nodes such that there subtree is a binary tree are {2, 8, 10, 6, 7, 3, 1, 9, 5, 11, 12}. Input: Tree in the image below Output: 9 Approach: The given problem can be solved by u
11 min read
Tree of Space - Locking and Unlocking N-Ary Tree
Given a world map in the form of Generic M-ary Tree consisting of N nodes and an array queries[], the task is to implement the functions Lock, Unlock and Upgrade for the given tree. For each query in queries[], the functions return true when the operation is performed successfully, otherwise it returns false. The functions are defined as: X: Name o
10 min read
Given a n-ary tree, count number of nodes which have more number of children than parents
Given a N-ary tree represented as adjacency list, we need to write a program to count all such nodes in this tree which has more number of children than its parent.For Example, In the above tree, the count will be 1 as there is only one such node which is '2' which has more number of children than its parent. 2 has three children (4, 5 and 6) where
6 min read
Min number of moves to traverse entire Matrix through connected cells with equal values
Given a matrix A[][] of dimension N*M, the task is to find the minimum number of moves required to traverse the entire matrix starting from (0, 0) by traversing connected cells with equal values at every step. From a cell (i, j), cells (i + 1, j), (i - 1, j), (i, j - 1) and (i, j + 1) can be connected. Examples: Input: arr[][] = {{1, 1, 2, 2, 1}, {
11 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg