📜  二叉树的范围系数

📅  最后修改于: 2022-05-13 01:57:05.662000             🧑  作者: Mango

二叉树的范围系数

给定一棵二叉树,任务是在其中找到范围系数
范围定义为一组数据中最大值和最小值之间的差异,范围系数是范围分散的相对度量。假设一个数据集中的最大值是maxVal ,最小值是minVal ,那么范围系数可以定义为:

考虑下面的二叉树:

例如,上述二叉树中的最大值为 9,最小值为 1,因此范围系数为 ((9 – 1)/ (9 + 1)) = 0.8。

方法:在二叉搜索树中,我们可以通过遍历右指针直到到达最右节点来找到最大值。但是在二叉树中,我们必须访问每个节点以找出最大值。所以想法是遍历给定的树,并为每个节点返回最多 3 个值。

  1. 节点的数据。
  2. 节点左子树中的最大值。
  3. 节点右子树中的最大值。

同样,在二叉树中找到最小值并计算范围系数。
下面是上述方法的实现:

C++
// CPP program to find Coefficient of
// Range in a Binary Tree
  
#include 
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


输出:
Coefficient of Range is 0.833333

时间复杂度: O(n),其中 n 是节点数。  
辅助空间:O(n)