Given two values n1 and n2 (where n1 < n2) and a root pointer to a Binary Search Tree. Print all the keys of tree in range n1 to n2. i.e. print all nodes n such that n1<=n<=n2 and n is a key of given BST. Print all the keys in increasing order.
Prerequisites: Morris traversal | Threaded binary trees
Inorder traversal uses recursion or stack/queue which consumes O(n) space. But there is one efficient way to do inorder tree traversal using Morris Traversal which is based in Threaded Binary trees. Morris traversal uses no recursion or stack/queue and simply stores some important information in the wasted NULL pointers. Morris traversal consumes constant extra memory O(1) as it uses no recursion or stack/queue. Hence we will use Morris traversal to do inorder traversal in the algorithm presented in this tutorial to print keys of a BST in a given range, which is efficient memory-wise.
The concept of Threaded Binary trees is simple in that they store some useful information in the wasted NULL pointers. In a normal binary tree with n nodes, n+1 NULL pointers waste memory.
Approach: Morris Traversal is a very nice memory-efficient technique to do tree traversal without using stack or recursion in constant memory O(1) based on Threaded Binary Trees. Morris traversal can be used in solving problems where inorder tree traversals are used especially in order statistics eg-Kth largest element in BST, Kth smallest in BST etc. Hence, this is where Morris traversal would come handy as a more efficient method to do inorder traversal in constant O(1) space without using any stack or recursion.
Algorithm
1) Initialize Current as root.
2) While current is not NULL :
2.1) If current has no left child
a) Check if current lies between n1 and n2.
1)If so, then visit the current node.
b)Otherwise, Move to the right child of current.
3) Else, here we have 2 cases:
a) Find the inorder predecessor of current node.
Inorder predecessor is the right most node
in the left subtree or left child itself.
b) If the right child of the inorder predecessor is NULL:
1) Set current as the right child of its inorder predecessor.
2) Move current node to its left child.
c) Else, if the threaded link between the current node
and it's inorder predecessor already exists :
1) Set right pointer of the inorder predecessor as NULL.
2) Again check if current node lies between n1 and n2.
a)If so, then visit the current node.
3)Now move current to it's right child.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node *left, *right;
};
void RangeTraversal(node* root,
int n1, int n2)
{
if (!root)
return ;
node* curr = root;
while (curr) {
if (curr->left == NULL)
{
if (curr->data <= n2 &&
curr->data >= n1)
{
cout << curr->data << " " ;
}
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->data <= n2 &&
curr->data >= n1)
{
cout << curr->data << " " ;
}
curr = curr->right;
}
}
}
}
node* newNode( int data)
{
node* temp = new node;
temp->data = data;
temp->right = temp->left = NULL;
return temp;
}
int main()
{
node* root = newNode(4);
root->left = newNode(2);
root->right = newNode(7);
root->left->left = newNode(1);
root->left->right = newNode(3);
root->right->left = newNode(6);
root->right->right = newNode(10);
RangeTraversal(root, 4, 12);
return 0;
}
|
Java
class GfG {
static class node {
int data;
node left, right;
}
static void RangeTraversal(node root, int n1, int n2)
{
if (root == null )
return ;
node curr = root;
while (curr != null ) {
if (curr.left == null )
{
if (curr.data <= n2 && curr.data >= n1)
{
System.out.print(curr.data + " " );
}
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.data <= n2 && curr.data >= n1)
{
System.out.print(curr.data + " " );
}
curr = curr.right;
}
}
}
}
static node newNode( int data)
{
node temp = new node();
temp.data = data;
temp.right = null ;
temp.left = null ;
return temp;
}
public static void main(String[] args)
{
node root = newNode( 4 );
root.left = newNode( 2 );
root.right = newNode( 7 );
root.left.left = newNode( 1 );
root.left.right = newNode( 3 );
root.right.left = newNode( 6 );
root.right.right = newNode( 10 );
RangeTraversal(root, 4 , 12 );
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def RangeTraversal(root, n1, n2):
if root = = None :
return
curr = root
while curr:
if curr.left = = None :
if curr.data < = n2 and curr.data > = n1:
print (curr.data, end = " " )
curr = curr.right
else :
pre = curr.left
while (pre.right ! = None and
pre.right ! = curr):
pre = pre.right
if pre.right = = None :
pre.right = curr;
curr = curr.left
else :
pre.right = None
if curr.data < = n2 and curr.data > = n1:
print (curr.data, end = " " )
curr = curr.right
if __name__ = = '__main__' :
root = newNode( 4 )
root.left = newNode( 2 )
root.right = newNode( 7 )
root.left.left = newNode( 1 )
root.left.right = newNode( 3 )
root.right.left = newNode( 6 )
root.right.right = newNode( 10 )
RangeTraversal(root, 4 , 12 )
|
C#
using System;
public class GfG
{
public class node
{
public int data;
public node left, right;
}
static void RangeTraversal(node root, int n1, int n2)
{
if (root == null )
return ;
node curr = root;
while (curr != null )
{
if (curr.left == null )
{
if (curr.data <= n2 && curr.data >= n1)
{
Console.Write(curr.data + " " );
}
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.data <= n2 && curr.data >= n1)
{
Console.Write(curr.data + " " );
}
curr = curr.right;
}
}
}
}
static node newNode( int data)
{
node temp = new node();
temp.data = data;
temp.right = null ;
temp.left = null ;
return temp;
}
public static void Main(String[] args)
{
node root = newNode(4);
root.left = newNode(2);
root.right = newNode(7);
root.left.left = newNode(1);
root.left.right = newNode(3);
root.right.left = newNode(6);
root.right.right = newNode(10);
RangeTraversal(root, 4, 12);
}
}
|
Javascript
<script>
class node {
constructor() {
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function RangeTraversal( root , n1 , n2)
{
if (root == null )
return ;
var curr = root;
while (curr != null ) {
if (curr.left == null )
{
if (curr.data <= n2 && curr.data >= n1)
{
document.write(curr.data + " " );
}
curr = curr.right;
}
else {
var 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.data <= n2 && curr.data >= n1)
{
document.write(curr.data + " " );
}
curr = curr.right;
}
}
}
}
function newNode(data)
{
temp = new node();
temp.data = data;
temp.right = null ;
temp.left = null ;
return temp;
}
root = newNode(4);
root.left = newNode(2);
root.right = newNode(7);
root.left.left = newNode(1);
root.left.right = newNode(3);
root.right.left = newNode(6);
root.right.right = newNode(10);
RangeTraversal(root, 4, 12);
</script>
|
Complexity Analysis:
- Time Complexity : O(n)
- Auxiliary Space : O(1), since no extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2022
Like Article
Save Article