Find the color of given node in an infinite binary tree
Given an infinitely long binary tree having a pattern as below:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ / \ / \
......................
Also given an array arr of size N and a number K. The task is to color all the subtrees of node arr[i] with colour i (1 <= i <= N) one after another. Find the colour of node K after colouring all subtrees.
Note:- Initially all nodes have color 0.
Input :arr[] = {3, 2, 1, 3}, K = 7
Output : 4
Explanation :
After applying the color 1 to subtree at node 3, node 7 contains color 1.
After applying the color 2 to subtree at node 2, node 7 contains color 1.
After applying the color 3 to subtree at node 1, node 7 contains color 3.
After applying the color 4 to subtree at node 3, node 7 contains color 4.
Input : arr[] = {3, 2, 1, 3}, K = 4
Output : 3
Explanation :
After applying the color 1 to subtree at node 3, node 4 contains color 0.
After applying the color 2 to subtree at node 2, node 4 contains color 2.
After applying the color 3 to subtree at node 1, node 4 contains color 3.
After applying the color 4 to subtree at node 3, node 4 contains color 3.
A naive approach is for every index i (1 <= i <= N) update color all nodes in a subtree until the level of the nodes less than the level of given node K. After N such operations print the color of node K.
An efficient approach is to use hashing
- First, insert all the nodes with their respective colors in a map( in case, if a node has more than one color, keep one with the maximum color number).
- Find all ancestors of the given node k and output the color having a maximum number. (If the current node is x, then (x-1)/2 or x/2 are its ancestors)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findColor(map< int , int > mapWithColor, int query)
{
int maximum = 0;
while (query >= 1) {
if (mapWithColor.find(query) != mapWithColor.end())
{
maximum = max(maximum, mapWithColor[query]);
}
if (query % 2 == 1)
query = (query - 1) / 2;
else
query = query / 2;
}
return maximum;
}
map< int , int > buildMapWithColor( int arr[], int n)
{
map< int , int > mapWithColor;
for ( int i = 0; i < n; i++) {
mapWithColor[arr[i]] = i + 1;
}
return mapWithColor;
}
int main()
{
int arr[] = { 3, 2, 1, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 7;
map< int , int > mapWithColor = buildMapWithColor(arr, n);
cout << findColor(mapWithColor, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findColor(HashMap<Integer, Integer> mapWithColor, int query)
{
int maximum = 0 ;
while (query >= 1 )
{
if (mapWithColor.containsKey(query))
{
maximum = Math.max(maximum, mapWithColor.get(query));
}
if (query % 2 == 1 )
query = (query - 1 ) / 2 ;
else
query = query / 2 ;
}
return maximum;
}
static HashMap<Integer, Integer> buildMapWithColor( int arr[], int n)
{
HashMap<Integer, Integer> mapWithColor = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++)
{
mapWithColor.put(arr[i], i + 1 );
}
return mapWithColor;
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 1 , 3 };
int n = arr.length;
int k = 7 ;
HashMap<Integer, Integer> mapWithColor = buildMapWithColor(arr, n);
System.out.print(findColor(mapWithColor, k));
}
}
|
Python3
def findColor(mapWithColor, query):
maximum = 0
while (query > = 1 ):
if (query) in mapWithColor.keys():
maximum = max (maximum, mapWithColor[query])
if (query % 2 = = 1 ):
query = (query - 1 ) / / 2
else :
query = query / / 2
return maximum
def buildMapWithColor(arr, n):
mapWithColor = {}
for i in range (n):
mapWithColor[arr[i]] = i + 1
return mapWithColor
arr = [ 3 , 2 , 1 , 3 ]
n = len (arr)
k = 7
mapWithColor = buildMapWithColor(arr, n)
print (findColor(mapWithColor, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int findColor(Dictionary< int , int > mapWithColor, int query)
{
int maximum = 0;
while (query >= 1)
{
if (mapWithColor.ContainsKey(query))
{
maximum = Math.Max(maximum, mapWithColor[query]);
}
if (query % 2 == 1)
query = (query - 1) / 2;
else
query = query / 2;
}
return maximum;
}
static Dictionary< int , int > buildMapWithColor( int []arr, int n)
{
Dictionary< int , int > mapWithColor = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (mapWithColor.ContainsKey(arr[i]))
mapWithColor[arr[i]] = i + 1;
else
mapWithColor.Add(arr[i], i + 1);
}
return mapWithColor;
}
public static void Main(String[] args)
{
int []arr = { 3, 2, 1, 3 };
int n = arr.Length;
int k = 7;
Dictionary< int , int > mapWithColor = buildMapWithColor(arr, n);
Console.Write(findColor(mapWithColor, k));
}
}
|
Javascript
<script>
function findColor(mapWithColor, query)
{
let maximum = 0;
while (query >= 1)
{
if (mapWithColor.has(query))
{
maximum = Math.max(maximum, mapWithColor.get(query));
}
if (query % 2 == 1)
query = (query - 1) / 2;
else
query = query / 2;
}
return maximum;
}
function buildMapWithColor(arr, n)
{
let mapWithColor = new Map();
for (let i = 0; i < n; i++)
{
mapWithColor.set(arr[i], i + 1);
}
return mapWithColor;
}
let arr = [ 3, 2, 1, 3 ];
let n = arr.length;
let k = 7;
let mapWithColor = buildMapWithColor(arr, n);
document.write(findColor(mapWithColor, k));
</script>
|
Last Updated :
21 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...