00:00Next let's do a question. Question's name is print in range. What has been given? A range of numbers.
00:06Here the first number is K1 and the second number is K2.
00:10From 5 to 12, all the numbers exist in our BST, we are going to print it.
00:17Look at this, including 5 and 12, what exists?
00:21First we have 5, then we have 6, then we have 8, then we have 10 and then we have
00:3111.
00:32What is the strategy of print in each step?
00:35In each step, we will compare what we have in the range.
00:40If the value of 8 is in the root of K1 and K2,
00:53we are going to print it under the root of K1 and K2.
00:58If the root is bigger than K1 or equal to K2 is bigger than K2,
01:05what is the case 1?
01:07This will be our case 1.
01:09In this case, we are going into the left subtree
01:11and we will also print it under the right subtree.
01:14The other one is our case 2.
01:17The case 2 is the value that the root is greater than K2.
01:23This means, the largest,
01:24our root is bigger than K2,
01:26so all the values exist only in the right subtree.
01:30What do we have to do in this case?
01:31We have to call for the right subtree.
01:33In this case, we have a case 3.
01:35The case 3 is the value that the root is less than K1.
01:40The minimum is the value that the root is less than K2.
01:42The number will exist in the left subtree.
01:47So, here we will only come from the left subtree.
01:51Let's see this one.
01:51How will it go?
01:53In the beginning, we will invoke case 1.
01:57Case 1 means,
01:588, 5 and 12 are both in the middle of K2.
02:01We will also call for the right subtree.
02:03So, first of all, we will call for the left subtree.
02:07Then we will print the root itself and then we will call for the right subtree.
02:12So, we have to call for the left subtree.
02:13We have to call for 5.
02:14Who will compare 5?
02:165 will compare 5 with 12.
02:17When it was compared with 5 and 12,
02:31Then we will print the root out of 6,
02:32Then we will print the root out of 5.
02:34Then we will print the root out of 8.
02:37Then we go back to 8.
02:388 will print out of 8 now.
02:39Now we'll check in the right subtree.
02:41Despite 10, we will compare 10.
02:43If 5 will be in this range,
02:45We will print out of 10 and then we will print out from 11.
02:48Then we will print out of 11.
02:48So, let's write code as shown.
02:50First of all, a function.
02:51public, static, void, print in range.
02:56We will pass our root in print in range
02:58beyond our k1 and k2.
03:03What will be the first comparison?
03:05The first comparison will be
03:06if our root data is greater than or equal to k1
03:10and the root data is less than or equal to k2.
03:14In this case,
03:16we will print values in left subtree
03:19we will print values in this.
03:21Then we will print values in our values
03:23because obviously we have to put in range
03:25so I have to print values in my value.
03:27So, the root data
03:31Then we will print values in right subtree.
03:33Print in range, root, right.
03:37What can be the second case?
03:39The second case is that
03:40I will put things in the root
03:41only in left subtree.
03:43So, the root data is less than or equal to
03:47less than k1.
03:50In this case, print in range, root, left.
03:55And the third case is that
03:56we will put values in right subtree.
03:59So, print in range, root, right.
04:02Now, the base case is that
04:04if our root is equal to null,
04:08then we will simply return
04:10and not print from there.
04:13So, we have written code for print in range.
04:16What will we do in the main function?
04:17What will we do in the main function?
04:19We are going to print
04:22our range from 5 to 12 to 12.
04:29So, print 5, 6, 8, 10 and 11
04:32Then we will print in range of 5 to 12.
04:35Then we will print in range of 5 to 12.