# Identify all Grand-Parent Nodes of each Node in a Map

Given in input that has relationships between a person and their children, for all the people in this data, identify grandparents of all the people in the input.

Input: A map of all the people and their children Map[String, Set[String]] Output: A map of all people and their grandparents Map[String, Set[String]] Example: Input Map(A -> Set(B,C), B -> Set(D, C), C -> Set(E)) Output: Map(D -> Set(A), C -> Set(A), E -> Set(A, B))

**We strongly recommend you to minimize your browser and try this yourself first**

Here we have iterate over each and every node in Map and find out the grand-parent of each child.

The idea is to use Recursion. But, it can be solved without Recursion too.

Lets see the Without Recursion solution first:

**Solution 1 (Without Recursion):**

Here we have to use Mutable Scala Map. Below is the Scala Code.

val input = Map("A" -> Set("B","C"), "B" -> Set("D", "C"), "C" -> Set("E")) val output: scala.collection.mutable.Map[String, Set[String]] = scala.collection.mutable.Map() input.map(node => node._2.map(child => input.get(child).map(grandchildren => grandchildren.map{grandchild => if(output.keys.exists(_ == grandchild)) { output.put(grandchild, output.get(grandchild).get ++ Set(node._1)) } else { output.put(grandchild, Set(node._1)) } } ) ))

Here we are iterating over every Node of Map and finding out the grandchildren of each child in node.This, will help us in creating a Map with Grand-Child and Grand-Parents relationship.

**Solution 2 (With Recursion):**

Here we can use Immutable Scala Map as we are using Recursion.

val input = Map("A" -> Set("B","C"), "B" -> Set("D", "C"), "C" -> Set("E")) val output = findGrandparents(input) def findGrandparents(family: Map[String, Set[String]]): Map[String, Set[String]] = { family.foldLeft(Map[String, Set[String]]()){ case (grandParents, oneFamily) => { val grandChildren: Set[String] = oneFamily._2.flatMap(member => family.get(member)).flatten val res = grandChildren.map(child => { grandParents.get(child) match { case None =>(child -> Set(oneFamily._1)) case Some(x) => (child -> (x + oneFamily._1)) } }).toMap grandParents ++ res }

This article is contributed by **Himanshu Gupta**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Print all leaf nodes of a Binary Tree from left to right
- Recursive function to delete k-th node from linked list
- Print alternate nodes of a linked list using recursion
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Find n-th node in Postorder traversal of a Binary Tree
- Recursive Approach to find nth node from the end in the linked list
- Find the node whose xor with x gives minimum value
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Delete nodes which have a greater value on right side using recursion
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Detect cycle in the graph using degrees of nodes of graph
- Print path from root to all nodes in a Complete Binary Tree