Coefficient of Range in a Binary Tree
Last Updated :
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.
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
Count of Fibonacci paths in a Binary tree
Given a Binary Tree, the task is to count the number of Fibonacci paths in the given Binary Tree. Fibonacci Path is a path which contains all nodes in root to leaf path are terms of Fibonacci series. Example: Input: 0 / \ 1 1 / \ / \ 1 10 70 1 / \ 81 2 Output: 2 Explanation: There are 2 Fibonacci pa
10 min read
Find the closest leaf in a Binary Tree
Given a Binary Tree and a key 'k', find distance of the closest leaf from 'k'. Examples: A / \ B C / \ E F / \ G H / \ / I J K Closest leaf to 'H' is 'K', so distance is 1 for 'H' Closest leaf to 'C' is 'B', so distance is 2 for 'C' Closest leaf to 'E' is either 'I' or 'J', so distance is 2 for 'E'
14 min read
Count of exponential paths in a Binary Tree
Given a Binary Tree, the task is to count the number of Exponential paths in the given Binary Tree. Exponential Path is a path where root to leaf path contains all nodes being equal to xy, & where x is a minimum possible positive constant & y is a variable positive integer. Example: Input: 2
11 min read
Count Non-Leaf nodes in a Binary Tree
Given a Binary tree, count the total number of non-leaf nodes in the tree Examples: Input : Output :2 Explanation In the above tree only two nodes 1 and 2 are non-leaf nodesRecommended PracticeCount Non-Leaf Nodes in TreeTry It! We recursively traverse the given tree. While traversing, we count non-
10 min read
Ceil in a Binary Search Tree Python
In a Binary Search Tree (BST), the ceiling of a given value is the smallest element in the tree that is greater than or equal to the given value. Finding the ceiling in a BST can be a useful operation when working with ordered data. In this article, we will explore how to find the ceiling in a Binar
3 min read
Count of 1's in any path in a Binary Tree
Given a binary tree of 0s and 1s, the task is to find the maximum number of 1s in any path in the tree. The path may start and end at any node in the tree.Example: Input: 1 / \ 0 1 / \ 1 1 / \ 1 0 Output: 4 Approach: A function countUntil has been created which returns the maximum count of 1 in any
10 min read
Check if a Binary Tree is BST or not
Given the root of a binary tree. Check whether it is a Binary Search Tree or not. A Binary Search Tree (BST) is a node-based binary tree data structure with the following properties. All keys in the left subtree are smaller than the root and all keys in the right subtree are greater.Both the left an
15+ min read
Density of Binary Tree in One Traversal
Given a Binary Tree, the task is to find the density of it by doing one traversal of it. Density of Binary Tree = (Size / Height). Examples: Input: Output: 1.5Explanation: As the height of given tree is 2 and size of given tree is 3 , the density is (3/2) = 1.5Input: Output: 1Explanation: As the hei
6 min read
Deepest left leaf node in a binary tree
Given a Binary Tree, find the deepest leaf node that is left child of its parent. For example, consider the following tree. The deepest left leaf node is the node with value 9. 1 / \ 2 3 / / \ 4 5 6 \ \ 7 8 / \ 9 10 The idea is to recursively traverse the given binary tree and while traversing, main
13 min read
Check if the given n-ary tree is a binary tree
Given an n-ary tree consisting of n nodes, the task is to check whether the given tree is binary or not.Note: An n-ary tree is a tree where each node can have zero or more children nodes. Unlike a binary tree, which has at most two children per node (left and right), the n-ary tree allows for multip
6 min read