The Wayback Machine - https://web.archive.org/web/20240725124433/https://www.geeksforgeeks.org/coefficient-of-range-in-a-binary-tree/
Open In App

Coefficient of Range in a Binary Tree

Last Updated : 13 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Given a Binary Tree, the task is to find the Coefficient of Range in it.
Range is defined as the difference between the maximum and minimum value in a set of data and Coefficient of Range is the relative measure of the dispersion of the range. Suppose the maximum value in a data set is maxVal and minimum value is minVal then the coefficient of range can be defined as: 
 

Coefficient of range = (maxVal – minVal)/(maxVal + minVal) 
 

Consider the below Binary Tree: 
 

For example, maximum in the above Binary Tree is 9 and minimum is 1 so coefficient of range is ((9 – 1)/ ( 9 + 1)) = 0.8. 
 

Approach: In Binary Search Tree, we can find maximum by traversing right pointers until we reach rightmost node. But in Binary Tree, we must visit every node to figure out maximum. So the idea is to traverse the given tree and for every node return maximum of 3 values. 
 

  1. Node’s data.
  2. Maximum in node’s left subtree.
  3. Maximum in node’s right subtree.

Similarly, find the minimum value in the Binary Tree and calculate the coefficient of range.
Below is the implementation of the above approach: 
 

C++




// CPP program to find Coefficient of
// Range in a Binary Tree
  
#include <bits/stdc++.h>
using namespace std;
  
// A tree node
struct Node {
    float data;
    struct Node *left, *right;
};
  
// A utility function to create a new node
struct Node* newNode(float data)
{
    struct Node* newnode = new Node();
    newnode->data = data;
    newnode->left = newnode->right = NULL;
    return (newnode);
}
  
// Returns maximum value in a given Binary Tree
float findMax(struct Node* root)
{
    // Base case
    if (root == NULL)
        return INT_MIN;
  
    // Return maximum of 3 values:
    // 1) Root's data 2) Max in Left Subtree
    // 3) Max in right subtree
    float res = root->data;
    float lres = findMax(root->left);
    float rres = findMax(root->right);
    if (lres > res)
        res = lres;
    if (rres > res)
        res = rres;
  
    return res;
}
  
// Returns minimum value in a given Binary Tree
float findMin(struct Node* root)
{
    // Base case
    if (root == NULL)
        return INT_MAX;
  
    // Return minimum of 3 values:
    // 1) Root's data 2) Min in Left Subtree
    // 3) Min in right subtree
    float res = root->data;
    float lres = findMin(root->left);
    float rres = findMin(root->right);
    if (lres < res)
        res = lres;
    if (rres < res)
        res = rres;
  
    return res;
}
  
// Function to find the value of the Coefficient
// of range in the Binary Tree
float coefRange(Node* root)
{
    float max = findMax(root);
    float min = findMin(root);
  
    return (max - min) / (max + min);
}
  
// Driver Code
int main(void)
{
    // Construct the Binary Tree
    struct Node* root = newNode(2);
    root->left = newNode(7);
    root->right = newNode(5);
    root->left->right = newNode(6);
    root->left->right->left = newNode(1);
    root->left->right->right = newNode(11);
    root->right->right = newNode(9);
    root->right->right->left = newNode(4);
  
    cout << "Coefficient of Range is " << coefRange(root);
  
    return 0;
}


Java




// JAVA program to find Coefficient of
// Range in a Binary Tree
  
class GFG
{
  
// A tree node
static class Node
{
    float data;
    Node left, right;
};
  
// A utility function to create a new node
static Node newNode(float data)
{
    Node node = new Node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
  
// Returns maximum value in a given Binary Tree
static float findMax(Node root)
{
    // Base case
    if (root == null)
        return Integer.MIN_VALUE;
  
    // Return maximum of 3 values:
    // 1) Root's data 2) Max in Left Subtree
    // 3) Max in right subtree
    float res = root.data;
    float lres = findMax(root.left);
    float rres = findMax(root.right);
    if (lres > res)
        res = lres;
    if (rres > res)
        res = rres;
  
    return res;
}
  
// Returns minimum value in a given Binary Tree
static float findMin(Node root)
{
    // Base case
    if (root == null)
        return Integer.MAX_VALUE;
  
    // Return minimum of 3 values:
    // 1) Root's data 2) Min in Left Subtree
    // 3) Min in right subtree
    float res = root.data;
    float lres = findMin(root.left);
    float rres = findMin(root.right);
    if (lres < res)
        res = lres;
    if (rres < res)
        res = rres;
  
    return res;
}
  
// Function to find the value of the Coefficient
// of range in the Binary Tree
static float coefRange(Node root)
{
    float max = findMax(root);
    float min = findMin(root);
  
    return (max - min) / (max + min);
}
  
// Driver Code
public static void main(String[] args)
{
    // Construct the Binary Tree
    Node root = newNode(2);
    root.left = newNode(7);
    root.right = newNode(5);
    root.left.right = newNode(6);
    root.left.right.left = newNode(1);
    root.left.right.right = newNode(11);
    root.right.right = newNode(9);
    root.right.right.left = newNode(4);
  
    System.out.print("Coefficient of Range is " + coefRange(root));
}
}
  
// This code is contributed by PrinciRaj1992


Python3




# Python3 program to find Coefficient of
# Range in a Binary Tree
from sys import maxsize
INT_MIN = -maxsize
INT_MAX = maxsize
  
# A tree node
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
  
# Returns maximum value in a given Binary Tree
def findMax(root: Node) -> float:
  
    # Base case
    if root is None:
        return INT_MIN
  
    # Return maximum of 3 values:
    # 1) Root's data 2) Max in Left Subtree
    # 3) Max in right subtree
    res = root.data
    lres = findMax(root.left)
    rres = findMax(root.right)
    if lres > res:
        res = lres
    if rres > res:
        res = rres
  
    return res
  
# Returns minimum value in a given Binary Tree
def findMin(root: Node) -> float:
  
    # Base case
    if root is None:
        return INT_MAX
  
    # Return minimum of 3 values:
    # 1) Root's data 2) Min in Left Subtree
    # 3) Min in right subtree
    res = root.data
    lres = findMin(root.left)
    rres = findMin(root.right)
    if lres < res:
        res = lres
    if rres < res:
        res = rres
  
    return res
  
# Function to find the value of the Coefficient
# of range in the Binary Tree
def coefRange(root: Node) -> float:
    maxx = findMax(root)
    minn = findMin(root)
  
    return (maxx - minn) / (maxx + minn)
  
# Driver Code
if __name__ == "__main__":
  
    # Construct the Binary Tree
    root = Node(2)
    root.left = Node(7)
    root.right = Node(5)
    root.left.right = Node(6)
    root.left.right.left = Node(1)
    root.left.right.right = Node(11)
    root.right.right = Node(9)
    root.right.right.left = Node(4)
  
    print("Coefficient of Range is", coefRange(root))
  
# This code is contributed by
# sanjeev2552


C#




// C# program to find Coefficient of
// Range in a Binary Tree
using System;
  
class GFG
{
  
// A tree node
class Node
{
    public float data;
    public Node left, right;
};
  
// A utility function to create a new node
static Node newNode(float data)
{
    Node node = new Node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
  
// Returns maximum value in a given Binary Tree
static float findMax(Node root)
{
    // Base case
    if (root == null)
        return int.MinValue;
  
    // Return maximum of 3 values:
    // 1) Root's data 2) Max in Left Subtree
    // 3) Max in right subtree
    float res = root.data;
    float lres = findMax(root.left);
    float rres = findMax(root.right);
    if (lres > res)
        res = lres;
    if (rres > res)
        res = rres;
  
    return res;
}
  
// Returns minimum value in a given Binary Tree
static float findMin(Node root)
{
    // Base case
    if (root == null)
        return int.MaxValue;
  
    // Return minimum of 3 values:
    // 1) Root's data 2) Min in Left Subtree
    // 3) Min in right subtree
    float res = root.data;
    float lres = findMin(root.left);
    float rres = findMin(root.right);
    if (lres < res)
        res = lres;
    if (rres < res)
        res = rres;
  
    return res;
}
  
// Function to find the value of the Coefficient
// of range in the Binary Tree
static float coefRange(Node root)
{
    float max = findMax(root);
    float min = findMin(root);
  
    return (max - min) / (max + min);
}
  
// Driver Code
public static void Main(String[] args)
{
    // Construct the Binary Tree
    Node root = newNode(2);
    root.left = newNode(7);
    root.right = newNode(5);
    root.left.right = newNode(6);
    root.left.right.left = newNode(1);
    root.left.right.right = newNode(11);
    root.right.right = newNode(9);
    root.right.right.left = newNode(4);
  
    Console.Write("Coefficient of Range is " + 
                             coefRange(root));
}
}
  
// This code is contributed by Rajput-Ji


Javascript




<script>
// javascript program to find Coefficient of
// Range in a Binary Tree
    // A tree node
class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}
  
    // A utility function to create a new node
    function newNode(data) {
var node = new Node();
        node.data = data;
        node.left = node.right = null;
        return (node);
    }
  
    // Returns maximum value in a given Binary Tree
    function findMax(root) {
        // Base case
        if (root == null)
            return Number.MIN_VALUE;
  
        // Return maximum of 3 values:
        // 1) Root's data 2) Max in Left Subtree
        // 3) Max in right subtree
        var res = root.data;
        var lres = findMax(root.left);
        var rres = findMax(root.right);
        if (lres > res)
            res = lres;
        if (rres > res)
            res = rres;
  
        return res;
    }
  
    // Returns minimum value in a given Binary Tree
    function findMin(root) {
        // Base case
        if (root == null)
            return Number.MAX_VALUE;
  
        // Return minimum of 3 values:
        // 1) Root's data 2) Min in Left Subtree
        // 3) Min in right subtree
        var res = root.data;
        var lres = findMin(root.left);
        var rres = findMin(root.right);
        if (lres < res)
            res = lres;
        if (rres < res)
            res = rres;
  
        return res;
    }
  
    // Function to find the value of the Coefficient
    // of range in the Binary Tree
    function coefRange(root) {
        var max = findMax(root);
        var min = findMin(root);
  
        return (max - min) / (max + min);
    }
  
    // Driver Code
      
        // Construct the Binary Tree
var root = newNode(2);
        root.left = newNode(7);
        root.right = newNode(5);
        root.left.right = newNode(6);
        root.left.right.left = newNode(1);
        root.left.right.right = newNode(11);
        root.right.right = newNode(9);
        root.right.right.left = newNode(4);
  
        document.write("Coefficient of Range is " + coefRange(root).toFixed(6));
  
// This code contributed by umadevi9616 
</script>


Output: 

Coefficient of Range is 0.833333

 

Time complexity : O(n) where n is the number of nodes.  
Auxiliary Space: O(n)



Similar Reads

Range and Coefficient of range of Array
Given an array arr of integer elements, the task is to find the range and coefficient of range of the given array where: Range: Difference between the maximum value and the minimum value in the distribution. Coefficient of Range: (Max – Min) / (Max + Min). Examples: Input: arr[] = {15, 16, 10, 9, 6, 7, 17} Output: Range : 11 Coefficient of Range :
7 min read
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Convert a Binary Tree into its Mirror Tree (Invert Binary Tree)
Given a binary tree, the task is to convert the binary tree into its Mirror tree. Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged. Recommended PracticeMirror TreeTry It!The idea is to traverse recursively and swap the right and left subtrees after traversing the subtrees. Follow
15+ min read
Replace the maximum element in the array by coefficient of range
Given an array arr[] of integer elements, the task is to replace the maximum element in the array by the coefficient of range of the same array. Coefficient of Range: (Max – Min) / (Max + Min) Examples: Input: arr[] = {15, 16, 10, 9, 6, 7, 17} Output: 15 16 10 9 6 7 0.478261 Max = 17, Min = 6 Coefficient of Range = (Max – Min) / (Max + Min) = 11 /
8 min read
Binary Indexed Tree : Range Update and Range Queries
Given an array arr[0..N-1]. The following operations need to be performed. update(l, r, val): Add ‘val’ to all the elements in the array from [l, r].getRangeSum(l, r): Find the sum of all elements in the array from [l, r].Initially, all the elements in the array are 0. Queries can be in any order, i.e., there can be many updates before range sum. E
15+ min read
Convert a Generic Tree(N-array Tree) to Binary Tree
Prerequisite: Generic Trees(N-array Trees) In this article, we will discuss the conversion of the Generic Tree to a Binary Tree. Following are the rules to convert a Generic(N-array Tree) to a Binary Tree: The root of the Binary Tree is the Root of the Generic Tree.The left child of a node in the Generic Tree is the Left child of that node in the B
13 min read
Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
We have discussed Threaded Binary Tree. The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without recursion. In a simple threaded binary tree, the NULL right pointers are used to store inorder successor. Wherever a right pointer is NULL, it is used to store inorder successor. The following diagram sho
12 min read
Check whether a binary tree is a full binary tree or not | Iterative Approach
Given a binary tree containing n nodes. The problem is to check whether the given binary tree is a full binary tree or not. A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has only one child node. Examples: Input : 1 / \ 2 3 / \ 4 5 Outp
8 min read
Binary Tree to Binary Search Tree Conversion using STL set
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of the Binary Tree.This solution will use Sets of C++ STL instead of array-based solution. Examples: Example 1 Input: 10 / \ 2 7 / \ 8 4 Output: 8 / \ 4 10 / \ 2 7 Example 2 Input: 10 / \ 30 15 / \ 20 5 Output: 15 / \
12 min read
Check whether a given binary tree is skewed binary tree or not?
Given a Binary Tree check whether it is skewed binary tree or not. A skewed tree is a tree where each node has only one child node or none. Examples: Input : 5 / 4 \ 3 / 2 Output : Yes Input : 5 / 4 \ 3 / \ 2 4 Output : No The idea is to check if a node has two children. If node has two children return false, else recursively compute whether subtre
13 min read
three90RightbarBannerImg