1 00:00:02,020 --> 00:00:02,770 Hi, everyone. 2 00:00:02,800 --> 00:00:08,940 So in this video, we are going to solve this question, convert sartorially to binary search tree, 3 00:00:09,520 --> 00:00:10,720 so given us today. 4 00:00:11,020 --> 00:00:16,740 So if this is my area, these are the elements one, two, three, four, five, six and seven. 5 00:00:16,990 --> 00:00:19,150 So this area is basically a certain area. 6 00:00:19,330 --> 00:00:24,520 And what we need to do so with the help of these elements, we want to construct a binary such tree 7 00:00:24,700 --> 00:00:30,060 so this can be one of the best five, six and seven. 8 00:00:30,400 --> 00:00:33,500 So this is the root node and these are all the nodes. 9 00:00:33,760 --> 00:00:35,100 So this is a bisdee. 10 00:00:36,180 --> 00:00:37,470 So we can construct this. 11 00:00:38,160 --> 00:00:45,420 But what I want to do, so I want to construct a balance to be housed, so basically for distri, so 12 00:00:45,420 --> 00:00:49,090 for these elements, one, two, three, four, five, six, seven. 13 00:00:49,110 --> 00:00:53,280 So how I will construct my body so I will find out the main elements. 14 00:00:53,310 --> 00:00:54,500 So this is the main element. 15 00:00:54,990 --> 00:00:56,940 I will make the main element as the root. 16 00:00:57,450 --> 00:00:58,730 So four will be my root. 17 00:00:59,670 --> 00:01:05,340 So all these elements will be the part of the nursery and all these elements will be the part of the 18 00:01:05,340 --> 00:01:06,060 right subtree. 19 00:01:07,820 --> 00:01:13,820 So for the left subtree, I have elements one, two and three and four, the right subtree, I have 20 00:01:13,820 --> 00:01:15,680 elements five, six and seven. 21 00:01:17,190 --> 00:01:22,530 Then what I will do, so I will call education for the next appre and I will call occasion for the right 22 00:01:22,530 --> 00:01:28,140 sempre so far left Sabry again I will find out Dimmit so I will construct the node. 23 00:01:28,740 --> 00:01:31,470 So this will be delivered subtree and this will be the right Sabry. 24 00:01:31,830 --> 00:01:34,920 So when will when will be on the left and he will be on the right. 25 00:01:35,160 --> 00:01:36,510 Similarly I will. 26 00:01:37,170 --> 00:01:42,020 So this is the main elements of this will be my route six is the route for left. 27 00:01:42,030 --> 00:01:42,740 I have five. 28 00:01:42,750 --> 00:01:45,120 So this is the left and this is the right subtree. 29 00:01:45,360 --> 00:01:46,810 So seven will be in the right. 30 00:01:47,400 --> 00:01:50,460 So finally, virtually my balance to Boudi. 31 00:01:51,410 --> 00:01:58,280 So this will be for then I have two, one, three, six, five and seven. 32 00:01:59,470 --> 00:02:02,960 So this is a beast and this is also a beast. 33 00:02:02,980 --> 00:02:05,340 But I want to construct a balanced beast. 34 00:02:05,590 --> 00:02:06,970 So this is the right answer. 35 00:02:07,870 --> 00:02:09,009 This is the right answer. 36 00:02:09,190 --> 00:02:10,620 So how we can solve this question. 37 00:02:10,630 --> 00:02:11,560 So it's very simple. 38 00:02:12,160 --> 00:02:13,330 We will create a function. 39 00:02:14,410 --> 00:02:17,760 Now, that function which will take it, will take that as input. 40 00:02:18,010 --> 00:02:19,480 It will take this starting. 41 00:02:19,660 --> 00:02:20,950 It will get started next. 42 00:02:20,950 --> 00:02:24,040 It will take the index and it will return the node. 43 00:02:24,490 --> 00:02:25,780 It will return the route in order. 44 00:02:25,810 --> 00:02:28,720 So North Star will be needed and type what we will do. 45 00:02:28,930 --> 00:02:32,680 So we will find out the more relevant it will find out, the relevant. 46 00:02:32,920 --> 00:02:34,330 I will construct my route. 47 00:02:35,140 --> 00:02:37,290 Then I will call into question for the left subtree. 48 00:02:37,300 --> 00:02:40,580 I will call the collision for the right some pretty simple. 49 00:02:41,020 --> 00:02:43,720 So this question is basically present only called. 50 00:02:44,660 --> 00:02:45,740 So this is the question. 51 00:02:48,630 --> 00:02:56,460 So this is the question I need to convert, I saw today to a binary search tree and it sees your tree 52 00:02:56,460 --> 00:02:57,660 should be height balanced. 53 00:02:59,000 --> 00:02:59,510 So. 54 00:03:00,430 --> 00:03:06,940 We are taking it as input and we need to return the route, so as discussed here, if it will see. 55 00:03:08,630 --> 00:03:14,260 So we need to construct a function like this, a function that will take every state index and index, 56 00:03:14,690 --> 00:03:17,750 so let's write the code so we will create a helper function. 57 00:03:19,680 --> 00:03:29,010 So let's create a helper function to train all star helper, so what it will take, it will take as 58 00:03:29,010 --> 00:03:29,400 input. 59 00:03:34,850 --> 00:03:41,800 So let's say the name of the area is the name of the vector is Eddie, oh, let's name it, only legitimate 60 00:03:41,820 --> 00:03:46,070 error, it will take what is the starting index of the array? 61 00:03:46,070 --> 00:03:48,530 And it will take a what is the end index of the Eddie? 62 00:03:51,740 --> 00:03:59,870 So how I will call this function so intense, how many number of elements are present num start size? 63 00:04:01,970 --> 00:04:03,650 Then I will call this function helper. 64 00:04:06,870 --> 00:04:11,420 So return call the helper function, give it a. 65 00:04:12,030 --> 00:04:17,970 So I am passing everybody day, starting next, starting next zero and indexers and minus one. 66 00:04:19,660 --> 00:04:25,240 So now we need to write the code for this function helper, so first of all, we need to discuss what 67 00:04:25,240 --> 00:04:26,190 should be the best case. 68 00:04:26,410 --> 00:04:28,660 So these cases, if there is somebody. 69 00:04:29,630 --> 00:04:30,640 So for MPRDA. 70 00:04:32,200 --> 00:04:34,930 For them to start will be good and end. 71 00:04:36,440 --> 00:04:44,390 So in this case, for them, my body should be null because if it is empty, then there will be no elements 72 00:04:44,660 --> 00:04:48,080 by necessity, so you will return otherwise what we need to do. 73 00:04:48,650 --> 00:04:51,170 So as discussed the approach, what we will do. 74 00:04:51,290 --> 00:04:53,060 So, first of all, we need to find out. 75 00:04:53,330 --> 00:04:57,190 So if these are the elements, first of all, I need to find out the main element. 76 00:04:58,040 --> 00:04:59,660 So I have this starting next. 77 00:04:59,660 --> 00:05:01,130 I have the index. 78 00:05:01,130 --> 00:05:03,830 I will start by two to find out. 79 00:05:04,390 --> 00:05:07,370 So what is the start plus and why do so? 80 00:05:07,370 --> 00:05:10,280 After finding out, I will construct the node. 81 00:05:10,520 --> 00:05:12,700 Then I will call for the left subtree. 82 00:05:12,860 --> 00:05:16,160 I will call Dacogen on the right subtree so far left. 83 00:05:16,160 --> 00:05:23,720 Sabry, what will be your next year and next will be of minus one and for calling on the right subtree. 84 00:05:23,960 --> 00:05:25,320 What is your starting next. 85 00:05:25,520 --> 00:05:27,350 So starting next will be my plus one. 86 00:05:28,460 --> 00:05:28,960 Simple. 87 00:05:29,090 --> 00:05:33,800 So this will really start the next four calling on the right three and this will be the end index for 88 00:05:33,800 --> 00:05:34,760 calling on the left. 89 00:05:34,760 --> 00:05:37,180 Sabry So let's do that. 90 00:05:38,060 --> 00:05:42,290 So first of all I need to find out the main element, main index. 91 00:05:42,650 --> 00:05:46,700 So many nexxus start plus and vidoe. 92 00:05:47,720 --> 00:05:49,310 And what is my data. 93 00:05:51,720 --> 00:05:52,920 So my roommate is. 94 00:05:54,370 --> 00:05:54,970 Area of. 95 00:05:56,260 --> 00:06:01,180 Now, after finding out the data, let's construct the root node, so the North Star route. 96 00:06:02,610 --> 00:06:04,680 Is Nutri A.. 97 00:06:05,830 --> 00:06:08,770 And I will give the data so I yield data. 98 00:06:12,860 --> 00:06:17,240 And now what we need to do so we need to call the helper function for the left subtree. 99 00:06:18,220 --> 00:06:25,030 So called the herbal function for the left subtree, I will give it the starting index is start and 100 00:06:25,300 --> 00:06:27,910 the end index is basically made of minus one. 101 00:06:30,810 --> 00:06:35,640 Similarly, we will call the helper function for the question on the right subtree for building the 102 00:06:35,640 --> 00:06:36,280 right subtree. 103 00:06:36,510 --> 00:06:41,730 So START index is made plus one and index is same as end. 104 00:06:42,870 --> 00:06:49,830 So let's give space here, so starting next, start and end indexers minus one, and then what we need 105 00:06:49,830 --> 00:06:55,820 to do so after constructing, we will return NEEDWOOD But there is one problem with this code. 106 00:06:56,010 --> 00:07:01,080 So what we are doing at this line, I'm constructing my note, so I am constructing my note. 107 00:07:01,350 --> 00:07:02,870 Then I'm calling the helper function. 108 00:07:03,150 --> 00:07:07,530 So I am passing the starting next and made my and so I am passing the left separate. 109 00:07:08,270 --> 00:07:09,030 So what will happen. 110 00:07:09,030 --> 00:07:14,340 Left subtree will be constructed and we will return the so that the function is rude. 111 00:07:14,580 --> 00:07:16,380 Then we are calling for the right subtree. 112 00:07:16,390 --> 00:07:22,680 We are passing start Izmit plus one and we are calling recursion collision that will be constructed. 113 00:07:22,800 --> 00:07:25,620 Right besty will be constructed and we are returning the rule. 114 00:07:25,980 --> 00:07:28,590 So what we need to do, we need to make this connection. 115 00:07:30,140 --> 00:07:35,960 We need to make this connection, so this is returning root node, so let's make these connections. 116 00:07:37,720 --> 00:07:43,690 So making the connection, we will right the first time building the So Rudra left. 117 00:07:44,760 --> 00:07:48,660 Call the helper function, then I'm constructing the right subtree. 118 00:07:49,990 --> 00:07:58,420 So now I think this is correct, so rude or left, call the helper function at a start, minus one rude 119 00:07:58,540 --> 00:07:58,960 right. 120 00:07:59,290 --> 00:08:00,850 Call the helper function at a. 121 00:08:01,930 --> 00:08:05,120 And so I think our call will work. 122 00:08:05,140 --> 00:08:07,510 So first we will run our code and then we will submit. 123 00:08:07,990 --> 00:08:09,220 So let's run our code. 124 00:08:12,780 --> 00:08:13,620 Now, let's admit. 125 00:08:17,220 --> 00:08:22,230 OK, so basically our goal is working, so this is from this video, I will see you in the next one.