📌  相关文章
📜  二叉搜索树 (BST) 中给定范围内所有节点的中值

📅  最后修改于: 2021-09-04 12:57:49             🧑  作者: Mango

给定一个由N 个节点和两个节点AB组成的二叉搜索树 (BST),任务是找到给定 BST 中所有节点的中值,这些节点的值位于[A, B]范围内。

例子:

方法:可以通过对给定树执行任意树遍历并存储位于[A, B]范围内的所有节点来解决给定问题,并找到所有存储元素的中值。请按照以下步骤解决问题:

  • 初始化一个向量,比如V ,它存储位于[A, B]范围内的树的所有值。
  • 执行给定树的中序遍历,如果任何节点的值位于[A, B]范围内则将该值插入向量V 中
  • 完成上述步骤后,将存储在向量V中的所有元素的中值作为结果打印出来。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Tree Node structure
struct Node {
    struct Node *left, *right;
    int key;
};
 
// Function to create a new BST node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Function to insert a new node with
// given key in BST
Node* insertNode(Node* node, int key)
{
    // If the tree is empty,
    // return a new node
    if (node == NULL)
        return newNode(key);
 
    // Otherwise, recur down the tree
    if (key < node->key)
        node->left = insertNode(
            node->left, key);
 
    else if (key > node->key)
        node->right = insertNode(
            node->right, key);
 
    // Return the node pointer
    return node;
}
 
// Function to find all the nodes that
// lies over the range [node1, node2]
void getIntermediateNodes(
    Node* root, vector& interNodes,
    int node1, int node2)
{
    // If the tree is empty, return
    if (root == NULL)
        return;
 
    // Traverse for the left subtree
    getIntermediateNodes(root->left,
                         interNodes,
                         node1, node2);
 
    // If a second node is found,
    // then update the flag as false
    if (root->key <= node2
        and root->key >= node1) {
        interNodes.push_back(root->key);
    }
 
    // Traverse the right subtree
    getIntermediateNodes(root->right,
                         interNodes,
                         node1, node2);
}
 
// Function to find the median of all
// the values in the given BST that
// lies over the range [node1, node2]
float findMedian(Node* root, int node1,
                 int node2)
{
    // Stores all the nodes in
    // the range [node1, node2]
    vector interNodes;
 
    getIntermediateNodes(root, interNodes,
                         node1, node2);
 
    // Store the size of the array
    int nSize = interNodes.size();
 
    // Print the median of array
    // based on the size of array
    return (nSize % 2 == 1)
               ? (float)interNodes[nSize / 2]
               : (float)(interNodes[(nSize - 1) / 2]
                         + interNodes[nSize / 2])
                     / 2;
}
 
// Driver Code
int main()
{
    // Given BST
    struct Node* root = NULL;
    root = insertNode(root, 8);
    insertNode(root, 3);
    insertNode(root, 1);
    insertNode(root, 6);
    insertNode(root, 4);
    insertNode(root, 11);
    insertNode(root, 15);
 
    cout << findMedian(root, 3, 11);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Tree Node structure
static class Node
{
    Node left, right;
    int key;
};
 
static Vector interNodes = new Vector();
 
// Function to create a new BST node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
    temp.left = temp.right = null;
    return temp;
}
 
// Function to insert a new node with
// given key in BST
static Node insertNode(Node node, int key)
{
     
    // If the tree is empty,
    // return a new node
    if (node == null)
        return newNode(key);
 
    // Otherwise, recur down the tree
    if (key < node.key)
        node.left = insertNode(
            node.left, key);
 
    else if (key > node.key)
        node.right = insertNode(
            node.right, key);
 
    // Return the node pointer
    return node;
}
 
// Function to find all the nodes that
// lies over the range [node1, node2]
static void getIntermediateNodes(Node root,
                                 int node1,
                                 int node2)
{
     
    // If the tree is empty, return
    if (root == null)
        return;
 
    // Traverse for the left subtree
    getIntermediateNodes(root.left,
                         node1, node2);
 
    // If a second node is found,
    // then update the flag as false
    if (root.key <= node2 &&
        root.key >= node1)
    {
        interNodes.add(root.key);
    }
 
    // Traverse the right subtree
    getIntermediateNodes(root.right,
                         node1, node2);
}
 
// Function to find the median of all
// the values in the given BST that
// lies over the range [node1, node2]
static float findMedian(Node root, int node1,
                                   int node2)
{
     
    // Stores all the nodes in
    // the range [node1, node2]
    getIntermediateNodes(root,
                         node1, node2);
 
    // Store the size of the array
    int nSize = interNodes.size();
 
    // Print the median of array
    // based on the size of array
    return (nSize % 2 == 1) ?
           (float)interNodes.get(nSize / 2) :
           (float)(interNodes.get((nSize - 1) / 2) +
                    interNodes.get(nSize / 2)) / 2;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given BST
    Node root = null;
    root = insertNode(root, 8);
    insertNode(root, 3);
    insertNode(root, 1);
    insertNode(root, 6);
    insertNode(root, 4);
    insertNode(root, 11);
    insertNode(root, 15);
 
    System.out.print(findMedian(root, 3, 11));
}
}
 
// This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
// Tree Node structure
class Node
{
    public Node left, right;
    public int key;
};
 
static List interNodes = new List();
 
// Function to create a new BST node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
    temp.left = temp.right = null;
    return temp;
}
 
// Function to insert a new node with
// given key in BST
static Node insertNode(Node node, int key)
{
     
    // If the tree is empty,
    // return a new node
    if (node == null)
        return newNode(key);
 
    // Otherwise, recur down the tree
    if (key < node.key)
        node.left = insertNode(
            node.left, key);
 
    else if (key > node.key)
        node.right = insertNode(
            node.right, key);
 
    // Return the node pointer
    return node;
}
 
// Function to find all the nodes that
// lies over the range [node1, node2]
static void getIntermediateNodes(Node root,
                                 int node1,
                                 int node2)
{
     
    // If the tree is empty, return
    if (root == null)
        return;
 
    // Traverse for the left subtree
    getIntermediateNodes(root.left,
                         node1, node2);
 
    // If a second node is found,
    // then update the flag as false
    if (root.key <= node2 &&
        root.key >= node1)
    {
        interNodes.Add(root.key);
    }
 
    // Traverse the right subtree
    getIntermediateNodes(root.right,
                         node1, node2);
}
 
// Function to find the median of all
// the values in the given BST that
// lies over the range [node1, node2]
static float findMedian(Node root, int node1,
                                   int node2)
{
     
    // Stores all the nodes in
    // the range [node1, node2]
    getIntermediateNodes(root,
                         node1, node2);
 
    // Store the size of the array
    int nSize = interNodes.Count;
 
    // Print the median of array
    // based on the size of array
    return (nSize % 2 == 1) ?
           (float)interNodes[nSize / 2] :
           (float)(interNodes[(nSize - 1) / 2] +
                    interNodes[nSize / 2]) / 2;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given BST
    Node root = null;
    root = insertNode(root, 8);
    insertNode(root, 3);
    insertNode(root, 1);
    insertNode(root, 6);
    insertNode(root, 4);
    insertNode(root, 11);
    insertNode(root, 15);
 
    Console.Write(findMedian(root, 3, 11));
}
}
 
// This code is contributed by shikhasingrajput


输出:
6

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live