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.
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 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.
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.
4 6 7 10
Time Complexity : O(n)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print BST keys in the given range
- Remove BST keys outside the given range
- Remove BST Keys in a given Range
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- BST to a Tree with sum of all smaller keys
- Find k-th smallest element in BST (Order Statistics in BST)
- Two nodes of a BST are swapped, correct the BST
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- K'th smallest element in BST using O(1) Extra Space
- Find median of BST in O(n) time and O(1) space
- K'th Largest element in BST using constant extra space
- Sum of K largest elements in BST using O(1) Extra space
- Count BST nodes that lie in a given range
- Count BST subtrees that lie in given range
- Print K inorder successors of a Binary Tree in O(1) space
- Find the largest BST subtree in a given Binary Tree | Set 1
- Sorted order printing of a given array that represents a BST
- Construct BST from given preorder traversal | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.