- 1 day ago
Transcript
00:00Next question, which we will talk about is Merge to BSTs. I will give two binary search trees and merge
00:06them. I will make a single binary search tree which will be balanced and satisfy all BST properties. Here I
00:15have BST1, which is root node 2, its left child is 1 and right child is 4. Then BST2 has
00:22root node 9, its left child is 3 and right child is 12.
00:29BSTs are which can be made. One possible BST which can be made is that I make 1 root, then
00:35I take 2, then I take 3, then I take 4, then I take 9 and 12. One possible BST
00:42that I have also made. But I will try to make a balanced BST that I will form a balanced
00:48BST.
00:50This is my first case. This is a valid case. And the second case is that I will make my
01:09BST. These two BSTs are valid BSTs.
01:18tree form, which is balanced and will see something like that. If you want, we have to tweak a little
01:23bit in the approach. We can do what we can do with this approach and BST, but we are going
01:30to solve it here. I will get some balanced binary search tree in which there will be three root nodes.
01:37One is left child, nine is right child. Then two will come, one is right child, four will come, nine
01:44is left child, twelve will come as nine.
01:47So what are we going to do? I know that I have a BST1 given and a BST2 given. And
01:55I know that I can always create a sorted array from a balanced BST.
02:08So if I have a sorted array or sorted array list, I can create a balanced binary search trick which
02:17will be my final BST. So the first step is to remove BST1 in order sequence. In order sequence is
02:28a sorted sequence.
02:37in order sequence and sorted sequence. The third task is to merge in order sequence directly binary search trees to
02:44merge in order sequence and make a big sorted array. I will make sorted array 1 and sorted array 2
02:56sorted array 1 will be in order of 1 sorted array 2 will be in order of 2 both in
03:02order sequences which are different sorted arrays I have to take them and I have to merge these arrays so
03:08what will come to me? a big sorted array will come to me a final sorted array and now as
03:16I have this final sorted array then I will create these sorted arrays with balanced BST so fourth step will
03:22be sorted array to
03:28Balanced, BST. So, in 4 steps, I'm going to solve the question. The first step is to remove binary search
03:33tree 1 in order sequence. The second step is to remove BST 2 in order sequence. Both in order sequences
03:40will be sorted sequence. The third step is to merge both. We already know that if two are sorted array
03:47are sorted, how do we merge both?
03:49This is a very easy approach we have done on arrays and on array lists. The fourth step is that
03:53the final sorted array comes from the sorted array. I will make a balanced BST. I will follow this approach.
03:59First, BST1 in order sequence will have 1, 2 and 4 equal. BST2 in order sequence will have 3, 9
04:08and 12 equal. Now what I will do? I will remove this I will remove this and now I will
04:13merge them. I will take a pointer here and a pointer here. And I will make a final
04:19sorted, now I compare these pointers to the value, which one is small, then 1 is small, then 1 is
04:24small, then my pointer will update this value, now compare 2 to 3, then 2 is small, then 2 is
04:33small, then my pointer will update this value,
04:37this value, then compare 4 to 3, so 3 will come here, 3 will come here, 3 will come here,
04:443 will come here, 3 will come here, 3 will come here, 4 will come here, 4 will come here,
04:494 will come here, 4 will come here
04:54I have to traverse the first area. Now the second element will come back. The second element will come back.
05:00The second element will come back. Then the second element will come back. Then the second element will come back.
05:04In this way, I have a final sorted area. Now I have to create BST from this sorted area.
05:11I know the process of VST from sorted area. I know what I will do. First, I will remove mid
05:15element. Now, for my mid element, this will be start is equal to 0, this will be end is equal
05:22to 5. Mid is start plus end divided by 2 which is equal to 2. So, mid will become 3.
05:29We want to change mid's logic and change 4.
05:33we can create our mid, so as we have 4 mid, we will create this BST, because we have 3
05:40mid, so we will create this BST, we will create this BST, we will follow this logic, so what will
05:48we do, 3 will become root of BST, 3 will become root here,
05:523. Now what do I do? 3 left of the element of the element of the element of the element
05:56of the element of the element of the element of the element of the element of the element of the
05:58element of the element of the element of the element of the element of the element of the element of
05:58the element of the element of the element of the element of the element of the element of the element
05:59of the element of the element of the element of the element of the element of the element of the
06:01element of the element of the element of the element of the element of the element of the element of
06:01the element of the element of the element of the element of the element of the element of the element
06:01of the element of the element of the element of the element of the element of the element of the
06:01element of the element of the element of the element of the element of the element of the element of
06:01the element of the element of the element of the element of the element of the element of the element
06:06of the element of the element of the element of the element of the element of the element of the
06:13element of
06:21right child. So my final BST will be something like this. And this is a balanced BST and it is
06:28a combination of both. Now we are going to write code for simple four steps. I have made a static
06:36node class in my class.
06:39In my main public class, I have made my static class node in which I have defined my BST nodes
06:44in which I have defined my constructor. I have made my BST1 in which I have created from root1. Then
06:53I have BST2 which I have created from root2. My final BST should be something like this and we will
06:59verify it in the pre-order sequence. First, we will make a function called Merge BSTs. Public, Static
07:07its return type will be node, its name will be merge, bs please. In this pass, one is root 1
07:15and the other will be my root 2. Now I will perform the work in 4 steps. The first step
07:21is to calculate root 1 for inorder sequence. So step 1 will be get inorder for my root 1. Now
07:30for get inorder for my function, I will create a function that I have made earlier. What is inorder?
07:37on the whole tree and calculate it in an array list. So public static void get in order get in
07:46order get in order get in order get in order get in order get in order get in order get
07:49in order get in order get in order get in order get in order get in order get in order
07:59get in order
08:03I will take all the elements to the left. Then I will add the array list to the array list
08:08to my root data. Then I will add the get in order to call for my root right. Now I
08:17have added the array to add the root data. Now I will add the call to get in order for
08:21my root right. So I will get in order here. Then I will pass here. First I will make an
08:27array list of integer.
08:35New ArrayList. And here I have passed my ArrayList. Same way, I have to perform my Step 2. My Step
08:452 will be that I have to take my ArrayList. So I have to create my ArrayList which is my
08:53ArrayList. And I have passed my ArrayList and get ArrayList. The third step is to merge.
09:05i is equal to 0 j is equal to 0 i is equal to 0 i is equal to 0
09:11i is equal to 0 i is equal to 0 i is equal to 0
09:32If array 1.get i less than or equal to is array 2.get j so in case i will
09:43add array 1.get i and i will add i. No, else case in case i will add array 2
09:57.get j and i will add j
10:01So whatever small value will add in my final array and I will do the pointer so that we can
10:06analyze the next value. Just like the while loop is finished, means that one array is completely traversed. If the
10:13other one is left, let's add the while loop. Until i is less than array 1.size, we have to
10:21perform these two steps. Or until while my j is less than array 2.size
10:30I have to perform these two steps. So, in this way, I will merge my two arrays. Now, I will
10:38perform our fourth step. Fourth step is that I will create a balanced BST with sorted array list. I will
10:43create a balanced BST. This code we have already written. We have two times written this code. Now, I will
10:49make it a function again. I will make public static node balanced or create BST.
11:00Create BST के अंदर एक मेरे पास आएगी मेरी ArrayList of Integers. उसको नाम दूंगी ARR. उसके अलावा मेरे पास
11:10आएगा Starting Point and my Ending Point. मैं हमिशा क्या करती हूँ? एक MID calculate करती हूँ. MID is equal
11:16to start plus end divided by 2. फिर मैं MID पर एक node create करती हूँ. My root node. This
11:22is going to be equal to new node of ARR.get MID.
11:27Then I will create my left subtree and I will create my right subtree. So create BST, I will pass
11:34in starting mid-1 and the root will become my root left. Then I will call for my root right.
11:46I will pass in create BST, I will pass in array mid-1 and end. Then I will return root.
11:55In this case, this will happen that if my start will be greater than end, then in this case, I
12:01have no valid node create. I need to return null from there. So, this will be my create BST total
12:09function. This will be my getInOrder function. Here, I have just getInOrder and brought both in order sequence. I have
12:16merged all of them. After that, I will add call to create.
12:22BST को. Create BST के अंदर pass कर दूँगी array starting point 0. यहाँ पर array की जगह pass होगा
12:30मेरा final array list. starting point 0, ending point हो जाएगा मेरे पास. final array list.size-1 के equal
12:38और यहाँ से जो भी node return होकर आएगी final BST की उसको मैं कर दूँगी return. तो मेरे main
12:46function के अंदर से call लगाऊँगी node root is equal to merge root 1 and root 2.
12:54So, whatever final BST comes in, its root has come and now I will call a pre-order. So, the
12:59pre-order function also create. Public static void pre-order. In which I will have my root. If root equals
13:08null. In that case, I will return my control. No, I will print my root data. Here I will call
13:25pre-order for root left and pre-order for root.
13:31Right. Let's summarize. In the main function, I had BST1 and I had BST2. If I merge them, then I
13:40wrote the merge BST code. The merge BST code will return me to my root, which I have pre-order.
13:46How does merge BST work? In the first step, sequence 1 calculate in order sequence of my binary search tree
13:541. Then BST2 will be in order sequence of my binary search tree.
13:58After that, I have to merge both sequences in sorted order. Then I will create my balanced binary search tree
14:05in sorted array. I mean get in order and create BST codes. We have already seen a lot of questions
14:11in create BST codes. Now let's save and run. The final answer has come from pre-order sequence. This final
14:20BST should come in which the pre-order will be the first print 3, then print 1, then print 2.
14:27Then print it will be 9, then print it will be 4 and then print it will be 12. So
14:34in this way, we can merge any two BSTs directly by them and create a new balanced binary search tree.
14:42And in this creation process, I have to take much time with big of n. Or if it's size n
14:48and size m, I have to take total time with big of n plus m. If the total time will
14:55take big of n plus m.
14:55Big of n will be big of n and m will be big of n. Basically, that is equal to
15:01linear time. I have a linear time complexity for solving this question. So now we have solved many questions of
15:10binary source trees.
15:15We are talking about segment trees in range queries and in many other chapters we are looking at the question
15:21of trees. So, these trees are just here, the logic of the logic is still here. So, I hope that
15:27as much as we have asked them, we will understand them properly. We will understand the recursion properly about trees,
15:33because trees basically
15:35almost 80% of our questions are solved. You can practice a lot of questions, we have to do the
15:42assignment questions properly. We can go on external platforms and solve other questions of trees. We have already discussed classical
15:48questions in class. I hope trees, which are the data structure, will be easier for you as an experienced programmer.
15:56And if it doesn't, then we have to solve assignment questions. We will kill a few more revisions, so we
16:02will be clear.
16:04It will be easy for trees because in the trees there are mostly questions that are classical questions. So many
16:10companies ask extremely difficult questions. Mostly companies ask our classical questions and once their logic gets clear, they start to
16:17solve that question.