Given a n-ary tree, count number of nodes which have more number of children than parents
Last Updated :
18 Sep, 2023
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) whereas its parent, 1 has only two children (2 and 3).
We can solve this problem using both BFS and DFS algorithms. We will explain here in details about how to solve this problem using BFS algorithm.
As the tree is represented using adjacency list representation. So, for any node say ‘u’ the number of children of this node can be given as adj[u].size().
Now the idea is to apply BFS on the given tree and while traversing the children of a node ‘u’ say ‘v’ we will simply check if adj[v].size() > adj[u].size().
Below is the implementation of above idea:
CPP
#include<bits/stdc++.h>
using namespace std;
int countNodes(vector< int > adj[], int root)
{
int count = 0;
queue< int > q;
q.push(root);
while (!q.empty())
{
int node = q.front();
q.pop();
for ( int i=0;i<adj[node].size();i++)
{
int children = adj[node][i];
if (adj[children].size() > adj[node].size())
count++;
q.push(children);
}
}
return count;
}
int main()
{
vector< int > adj[10];
adj[1].push_back(2);
adj[1].push_back(3);
adj[2].push_back(4);
adj[2].push_back(5);
adj[2].push_back(6);
adj[3].push_back(9);
adj[5].push_back(7);
adj[5].push_back(8);
int root = 1;
cout << countNodes(adj, root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countNodes(Vector<Integer> adj[], int root)
{
int count = 0 ;
Queue<Integer> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty())
{
int node = q.peek();
q.remove();
for ( int i= 0 ;i<adj[node].size();i++)
{
int children = adj[node].get(i);
if (adj[children].size() > adj[node].size())
count++;
q.add(children);
}
}
return count;
}
public static void main(String[] args)
{
Vector<Integer> []adj = new Vector[ 10 ];
for ( int i= 0 ; i < 10 ; i++) {
adj[i] = new Vector<>();
}
adj[ 1 ].add( 2 );
adj[ 1 ].add( 3 );
adj[ 2 ].add( 4 );
adj[ 2 ].add( 5 );
adj[ 2 ].add( 6 );
adj[ 3 ].add( 9 );
adj[ 5 ].add( 7 );
adj[ 5 ].add( 8 );
int root = 1 ;
System.out.print(countNodes(adj, root));
}
}
|
Python3
from collections import deque
adj = [[] for i in range ( 100 )]
def countNodes(root):
count = 0
q = deque()
q.append(root)
while len (q) > 0 :
node = q.popleft()
for i in adj[node]:
children = i
if ( len (adj[children]) > len (adj[node])):
count + = 1
q.append(children)
return count
adj[ 1 ].append( 2 )
adj[ 1 ].append( 3 )
adj[ 2 ].append( 4 )
adj[ 2 ].append( 5 )
adj[ 2 ].append( 6 )
adj[ 3 ].append( 9 )
adj[ 5 ].append( 7 )
adj[ 5 ].append( 8 )
root = 1
print (countNodes(root))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countNodes(List< int > []adj, int root)
{
int count = 0;
List< int > q = new List< int >();
q.Add(root);
while (q.Count != 0)
{
int node = q[0];
q.RemoveAt(0);
for ( int i = 0; i < adj[node].Count; i++)
{
int children = adj[node][i];
if (adj[children].Count > adj[node].Count)
count++;
q.Add(children);
}
}
return count;
}
public static void Main(String[] args)
{
List< int > []adj = new List< int >[10];
for ( int i= 0; i < 10 ; i++) {
adj[i] = new List< int >();
}
adj[1].Add(2);
adj[1].Add(3);
adj[2].Add(4);
adj[2].Add(5);
adj[2].Add(6);
adj[3].Add(9);
adj[5].Add(7);
adj[5].Add(8);
int root = 1;
Console.Write(countNodes(adj, root));
}
}
|
Javascript
<script>
function countNodes(adj,root)
{
let count = 0;
let q = [];
q.push(root);
while (q.length!=0)
{
let node = q[0];
q.shift();
for ( let i=0;i<adj[node].length;i++)
{
let children = adj[node][i];
if (adj[children].length > adj[node].length)
count++;
q.push(children);
}
}
return count;
}
let adj = [];
for (let i= 0; i < 10 ; i++) {
adj[i] = [];
}
adj[1].push(2);
adj[1].push(3);
adj[2].push(4);
adj[2].push(5);
adj[2].push(6);
adj[3].push(9);
adj[5].push(7);
adj[5].push(8);
let root = 1;
document.write(countNodes(adj, root));
</script>
|
Time Complexity: O(n), where n is the number of nodes in the tree.
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...