• 1 day ago
Transcript
00:00Next question which we are going to talk about is to convert a binary search tree to a balanced binary
00:05search tree.
00:29foreign foreign foreign
00:45My height is equal to 4, because in this BST, my height will be tricky, that will be equal to
00:503. So, I have minimum height, which is my BST, that is my balanced BST. I have to create it.
00:56So, what will be the approach for solving this question? In this approach, we will see that if I have
01:02a sorted area, then I can say that I will create a balanced BST.
01:11We have seen the sorted array in the last question that if there is sorted array then it looks like
01:15an order sequence. And we have to create a balanced BST with sorted array. For this, we have already learned
01:21the logic. If this logic is not clear, then we can look at the first question again. Not only if
01:29we look at the code, we will remember the logic. So, in any way, if I take sorted array for
01:36the sorted array, then I can create a balanced binary search tree with it.
01:40How will this sorted array come for any tree? For this tree, what I can say is that the in
01:46order sequence of this tree will be my sorted array. What I want to do is that the given binary
01:52search tree, like binary search tree, first I will remove it in order sequence. In order sequence is always sorted
01:59sequence. As I have in order sequence, then I will create a balanced binary search tree.
02:06So what do I have to do? First of all, I will calculate in order sequence. Once I have in
02:14order sequence, I will create my sorted array or array list to my balanced BST.
02:24For this, we have already written code in order sequence. We know how to calculate this tree. The in order
02:31sequence will come first 3, then we will go 5, then we will go 6, then we will go 8,
02:37then we will go 10, then we will go 11.
02:41Then we will go to 12. So the in order sequence will show something that I can have a sorted
02:47array list. I will pass an array list with an array list. I will pass the array list and then
02:54I will store all the root data. Like this data is stored, what do I do? Now I will remove
03:00the middle. If I remove the middle, it will become my root. This will become my left subtree. For the
03:07left subtree, I will remove the middle. If I remove the middle, it will become my left child.
03:10which is equal to 5. Now this will be 5's left child. So I'm going to add 3 to 5
03:15to the left. I'm going to add 3 to 5 to the right. I'm going to add 3 to 5
03:19to the right. I'm going to connect with 8 to the right. So 8 and its entire left sub-tree
03:23will show something in this way. After that, we will come to the right sub-tree. The right sub-tree
03:27will become so much. So I'm going to create my right child here which is 11. 11's left child will
03:35become 10.
03:36So, 10 will come here and 11 will become 12. So, 12 will come here. Now, I will connect with
03:4211 to 8. So, my final BST will come here. So, the process is just two-step process and the
03:50two functions are already written. The first part is to create.
03:56in order sequence, in order same recursive function, we will pass an array list and take our entire in order
04:02sequence. After that, the sorted sequence will come in the array list, which we can calculate in the array list.
04:09We will create our balanced BST, like we have done our last question, we will remove our middle node, we
04:16will create a root, all the nodes are left.
04:20They will become the left sub-tree and the right sub-tree nodes will become the right sub-tree nodes.
04:26For this, let's write code for this. I have a class or classroom. I have created a static class node.
04:32I have created a pre-order function so that I can create my final tree and print it. Under the
04:38main function, I have given this BST.
04:40This is already my code, you can copy directly this code. If you code with it, then we have to
04:47create our root node. The tree is also created by the structure of the tree. My final expected binary search
04:54tree should come in the answer. I will write my function. The name of the function will be public static
05:04void balance BST.
05:07I have to balance my binary search tree and I will give my root. In fact, we will keep the
05:13return type in the node so that the BST will balance the final node's value in the main function. There
05:20will be two steps. First step is to calculate my inorder sequence. Second step is sorted in order to create
05:28my balanced BST.
05:31So, first of all, let's calculate our in-order sequence. In-order sequence, we are going to store an array
05:36list. So, array list will be my in-order is equal to new array list. Let's import java.util.all
05:53Now, what will I do? First of all, I am going to create a function. public static void in-order
06:05Yes, I will say get in order. In get in order, I have my root node, above that I have
06:11my array list. My array list of integers, which I have named in order. What do I do in order?
06:19If my root is null, in that case, I don't have to print anything, so I can simply do return.
06:24No, first of all, I will call get in order for my root left. In the root left, all in
06:31order,
06:32all the elements will be stored in the sequence. After that, I am going to store in order.add my
06:39root data and then I will call get in order for my root right.
06:45and call us in order to pass the whole function in order traversal we will traverse the whole BST and
06:53in order array list we will return in order sorted sequence here I will call get in order which I
07:02will pass root and root
07:04So here my first step is complete which is to get my sorted in order sequence. Now let's talk about
07:10the second step, where we are going to create our balanced binary search tree with sorted in order. So let's
07:15create a function for this. It will be the same function that we have made last time last question. What
07:21did we do last time? We used the array list. We will use the array list. Here I will write
07:27public static node create BST.
07:34ArrayList of Integers which is my inOrder and with that I pass my starting point and my ending point. Now
07:44I know how to create sorted ArrayList from BST. If my start is greater than end, I will return simply
07:52to null. In that case, I will not create a node. No, I will calculate my middle value which is
07:59start plus end divided by
08:01to,us ke baad mujhe create karna hai aapne node root ko which is equal to new node of my
08:06inorder dot get middle,phir may recursively create kar dhungi starting se lekar mid minus 1 tak aapne root ke
08:20left ko,yaanhi root ka left aakar store ho jayega iske amdar and phir root dot right ko create kar
08:27dhengai by calling create bst inorder mid plus 1 se lekar mayra
08:33I will return my root node. What did I do? I left middle and created the middle node. Then I
08:41created the left subtree to start to mid-1. Then I created the right subtree to mid-1. Then I
08:48created the right subtree to mid-1. Then I created the right subtree to mid-1 to end. And in
08:51the last I returned my root. The base case happens that I have an invalid condition and start is greater
08:57than end. In that case, I will return the null.
09:02BST create and give BST, which I can store in my node root. The root is already created, so we
09:10will simply change the root value, and here we will call for create BST, which I will pass, in order
09:16starting point will be 0, ending point will be in order size-1, which means the total size-1. Lastly,
09:28I will return node to this root.
09:33So my root will return here. You can see there are two steps I have performed. The first step is
09:38sorted in order sequence which I have taken in this function. The second step is I have to create a
09:44balanced BST using the sorted in order. So I have simply left function which I have written in the last
09:51question and in the last I have returned my root. What will we do in the main function? First we
09:57will call the balanced BST for my root.
10:01whatever answer will be stored in the root and then call your pre-order to check that whatever balanced BST
10:08has created, it will be like this to verify so we will do it one time save and run I
10:16have the pre-order that shows something like this pre-order in the pre-order, first of all, I have
10:21to print 8, then 5 will be printed, then 3 will be printed, then 6 will be printed
10:28So I printed 6. Then in the right subtree, 11 will come, then 11 will come, 10 will come, 10
10:33will come, and 12 will come.
10:3512% so in this way we convert our normal binary search tree to a balanced binary search tree and
10:44in this way we will notice the time complexity which we will look like linear time complexity because in order
10:50of complexity we know how big of n is equal to create bst we have talked about complexity this is
10:56also big of n if my first step is taking big of n time then my second step is also
11:02big of n
11:03I am taking n time, so 2 times n is always equal to n in terms of my time complexity.
11:08So my total time complexity will become my binary search tree balance. That will become big of n.