Open In App

Diameter of a Binary Indexed Tree with N nodes

Given a Binary Indexed Tree with N nodes except root node 0 (Numbered from 1 to N), Find its diameter. 
Binary Indexed Tree is a tree where parent of a node number X = X – (X & (X – 1)) i.e. last bit is unset in X. The diameter of a tree is the longest simple path between any two leaves. 
Examples: 
 

Input: N = 12 
Output:
Explanation: Path from node 7 to node 11.
 

Input : n = 15 
Output : 7 
 

Approach: 
 

Below are the implementation of the above approach: 
 




#include <bits/stdc++.h>
using namespace std;
 
// Function to find diameter
// of BIT with N + 1 nodes
int diameter(int n)
{
    // L is size of subtree just before subtree
    // in which N lies
    int L, H, templen;
    L = 1;
 
    // H is the height of subtree just before
    // subtree in which N lies
    H = 0;
 
    // Base Cases
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 2;
    }
    if (n == 3) {
        return 3;
    }
 
    // Size of subtree are power of 2
    while (L * 2 <= n) {
        L *= 2;
        H++;
    }
 
    // 3 Cases as explained in Approach
    if (n >= L * 2 - 1)
        return 2 * H + 1;
    else if (n >= L + (L / 2) - 1)
        return 2 * H;
    return 2 * H - 1;
}
 
// Driver Code
int main()
{
    int n = 15;
    cout << diameter(n) << endl;
}




// Java implementation of the approach
class GFG
{
 
// Function to find diameter
// of BIT with N + 1 nodes
static int diameter(int n)
{
    // L is size of subtree just before subtree
    // in which N lies
    int L, H, templen;
    L = 1;
  
    // H is the height of subtree just before
    // subtree in which N lies
    H = 0;
  
    // Base Cases
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 2;
    }
    if (n == 3) {
        return 3;
    }
  
    // Size of subtree are power of 2
    while (L * 2 <= n) {
        L *= 2;
        H++;
    }
  
    // 3 Cases as explained in Approach
    if (n >= L * 2 - 1)
        return 2 * H + 1;
    else if (n >= L + (L / 2) - 1)
        return 2 * H;
    return 2 * H - 1;
}
  
// Driver Code
public static void main(String []args)
{
    int n = 15;
 
    System.out.println(diameter(n));
}
}
 
// This code contributed by PrinciRaj1992




# Python3 implementation of the approach
 
# Function to find diameter
# of BIT with N + 1 nodes
def diameter(n):
     
    # L is size of subtree just before
    # subtree in which N lies
    L, H, templen = 0, 0, 0;
    L = 1;
 
    # H is the height of subtree just before
    # subtree in which N lies
    H = 0;
 
    # Base Cases
    if (n == 1):
        return 1;
     
    if (n == 2):
        return 2;
     
    if (n == 3):
        return 3;
 
    # Size of subtree are power of 2
    while (L * 2 <= n):
        L *= 2;
        H += 1;
     
    # 3 Cases as explained in Approach
    if (n >= L * 2 - 1):
        return 2 * H + 1;
    elif (n >= L + (L / 2) - 1):
        return 2 * H;
    return 2 * H - 1;
 
# Driver Code
n = 15;
print(diameter(n));
 
# This code is contributed by Rajput-Ji




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to find diameter
// of BIT with N + 1 nodes
static int diameter(int n)
{
    // L is size of subtree just before subtree
    // in which N lies
    int L, H;
    L = 1;
 
    // H is the height of subtree just before
    // subtree in which N lies
    H = 0;
 
    // Base Cases
    if (n == 1)
    {
        return 1;
    }
    if (n == 2)
    {
        return 2;
    }
    if (n == 3)
    {
        return 3;
    }
 
    // Size of subtree are power of 2
    while (L * 2 <= n)
    {
        L *= 2;
        H++;
    }
 
    // 3 Cases as explained in Approach
    if (n >= L * 2 - 1)
        return 2 * H + 1;
    else if (n >= L + (L / 2) - 1)
        return 2 * H;
    return 2 * H - 1;
}
 
// Driver Code
public static void Main(String []args)
{
    int n = 15;
 
    Console.WriteLine(diameter(n));
}
}
 
// This code is contributed by 29AjayKumar




<script>
 
// Function to find diameter
// of BIT with N + 1 nodes
function diameter(n)
{
    // L is size of subtree just before subtree
    // in which N lies
    var L, H, templen;
    L = 1;
 
    // H is the height of subtree just before
    // subtree in which N lies
    H = 0;
 
    // Base Cases
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 2;
    }
    if (n == 3) {
        return 3;
    }
 
    // Size of subtree are power of 2
    while (L * 2 <= n) {
        L *= 2;
        H++;
    }
 
    // 3 Cases as explained in Approach
    if (n >= L * 2 - 1)
        return 2 * H + 1;
    else if (n >= L + (L / 2) - 1)
        return 2 * H;
    return 2 * H - 1;
}
 
// Driver Code
var n = 15;
document.write( diameter(n));
 
</script>

Output: 
7

 

Time Complexity: O(log n)

Auxiliary Space: O(1)


Article Tags :