Coefficient of Range in a Binary Tree - GeeksforGeeks

13 Sep, 2021
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.  
Node’s data.Maximum in node’s left subtree.Maximum in node’s right subtree.
Node’s data.
Maximum in node’s left subtree.
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++
Java
Python3
C#
Javascript
// CPP program to find Coefficient of// Range in a Binary Tree  #include <bits/stdc++.h>using namespace std;  // A tree nodestruct Node {    float data;    struct Node *left, *right;};  // A utility function to create a new nodestruct 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 Treefloat 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 Treefloat 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 Treefloat coefRange(Node* root){    float max = findMax(root);    float min = findMin(root);      return (max - min) / (max + min);}  // Driver Codeint 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 program to find Coefficient of// Range in a Binary Tree  class GFG{  // A tree nodestatic class Node{    float data;    Node left, right;};  // A utility function to create a new nodestatic 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 Treestatic 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 Treestatic 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 Treestatic float coefRange(Node root){    float max = findMax(root);    float min = findMin(root);      return (max - min) / (max + min);}  // Driver Codepublic 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 program to find Coefficient of# Range in a Binary Treefrom sys import maxsizeINT_MIN = -maxsizeINT_MAX = maxsize  # A tree nodeclass Node:    def __init__(self, data):        self.data = data        self.left = None        self.right = None  # Returns maximum value in a given Binary Treedef 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 Treedef 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 Treedef coefRange(root: Node) -> float:    maxx = findMax(root)    minn = findMin(root)      return (maxx - minn) / (maxx + minn)  # Driver Codeif __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# program to find Coefficient of// Range in a Binary Treeusing System;  class GFG{  // A tree nodeclass Node{    public float data;    public Node left, right;};  // A utility function to create a new nodestatic 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 Treestatic 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 Treestatic 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 Treestatic float coefRange(Node root){    float max = findMax(root);    float min = findMin(root);      return (max - min) / (max + min);}  // Driver Codepublic 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
<script>// javascript program to find Coefficient of// Range in a Binary Tree    // A tree nodeclass 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 Treevar 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>
Coefficient of Range is 0.833333
 
Time complexity : O(n) where n is the number of nodes.  Auxiliary Space: O(n)
VishalBachchas
nidhi_biet
princiraj1992
Rajput-Ji
sanjeev2552
Akanksha_Rai
umadevi9616
surindertarika1234
pankajsharmagfg
Binary Tree
Technical Scripter 2018
Traversal
Data Structures
Technical Scripter
Tree
Data Structures
Traversal
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Tree Data Structure
Program to implement Singly Linked List in C++ using class
Insertion in a B+ tree
Hash Functions and list/types of Hash functions
Shortest path in a directed graph by Dijkstra’s algorithm
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
AVL Tree | Set 1 (Insertion)
Inorder Tree Traversal without Recursion