Identify all Grand-Parent Nodes of each Node in a Map
Last Updated :
17 May, 2017
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
}
Similar Reads
Find parent of each node in a tree for multiple queries Given a tree with N vertices numbered from 0 to N â 1 and Q query containing nodes in the tree, the task is to find the parent node of the given node for multiple queries. Consider the 0th node as the root node and take the parent of the root node as the root itself.Examples: Tree: 0 / \ 1 2 | / \ 3
8 min read
Print all nodes that are at distance k from a leaf node Given a Binary Tree and a positive integer K, print all nodes that are distance K from a leaf node. Here K distance from a leaf means K levels higher than a leaf node. For example, if K is more than the height of the Binary Tree, then nothing should be printed. Examples: Recommended PracticeNode at
15+ min read
Median of ancestors for each Node of a given tree Given a tree with N vertices from 0 to N - 1 (0th node is root) and val[] where val[i] denotes the value of the ith vertex, the task is to find the array of integers ans[] of length N, where ans[i] denotes the median value of all its ancestors' values including ith vertex. Points to remember: If num
10 min read
Print Levels of all nodes in a Binary Tree Given a Binary Tree and a key, write a function that prints levels of all keys in given binary tree. For example, consider the following tree. If the input key is 3, then your function should return 1. If the input key is 4, then your function should return 3. And for key which is not present in key
7 min read
Print all nodes at distance K from given node: Iterative Approach Given a binary tree, a target node in the binary tree, and an integer value k, the task is to find all the nodes at a distance k from the given target node. No parent pointers are available.Note:You have to return the list in sorted order.The tree will not contain duplicate values.Examples:Input: ta
12 min read