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:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find color of the node
#include <bits/stdc++.h>
using namespace std;
  
// Function to find color of the node
int findColor(map<int, int> mapWithColor, int query)
{
    // Maximum is to store maximum color
    int maximum = 0;
  
    // Loop to check all the parent 
    // values to get maximum color
    while (query >= 1) {
  
        // Find the number into map and get maximum color
        if (mapWithColor.find(query) != mapWithColor.end())
        {
            // Take the maximum color and
            // assign into maximum variable
            maximum = max(maximum, mapWithColor[query]);
        }
  
        // Find parent index
        if (query % 2 == 1)
            query = (query - 1) / 2;
        else
            query = query / 2;
    }
  
    // Return maximum color
    return maximum;
}
  
// Function to build hash map with color
map<int, int> buildMapWithColor(int arr[], int n)
{
    // To store color of each node
    map<int, int> mapWithColor;
  
    // For each number add a color number
    for (int i = 0; i < n; i++) {
        // Assigning color
        mapWithColor[arr[i]] = i + 1;
    }
  
    // Return hash map
    return mapWithColor;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 2, 1, 3 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    int k = 7;
  
    // Build mapWithColor
    map<int, int> mapWithColor = buildMapWithColor(arr, n);
  
    // Print the maximum color
    cout << findColor(mapWithColor, k);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code program to find color of the node
  
# Function to find color of the node
def findColor(mapWithColor, query):
  
    # Maximum is to store maximum color
    maximum = 0
  
    # Loop to check all the parent
    # values to get maximum color
    while (query >= 1):
  
        # Find the number into map and get maximum color
        if (query) in mapWithColor.keys():
            # Take the maximum color and
            # assign into maximum variable
            maximum = max(maximum, mapWithColor[query])
  
        # Find parent index
        if (query % 2 == 1):
            query = (query - 1) // 2
        else:
            query = query // 2
  
  
    # Return maximum color
    return maximum
  
# Function to build hash map with color
def buildMapWithColor(arr, n):
    # To store color of each node
    mapWithColor={}
  
    # For each number add a color number
    for i in range(n):
        # Assigning color
        mapWithColor[arr[i]] = i + 1
  
    # Return hash map
    return mapWithColor
  
  
# Driver code
  
arr = [3, 2, 1, 3]
  
n = len(arr)
  
k = 7
  
# Build mapWithColor
mapWithColor = buildMapWithColor(arr, n)
  
# Prthe maximum color
print(findColor(mapWithColor, k))
  
# This code is contributed by mohit kumar 29

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29