Print BST keys in the given range
Last Updated :
23 Aug, 2023
Given two values k1 and k2 where k1 < k2 and a root pointer to a Binary Search Tree. The task is to print all the keys of the tree in the range k1 to k2 in increasing order.
Examples:
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: 8
Explanation: The key 8 is in the range 1 to 10
Approach: Below is the idea to solve the problem:
Traverse the tree in the inorder traversal. If the Binary search tree is traversed in inorder traversal the keys are traversed in increasing order. So while traversing the keys in the inorder traversal. If the key lies in the range print the key else skip the key.
Follow the below steps to Implement the idea:
- Run DFS on BST in in-order traversal starting from root keeping k1 and k2 as parameters.
- If the value of the root’s key is greater than k1, then recursively call in the left subtree i.e. if k1 < root->data call for root->left.
- If the value of the root’s key is in range, then print the root’s key i.e. if k1 <= root->data and k2 >= root->data print root->data.
- Recursively call for the right subtree i.e. root->right.
Below is the Implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node *left;
node *right;
};
void Print(node *root, int k1, int k2)
{
if ( NULL == root )
return ;
Print(root->left, k1, k2);
if ( k1 <= root->data && k2 >= root->data )
cout<<root->data<< " " ;
Print(root->right, k1, k2);
}
node* newNode( int data)
{
node *temp = new node();
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
node *root = new node();
int k1 = 10, k2 = 25;
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;
}
|
C
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
void Print( struct node *root, int k1, int k2)
{
if ( NULL == root )
return ;
if ( k1 < root->data )
Print(root->left, k1, k2);
if ( k1 <= root->data && k2 >= root->data )
printf ( "%d " , root->data );
Print(root->right, k1, k2);
}
struct node* newNode( int data)
{
struct node *temp = malloc ( sizeof ( struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
struct node *root = malloc ( sizeof ( struct node));
int k1 = 10, k2 = 25;
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;
}
|
Java
class Node {
int data;
Node left, right;
Node( int d) {
data = d;
left = right = null ;
}
}
class BinaryTree {
static Node root;
void Print(Node node, int k1, int k2) {
if (node == null ) {
return ;
}
if (k1 < node.data) {
Print(node.left, k1, k2);
}
if (k1 <= node.data && k2 >= node.data) {
System.out.print(node.data + " " );
}
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);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def Print (root, k1, k2):
if root is None :
return
if k1 < root.data :
Print (root.left, k1, k2)
if k1 < = root.data and k2 > = root.data:
print (root.data,end = ' ' )
Print (root.right, k1, k2)
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)
|
C#
using System;
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;
public virtual void Print(Node node, int k1, int k2)
{
if (node == null )
{
return ;
}
if (k1 < node.data)
{
Print(node.left, k1, k2);
}
if (k1 <= node.data && k2 >= node.data)
{
Console.Write(node.data + " " );
}
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);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
var root = null ;
function Print(node , k1 , k2) {
if (node == null ) {
return ;
}
if (k1 < node.data) {
Print(node.left, k1, k2);
}
if (k1 <= node.data && k2 >= node.data) {
document.write(node.data + " " );
}
Print(node.right, k1, k2);
}
var k1 = 10, k2 = 25;
root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.left.right = new Node(12);
Print(root, k1, k2);
</script>
|
Time Complexity: O(N), where N is the total number of keys in the tree, A single traversal of the tree is needed.
Auxiliary Space: O(H). where H is the height of the tree in recursion call stack
Approach 2: Using Morris Inorder Traversal
In this approach, we use Morris Inorder Traversal to traverse the tree without using a stack or recursion. In Morris Inorder Traversal, we make use of threaded binary trees to find the predecessor of the current node. We start with the root node and traverse to the leftmost node of the subtree rooted at the current node. For each node in the subtree, we check if it lies within the given range and print its key. If the node has a right child, we find its inorder successor using the threaded binary tree and update the pointers to create a threaded binary tree.
- If the given root node is NULL, return.
- Initialize a current pointer to point to the root node.
- While the current pointer is not NULL, do the following:
- If the left child of the current node is NULL:
- If the key of the current node is within the given range, print the key.
- Move to the right child of the current node.
- If the left child of the current node is not NULL:
- Find the rightmost node in the left subtree of the current node.
- If the right child of the rightmost node is NULL, set it to point to the current node and move to the left child of the current node.
- If the right child of the rightmost node is not NULL, set it to NULL, check if the key of the current node is within the given range, and move to the right child of the current node.
Here’s the C++ code for this approach:
C++
#include<iostream>
using namespace std;
struct Node
{
int key;
Node *left, *right;
};
Node* newNode( int item)
{
Node* temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void printRange(Node* root, int lower, int upper)
{
if (root == NULL)
return ;
Node* curr = root;
while (curr != NULL)
{
if (curr->left == NULL)
{
if (curr->key >= lower && curr->key <= upper)
cout << curr->key << " " ;
curr = curr->right;
}
else
{
Node* pre = curr->left;
while (pre->right != NULL && pre->right != curr)
pre = pre->right;
if (pre->right == NULL)
{
pre->right = curr;
curr = curr->left;
}
else
{
pre->right = NULL;
if (curr->key >= lower && curr->key <= upper)
cout << curr->key << " " ;
curr = curr->right;
}
}
}
}
int main()
{
Node* root = newNode(20);
root->left = newNode(8);
root->right = newNode(22);
root->left->left = newNode(4);
root->right->left = newNode(12);
int lower = 10, upper = 25;
printRange(root, lower, upper);
return 0;
}
|
Java
class Node {
int key;
Node left, right;
Node( int item) {
key = item;
left = right = null ;
}
}
public class Main {
static void printRange(Node root, int lower, int upper) {
if (root == null ) {
return ;
}
Node curr = root;
while (curr != null ) {
if (curr.left == null ) {
if (curr.key >= lower && curr.key <= upper) {
System.out.print(curr.key + " " );
}
curr = curr.right;
} else {
Node pre = curr.left;
while (pre.right != null && pre.right != curr) {
pre = pre.right;
}
if (pre.right == null ) {
pre.right = curr;
curr = curr.left;
} else {
pre.right = null ;
if (curr.key >= lower && curr.key <= upper) {
System.out.print(curr.key + " " );
}
curr = curr.right;
}
}
}
}
public static void main(String[] args) {
Node root = new Node( 20 );
root.left = new Node( 8 );
root.right = new Node( 22 );
root.left.left = new Node( 4 );
root.right.left = new Node( 12 );
int lower = 10 , upper = 25 ;
printRange(root, lower, upper);
}
}
|
Python3
class Node:
def __init__( self , item):
self .key = item
self .left = None
self .right = None
def newNode(item):
temp = Node(item)
return temp
def printRange(root, lower, upper):
if root is None :
return
curr = root
while curr is not None :
if curr.left is None :
if curr.key > = lower and curr.key < = upper:
print (curr.key, end = ' ' )
curr = curr.right
else :
pre = curr.left
while pre.right is not None and pre.right ! = curr:
pre = pre.right
if pre.right is None :
pre.right = curr
curr = curr.left
else :
pre.right = None
if curr.key > = lower and curr.key < = upper:
print (curr.key, end = ' ' )
curr = curr.right
root = newNode( 20 )
root.left = newNode( 8 )
root.right = newNode( 22 )
root.left.left = newNode( 4 )
root.right.left = newNode( 12 )
lower = 10
upper = 25
printRange(root, lower, upper)
|
C#
using System;
public class Node
{
public int key;
public Node left, right;
public Node( int item)
{
key = item;
left = right = null ;
}
}
public class Program
{
static void PrintRange(Node root, int lower, int upper)
{
if (root == null )
return ;
Node curr = root;
while (curr != null )
{
if (curr.left == null )
{
if (curr.key >= lower && curr.key <= upper)
Console.Write(curr.key + " " );
curr = curr.right;
}
else
{
Node pre = curr.left;
while (pre.right != null && pre.right != curr)
pre = pre.right;
if (pre.right == null )
{
pre.right = curr;
curr = curr.left;
}
else
{
pre.right = null ;
if (curr.key >= lower && curr.key <= upper)
Console.Write(curr.key + " " );
curr = curr.right;
}
}
}
}
public static void Main( string [] args)
{
Node root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.right.left = new Node(12);
int lower = 10, upper = 25;
PrintRange(root, lower, upper);
}
}
|
Javascript
class Node {
constructor(key) {
this .key = key;
this .left = null ;
this .right = null ;
}
}
function printRange(root, lower, upper) {
if (root === null ) {
return ;
}
let curr = root;
while (curr !== null ) {
if (curr.left === null ) {
if (curr.key >= lower && curr.key <= upper) {
process.stdout.write(curr.key + " " );
}
curr = curr.right;
} else {
let pre = curr.left;
while (pre.right !== null && pre.right !== curr) {
pre = pre.right;
}
if (pre.right === null ) {
pre.right = curr;
curr = curr.left;
} else {
pre.right = null ;
if (curr.key >= lower && curr.key <= upper) {
process.stdout.write(curr.key + " " );
}
curr = curr.right;
}
}
}
}
function main() {
let root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(4);
root.right.left = new Node(12);
let lower = 10, upper = 25;
printRange(root, lower, upper);
}
main();
|
Time Complexity: O(N), where N is the total number of keys in the tree, A single traversal of the tree is needed.
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...