

Find the cousins of a given element in an N-ary tree
source link: https://www.geeksforgeeks.org/find-the-cousins-of-a-given-element-in-an-n-ary-tree/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find the cousins of a given element in an N-ary tree
- Last Updated : 12 Jan, 2022
Given an N-array tree root and an integer K, the task is to print all the cousins of node K.
Note: Two nodes are considererd to be cousins if they have the same depth (are on the same level) and have different parents.
Examples:
Consider the below tree:
Input: K = 39
Ouput: 88 98 61 74 17 72 19Input: K = 17
Ouput: 88 98 61 74 39Input: K = 90
Ouput: NA
Approach: The idea is to do a level order traversal. During the traversal, if we find a node whose child is equal to the given element, then we will not push the children of this node. We will push the children of the other nodes and the inner loop will end when all the elements of that level are traversed. Follow the steps below to solve the problem:
- If root equals null, then return.
- Initialize the queue q[] and push root into the queue q[].
- Initialize the boolean variable found as false.
- Initialize the variables qsize as 0 and node temp.
- Iterate over the while loop till q[] is not empty and node is not found and perform the following tasks:
- Set size qsize as the size of the queue q[].
- Iterate over the while loop till qsize is greater than 0 and perform the following tasks:
- Set tempp as the front of the queue q[].
- De queue from the queue q[].
- If found equals true, then push all it’s children into the queue q[].
- Iterate over the range [0, temp->child.size()) using the variable i and perform the following tasks:
- If the child is not null and it’s key equals value, then set the value of found as true.
- If found is false, then push all it’s children into the queue q[].
- Decrease the value of qsize by 1.
- If found is false, then print “Not Possible.”
- Else, Initialize the variables qsize as the size of the queue q[].
- If qsize equals 0 then print “No Cousins”.
- Else, print all the elements of the queue q[].
Below is the implementation of the above approach
// C++ program for the above approach
#include <bits/stdc++.h>
using
namespace
std;
// Structure of a node of N-ary tree
struct
Node {
int
key;
vector<Node*> child;
};
// New node creation
Node* newNode(
int
key)
{
Node* temp =
new
Node;
temp->key = key;
return
temp;
}
// Function to find the cousins of a
// given node in an N-array tree
void
printCousins(Node* root,
int
value)
{
// Base case
if
(root == NULL)
return
;
queue<Node*> q;
q.push(root);
// If we find the node
// with value as the key
bool
found =
false
;
int
qsize = 0;
Node* tempp;
while
(!q.empty() && !found) {
qsize = q.size();
while
(qsize) {
// Storing the current node
tempp = q.front();
q.pop();
// If we have already found
// the value as child of a node,
// we need to insert children of other
// node of same level in the queue
if
(found ==
true
) {
for
(
int
i = 0; i < tempp->child.size();
i++) {
if
(tempp->child[i] != NULL)
q.push(tempp->child[i]);
}
}
// If value is child of tempp node
for
(
int
i = 0; i < tempp->child.size(); i++)
if
(tempp->child[i] != NULL
&& tempp->child[i]->key == value)
found =
true
;
// If value is not the child of tempp node
// then insert all the children
// of the tempp node
if
(found ==
false
) {
for
(
int
i = 0; i < tempp->child.size();
i++) {
if
(tempp->child[i] != NULL)
q.push(tempp->child[i]);
}
}
qsize--;
}
}
if
(found) {
// Queue will contain the cousins
qsize = q.size();
if
(qsize == 0)
cout <<
"NA"
;
for
(
int
i = 0; i < qsize; i++) {
tempp = q.front();
q.pop();
cout << tempp->key <<
" "
;
}
}
else
{
// When value is not in the tree
cout <<
"Not Possible"
;
}
cout <<
"\n"
;
return
;
}
// Driver Code
int
main()
{
Node* root = newNode(10);
(root->child).push_back(newNode(77));
(root->child).push_back(newNode(90));
(root->child).push_back(newNode(35));
(root->child).push_back(newNode(19));
(root->child[0]->child).push_back(newNode(88));
(root->child[0]->child).push_back(newNode(98));
(root->child[0]->child[1]->child)
.push_back(newNode(76));
(root->child[0]->child[1]->child)
.push_back(newNode(20));
(root->child[1]->child).push_back(newNode(61));
(root->child[1]->child).push_back(newNode(74));
(root->child[2]->child).push_back(newNode(39));
(root->child[3]->child).push_back(newNode(17));
(root->child[3]->child).push_back(newNode(72));
(root->child[3]->child).push_back(newNode(19));
// Find the cousins of value
int
value = 39;
printCousins(root, value);
return
0;
}
Cousins for the element 39: 88 98 61 74 17 72 19
Time Complexity: O(N)
Auxiliary Space: O(N)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK