Coefficient of Range in a Binary Tree
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 coeffcient 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.
Similarly, find the minimum value in the Binary Tree and calculate the coefficient of range.
Below is the implementation of the above approach:
// CPP program to find Coefficient of // Range in a Bianry 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* node = (struct Node*) malloc(sizeof(struct Node)); node->data = data; node->left = node->right = NULL; return (node); } // 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) { // Contruct 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; } |
Coefficient of Range is 0.833333
Time complexity : O(n) where n is the number of nodes.
Recommended Posts:
- Binary Indexed Tree : Range Update and Range Queries
- Range and Coefficient of range of Array
- Binary Indexed Tree : Range Updates and Point Queries
- Replace the maximum element in the array by coefficient of range
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion using STL set
- Check whether a given binary tree is skewed binary tree or not?
- Check whether a binary tree is a full binary tree or not
- Check if a binary tree is subtree of another binary tree | Set 2
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 1
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 : VishalBachchas



