Find the type of Tree based on the child count
Last Updated :
28 Aug, 2023
Given a tree that has some nodes and each node has a certain number of child nodes, the task is to find the type of tree based on the child count. For example, if each node has two children, then we can say it is a binary tree, and if each node has almost three children, then we can say it is a Ternary tree.
Examples:
Input: A
/ | \
B C D
/ | \ / \
E F G H K
Output: Ternary Tree
Explanation: Here we can observe that each node has at most 3 children, which is basically the property of a ternary tree.
Input: A
/ \
B C
/ \ / \
E F H K
Output: Binary Tree
Explanation: Here we can observe that each node has 2 children, which is basically the property of a binary tree
Naive Approach: To solve the problem follow the below idea:
The basic way is to find the type of tree from the child count, we can iterate over each node and check the count of the child each node has and then we can easily determine the type of tree. It can be carried out in three steps first we traverse each node then we keep the count of children at each node and then at last we compare it with the conditions to get the type of the tree.
Time Complexity: O(N), where N is the number of nodes
Auxiliary Space: O(1)
Efficient Approach: In this approach, instead of keeping track of the child count of each node, we will take advantage of the properties of the specific tree that will help us to determine the type of tree without increasing the complexity of the program.
Dry run: Please consider the below example to understand the above approach easily
Tree: A
/ | \
B C D
/ | \ | \
E F G H K
In this example, the tree has 9 nodes
- Traverse each node in the tree.
- Check the child count of each node based on the predefined criteria for different types of trees.
- Identify the type of tree based on the child count of the nodes.
Here we can see that each node present in the tree has at most three children, from which we can say that this tree is a ternary tree in nature based on the count of the node.
Below is the implementation of the above approach in Python:
C++
#include <iostream>
#include <vector>
class Node {
public :
int data;
std::vector<Node*> children;
Node( int data) {
this ->data = data;
}
};
std::string functionTree(Node* root) {
int max_child_count = 0;
for (Node* child : root->children) {
int child_count = child->children.size();
if (child_count > max_child_count) {
max_child_count = child_count;
}
}
if (max_child_count <= 2) {
return "Binary Tree" ;
}
else if (max_child_count <= 3) {
return "Ternary Tree" ;
}
else {
return "N-ary Tree" ;
}
}
int main() {
Node* root = new Node( 'A' );
root->children.push_back( new Node( 'B' ));
root->children.push_back( new Node( 'C' ));
root->children.push_back( new Node( 'D' ));
root->children[0]->children.push_back( new Node( 'E' ));
root->children[0]->children.push_back( new Node( 'F' ));
root->children[0]->children.push_back( new Node( 'G' ));
root->children[2]->children.push_back( new Node( 'H' ));
root->children[2]->children.push_back( new Node( 'K' ));
std::string typeofTree = functionTree(root);
std::cout << "Optimized Approach: " << typeofTree << std::endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
class Node {
int data;
List<Node> children;
Node( int data) {
this .data = data;
this .children = new ArrayList<>();
}
}
class GFG {
public static String functionTree(Node root) {
int maxChildCount = 0 ;
for (Node child : root.children) {
int childCount = child.children.size();
if (childCount > maxChildCount) {
maxChildCount = childCount;
}
}
if (maxChildCount <= 2 ) {
return "Binary Tree" ;
} else if (maxChildCount <= 3 ) {
return "Ternary Tree" ;
} else {
return "N-ary Tree" ;
}
}
public static void main(String[] args) {
Node root = new Node( 'A' );
root.children.add( new Node( 'B' ));
root.children.add( new Node( 'C' ));
root.children.add( new Node( 'D' ));
root.children.get( 0 ).children.add( new Node( 'E' ));
root.children.get( 0 ).children.add( new Node( 'F' ));
root.children.get( 0 ).children.add( new Node( 'G' ));
root.children.get( 2 ).children.add( new Node( 'H' ));
root.children.get( 2 ).children.add( new Node( 'K' ));
String typeOfTree = functionTree(root);
System.out.println( "Optimized Approach: " + typeOfTree);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .children = []
def functionTree(root):
max_child_count = 0
for child in root.children:
child_count = len (child.children)
if child_count > max_child_count:
max_child_count = child_count
if max_child_count < = 2 :
return "Binary Tree"
elif max_child_count < = 3 :
return "Ternary Tree"
else :
return "N-ary Tree"
root = Node( 'A' )
root.children.append(Node( 'B' ))
root.children.append(Node( 'C' ))
root.children.append(Node( 'D' ))
root.children[ 0 ].children.extend([Node( 'E' ), Node( 'F' ), Node( 'G' )])
root.children[ 2 ].children.append([Node( 'H' ),Node( 'K' )])
typeofTree = functionTree(root)
print ( "Optimized Approach:" , typeofTree)
|
C#
using System;
using System.Collections.Generic;
class Node
{
public int Data;
public List<Node> Children;
public Node( int data)
{
Data = data;
Children = new List<Node>();
}
}
class GFG
{
public static string FunctionTree(Node root)
{
int maxChildCount = 0;
foreach (Node child in root.Children)
{
int childCount = child.Children.Count;
if (childCount > maxChildCount)
{
maxChildCount = childCount;
}
}
if (maxChildCount <= 2)
{
return "Binary Tree" ;
}
else if (maxChildCount <= 3)
{
return "Ternary Tree" ;
}
else
{
return "N-ary Tree" ;
}
}
public static void Main( string [] args)
{
Node root = new Node( 'A' );
root.Children.Add( new Node( 'B' ));
root.Children.Add( new Node( 'C' ));
root.Children.Add( new Node( 'D' ));
root.Children[0].Children.Add( new Node( 'E' ));
root.Children[0].Children.Add( new Node( 'F' ));
root.Children[0].Children.Add( new Node( 'G' ));
root.Children[2].Children.Add( new Node( 'H' ));
root.Children[2].Children.Add( new Node( 'K' ));
string typeOfTree = FunctionTree(root);
Console.WriteLine( "Optimized Approach: " + typeOfTree);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .children = [];
}
}
function functionTree(root) {
let maxChildCount = 0;
for (let child of root.children) {
let childCount = child.children.length;
if (childCount > maxChildCount) {
maxChildCount = childCount;
}
}
if (maxChildCount <= 2) {
return "Binary Tree" ;
} else if (maxChildCount <= 3) {
return "Ternary Tree" ;
} else {
return "N-ary Tree" ;
}
}
let root = new Node( 'A' );
root.children.push( new Node( 'B' ));
root.children.push( new Node( 'C' ));
root.children.push( new Node( 'D' ));
root.children[0].children.push( new Node( 'E' ));
root.children[0].children.push( new Node( 'F' ));
root.children[0].children.push( new Node( 'G' ));
root.children[2].children.push( new Node( 'H' ));
root.children[2].children.push( new Node( 'K' ));
let typeofTree = functionTree(root);
console.log( "Optimized Approach: " + typeofTree);
|
Output
Optimized Approach: Ternary Tree
Time Complexity: O(N), where N is the number of nodes
Auxiliary Space: O(1)
Note: Although the Time and Space complexity of both brute and optimised approaches is the same, still the optimised approach avoids the redundant count of children at each node and comparison and helps in removing the overhead.
Share your thoughts in the comments
Please Login to comment...