📜  二叉搜索树的最小和最大元素的和与乘积

📅  最后修改于: 2021-05-24 23:53:44             🧑  作者: Mango

给定二叉搜索树。任务是找到树的最大值和最小值的和与乘积。


对于上述树,树的最大值和最小值的总和和乘积分别为26和88。

方法:

  1. 对于具有最小值的节点:查找最左边的叶节点
  2. 对于具有最大值的节点:查找最右边的叶节点

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
/* A binary tree node has data, pointer to left child  
   and a pointer to right child */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
  
/* Helper function that allocates a new node  
with the given data and NULL left and right  
pointers. */
struct node* newNode(int data)
{
    struct node* node = (struct node*)
        malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
  
    return (node);
}
  
// Function to insert a node in BST
struct node* insert(struct node* node, int data)
{
    /* 1. If the tree is empty, return a new,      
      single node */
    if (node == NULL)
        return (newNode(data));
    else {
        /* 2. Otherwise, recur down the tree */
        if (data <= node->data)
            node->left = insert(node->left, data);
        else
            node->right = insert(node->right, data);
  
        /* return the (unchanged) node pointer */
        return node;
    }
}
  
// Function to find the node with maximum value
int maxValue(struct node* node)
{
    struct node* current = node;
  
    // Find the rightmost leaf
    while (current->right != NULL) {
        current = current->right;
    }
    return (current->data);
}
  
// Function to find the node with minimum value
int minValue(struct node* node)
{
    struct node* current = node;
  
    // Find the leftmost leaf
    while (current->left != NULL) {
        current = current->left;
    }
    return (current->data);
}
  
// Driver code
int main()
{
    struct node* root = NULL;
    root = insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
      
   int maxNodeValue = maxValue(root);
   int minNodeValue = minValue(root);
  
    cout << "Sum of Maximum value and Minimum value in BST is "
         << maxNodeValue + minNodeValue << endl;
  
    cout << "Product of Maximum value and Minimum value in BST is "
         << maxNodeValue * minNodeValue;
  
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
  
/* A binary tree node has data, 
pointer to left child and 
a pointer to right child */
static class node 
{
    int data;
    node left;
    node right;
};
  
/* Helper function that allocates a new node 
with the given data and null left and right 
pointers. */
static node newNode(int data)
{
    node node = new node();
    node.data = data;
    node.left = null;
    node.right = null;
  
    return (node);
}
  
// Function to insert a node in BST
static node insert( node node, int data)
{
    /* 1. If the tree is empty,      
    return a new, single node */
    if (node == null)
        return (newNode(data));
    else 
    {
        /* 2. Otherwise, recur down the tree */
        if (data <= node.data)
            node.left = insert(node.left, data);
        else
            node.right = insert(node.right, data);
  
        /* return the (unchanged) node pointer */
        return node;
    }
}
  
// Function to find the node with maximum value
static int maxValue(node node)
{
    node current = node;
  
    // Find the rightmost leaf
    while (current.right != null) 
    {
        current = current.right;
    }
    return (current.data);
}
  
// Function to find the node with minimum value
static int minValue(node node)
{
    node current = node;
  
    // Find the leftmost leaf
    while (current.left != null) 
    {
        current = current.left;
    }
    return (current.data);
}
  
// Driver code
public static void main(String args[])
{
    node root = null;
    root = insert(root, 4);
    root = insert(root, 2);
    root = insert(root, 1);
    root = insert(root, 3);
    root = insert(root, 6);
    root = insert(root, 5);
      
    int maxNodeValue = maxValue(root);
    int minNodeValue = minValue(root);
  
    System.out.println( "Sum of Maximum value and" + 
                        " Minimum value in BST is " +
                        (maxNodeValue + minNodeValue));
  
    System.out.println( "Product of Maximum value and " +
                        "Minimum value in BST is " + 
                         maxNodeValue * minNodeValue);
}
}
  
// This code is contributed by Arnab Kundu


Python3
# Python program to find sum and product of 
# maximum and minimum in a Binary search Tree
  
_MIN=-2147483648
_MAX=2147483648
  
# Helper function that allocates a new 
# node with the given data and None left 
# and right poers.                                 
class newNode: 
  
    # Constructor to create a new node 
    def __init__(self,data): 
        self.data = data 
        self.left = None
        self.right = None
  
# Function to insert a node in BST
def insert(node, data):
  
    # 1. If the tree is empty, return a new,     
    # single node
    if (node == None):
        return (newNode(data))
    else:
          
        # 2. Otherwise, recur down the tree 
        if (data <= node.data):
            node.left = insert(node.left, data)
        else:
            node.right = insert(node.right, data)
  
        # return the (unchanged) node poer 
        return node
      
  
  
# Function to find the node with maximum value
def maxValue(node):
    current = node 
      
    # Find the rightmost leaf
    while (current.right != None) :
        current = current.right
      
    return (current.data)
  
  
# Function to find the node with minimum value
def minValue(node):
  
    current = node
  
    # Find the leftmost leaf
    while (current.left != None):
        current = current.left
      
    return (current.data)
  
  
          
# Driver Code 
if __name__ == '__main__':
      
    # Create binary Tree
    root = newNode(2) 
    insert(root, 1)
    insert(root, 3)
    insert(root, 6)
    insert(root, 5)
    max = maxValue(root)
    min = minValue(root)
      
    print("Sum of Maximum and Minimum" +
            "element is ", max + min)
    print("Product of Maximum and Minimum" +
            "element is", max * min)
      
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# implementation of the above approach
using System;
      
class GFG
{
  
/* A binary tree node has data, 
pointer to left child and 
a pointer to right child */
public class node 
{
    public int data;
    public node left;
    public node right;
};
  
/* Helper function that allocates a new node 
with the given data and null left and right 
pointers. */
static node newNode(int data)
{
    node node = new node();
    node.data = data;
    node.left = null;
    node.right = null;
  
    return (node);
}
  
// Function to insert a node in BST
static node insert( node node, int data)
{
    /* 1. If the tree is empty,     
    return a new, single node */
    if (node == null)
        return (newNode(data));
    else
    {
        /* 2. Otherwise, recur down the tree */
        if (data <= node.data)
            node.left = insert(node.left, data);
        else
            node.right = insert(node.right, data);
  
        /* return the (unchanged) node pointer */
        return node;
    }
}
  
// Function to find the node with maximum value
static int maxValue(node node)
{
    node current = node;
  
    // Find the rightmost leaf
    while (current.right != null) 
    {
        current = current.right;
    }
    return (current.data);
}
  
// Function to find the node with minimum value
static int minValue(node node)
{
    node current = node;
  
    // Find the leftmost leaf
    while (current.left != null) 
    {
        current = current.left;
    }
    return (current.data);
}
  
// Driver code
public static void Main(String []args)
{
    node root = null;
    root = insert(root, 4);
    root = insert(root, 2);
    root = insert(root, 1);
    root = insert(root, 3);
    root = insert(root, 6);
    root = insert(root, 5);
      
    int maxNodeValue = maxValue(root);
    int minNodeValue = minValue(root);
  
    Console.WriteLine( "Sum of Maximum value and" + 
                        " Minimum value in BST is " +
                        (maxNodeValue + minNodeValue));
  
    Console.WriteLine( "Product of Maximum value and " +
                        "Minimum value in BST is " + 
                        maxNodeValue * minNodeValue);
}
}
  
/* This code is contributed by PrinciRaj1992 */


输出:
Sum of Maximum value and Minimum value in BST is 7
Product of Maximum value and Minimum value in BST is 6

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。