原文:

给定两个值 k1 和 k2(其中 k1 < k2) and a root pointer to a binary search tree. print all the keys of the tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given bst. print all the keys in increasing order.  示例:

graph:
input: k1 = 10 and k2 = 22
output: 12, 20 and 22.
explanation: the keys are 4, 8, 12, 20, and 22.
so keys in range 10 to 22 is 12, 20 and 22.
input: k1 = 1 and k2 = 10
output: 4 and 8
explanation: the keys are 4, 8, 12, 20, and 22.
so keys in range 1 to 10 is 4 and 8.

方法:按顺序遍历树。如果二叉查找树按顺序遍历,则键按递增顺序遍历。所以在有序遍历中遍历键时。如果密钥在范围内,打印密钥,否则跳过密钥。 算法:

  1. 创建一个递归函数,以根为参数,范围为(k1,k2)
  2. 如果根键的值大于 k1,则递归调用左子树。
  3. 如果根键的值在范围内,则打印根键。
  4. 递归调用右子树。

实施:

c

// c   program to print bst in given range
#include
using namespace std;
/* a tree node structure */
class node
{
    public:
    int data;
    node *left;
    node *right;
};
/* the functions prints all the keys
which in the given range [k1..k2].
    the function assumes than k1 < k2 */
void print(node *root, int k1, int k2)
{
    /* base case */
    if ( null == root )
        return;
    /* since the desired o/p is sorted,
        recurse for left subtree first
        if root->data is greater than k1,
        then only we can get o/p keys
        in left subtree */
    if ( k1 < root->data )
        print(root->left, k1, k2);
    /* if root's data lies in range,
    then prints root's data */
    if ( k1 <= root->data && k2 >= root->data )
        cout<data<<" ";
    /* recursively call the right subtree */
   print(root->right, k1, k2);
}
/* utility function to create a new binary tree node */
node* newnode(int data)
{
    node *temp = new node();
    temp->data = data;
    temp->left = null;
    temp->right = null;
    return temp;
}
/* driver code */
int main()
{
    node *root = new node();
    int k1 = 10, k2 = 25;
    /* constructing tree given
    in the above figure */
    root = newnode(20);
    root->left = newnode(8);
    root->right = newnode(22);
    root->left->left = newnode(4);
    root->left->right = newnode(12);
    print(root, k1, k2);
    return 0;
}
// this code is contributed by rathbhupendra

c

#include
#include
/* a tree node structure */
struct node
{
  int data;
  struct node *left;
  struct node *right;
};
/* the functions prints all the keys which in
the given range [k1..k2]. the function assumes than k1 < k2 */
void print(struct node *root, int k1, int k2)
{
   /* base case */
   if ( null == root )
      return;
   /* since the desired o/p is sorted, recurse for left subtree first
      if root->data is greater than k1, then only we can get o/p keys
      in left subtree */
   if ( k1 < root->data )
     print(root->left, k1, k2);
   /* if root's data lies in range, then prints root's data */
   if ( k1 <= root->data && k2 >= root->data )
     printf("%d ", root->data );
  /* recursively call the right subtree */
   print(root->right, k1, k2);
}
/* utility function to create a new binary tree node */
struct node* newnode(int data)
{
  struct node *temp = malloc(sizeof(struct node));
  temp->data = data;
  temp->left = null;
  temp->right = null;
  return temp;
}
/* driver function to test above functions */
int main()
{
  struct node *root = malloc(sizeof(struct node));
  int k1 = 10, k2 = 25;
  /* constructing tree given in the above figure */
  root = newnode(20);
  root->left = newnode(8);
  root->right = newnode(22);
  root->left->left = newnode(4);
  root->left->right = newnode(12);
  print(root, k1, k2);
  getchar();
  return 0;
}
// this code was modified by italovinicius18

java 语言(一种计算机语言,尤用于创建网站)

// java program to print bst in given range
// a binary tree node
class node {
    int data;
    node left, right;
    node(int d) {
        data = d;
        left = right = null;
    }
}
class binarytree {
    static node root;
    /* the functions prints all the keys which in
     the given range [k1..k2]. the function assumes than k1 < k2 */
    void print(node node, int k1, int k2) {
        /* base case */
        if (node == null) {
            return;
        }
        /* since the desired o/p is sorted, recurse for left subtree first
         if root->data is greater than k1, then only we can get o/p keys
         in left subtree */
        if (k1 < node.data) {
            print(node.left, k1, k2);
        }
        /* if root's data lies in range, then prints root's data */
        if (k1 <= node.data && k2 >= node.data) {
            system.out.print(node.data   " ");
        }
        /* recursively call the right subtree  */
         print(node.right, k1, k2);
    }
    public static void main(string[] args) {
        binarytree tree = new binarytree();
        int k1 = 10, k2 = 25;
        tree.root = new node(20);
        tree.root.left = new node(8);
        tree.root.right = new node(22);
        tree.root.left.left = new node(4);
        tree.root.left.right = new node(12);
        tree.print(root, k1, k2);
    }
}
// this code has been contributed by mayank jaiswal

计算机编程语言

# python program to find bst keys in given range
# a binary tree node
class node:
    # constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = none
        self.right = none
# the function prints all the keys in the gicven range
# [k1..k2]. assumes that k1 < k2
def print(root, k1, k2):
    # base case
    if root is none:
        return
    # since the desired o/p is sorted, recurse for left
    # subtree first. if root.data is greater than k1, then
    # only we can get o/p keys in left subtree
    if k1 < root.data :
        print(root.left, k1, k2)
    # if root's data lies in range, then prints root's data
    if k1 <= root.data and k2 >= root.data:
        print root.data,
    # recursively call the right subtree
    print(root.right, k1, k2)
# driver function to test above function
k1 = 10 ; k2 = 25 ;
root = node(20)
root.left = node(8)
root.right = node(22)
root.left.left = node(4)
root.left.right = node(12)
print(root, k1, k2)
# this code is contributed by nikhil kumar singh(nickzuck_007)

c

using system;
// c# program to print bst in given range
// a binary tree node
public class node
{
    public int data;
    public node left, right;
    public node(int d)
    {
        data = d;
        left = right = null;
    }
}
public class binarytree
{
    public static node root;
    /* the functions prints all the keys which in the
     given range [k1..k2]. the function assumes than k1 < k2 */
    public virtual void print(node node, int k1, int k2)
    {
        /* base case */
        if (node == null)
        {
            return;
        }
        /* since the desired o/p is sorted, recurse for left subtree first
         if root->data is greater than k1, then only we can get o/p keys
         in left subtree */
        if (k1 < node.data)
        {
            print(node.left, k1, k2);
        }
        /* if root's data lies in range, then prints root's data */
        if (k1 <= node.data && k2 >= node.data)
        {
            console.write(node.data   " ");
        }
        /* recursively call the right subtree */
            print(node.right, k1, k2);
    }
    public static void main(string[] args)
    {
        binarytree tree = new binarytree();
        int k1 = 10, k2 = 25;
        binarytree.root = new node(20);
        binarytree.root.left = new node(8);
        binarytree.root.right = new node(22);
        binarytree.root.left.left = new node(4);
        binarytree.root.left.right = new node(12);
        tree.print(root, k1, k2);
    }
}
// this code is contributed by shrikant13

java 描述语言


输出:

12 20 22

复杂度分析:

  • 时间复杂度: o(n),其中 n 为树中键的总数。 需要对树进行一次遍历。
  • 空间复杂度: o(树的高度)。//作为递归调用
  • 不需要额外的空间。

如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。