[00:00:03] So as I already like a given into introduction or the overview, we will reduce the like ability by we need it. Right. So again, to like a cover that evil tree is nothing got like a self balanced binary tree. So why it is self balancing? Because in the case of, [00:00:39] binary search tree so binary search tree y came because to efficient the searching part right [00:00:46] so any node or any number you want to search okay you can search is easily like in the login time [00:00:53] right and uh if let's say that is only a skewed tree okay if dst follow uh and we input in the [00:01:04] order of like ascending or descending okay it will be like a skewed tree okay 10 20 30 40 okay [00:01:12] okay if you insert in a bsd it will be like a skewed so again the time will be increased right [00:01:17] so that's why like a evil tree comes into the picture so what it does is first it inserts those [00:01:25] elements okay so wherever it see like it's not balancing or something or broken down like a [00:01:33] breaking the balance it will be like a self-balance uh through its logic so [00:01:41] eventually followed two rules okay like first of all it follows the bsd rules like as we know [00:01:50] values smaller than the root go to the left and values greater than the root goes to the right [00:01:56] it follows that and another condition is like it follows the balance factor [00:02:02] like how the balance factor is calculated it's like a height of the left subtree minus height [00:02:08] of the right subtree right uh so uh it is introduced by two scientists okay so uh their [00:02:21] name is like a georgie adensal valiski and another one is landis so yeah so before or like going into [00:02:33] that i want to introduce to something important but a basic height of a tree okay so as you can [00:02:42] like you maybe ignore okay this topic but barely you know like there are two types okay if we [00:02:49] follow like it was not coming from computer science background okay so why we need to calculate the [00:02:57] height of the tree because uh in this balance factor only okay we need to calculate the height [00:03:04] of a left subtree minus height of the right subtree it's like a recursive function okay [00:03:08] so that's why we have to know how to calculate the height of the tree correct so again the height of [00:03:15] the tree is uh the node so so sorry height of a node is a number of edges in the longest path [00:03:25] from that node to a leaf node okay uh what do i mean okay so if you want to calculate this height [00:03:34] of 30 okay so through these edges to the leaf node how much it is like a coming like if you calculate [00:03:43] uh also like there are two approaches to calculate this okay if you calculate in [00:03:49] edge-based manner so it will be like a two okay because we are calculating only the edges [00:03:55] if you want to calculate in the node-based approach okay we may be ended up with the three [00:04:00] because we are including this node also as a height okay so agar malo take care [00:04:08] so let's say i'm writing something like a height 30 okay equals to 3 let's say i'm writing [00:04:53] three okay so which approach i'm approaching is like a node based okay because height equals to [00:05:01] one is here height equals to two is here height equals to three is here okay if i'm writing h of [00:05:08] 30 is like a two okay i'm calculating in the manner of edges okay then in the case of a height of 10 [00:05:20] is zero this is one this is two okay correct so uh so this is the two approaches okay so why i [00:05:33] introduced because we write in the formula of this okay like a height of a node equals to one plus [00:05:42] maximum of left height mine of and right height okay so why i written that because i also mentioned [00:05:50] the longest path okay so longest path means we have to calculate also the max of okay that's [00:05:57] why i mentioned the maximum of okay so why i added one plus okay why why like what's the reason and [00:06:06] and if you see here like i also mentioned the base case okay so so let's say you don't [00:06:13] have any like a tree right so i'm returning minus one okay and again i'm written here [00:06:19] in the node-based approach zero okay so can't we write like this okay so something like [00:06:29] there so so if you write okay maximum of left height okay so left height [00:06:39] lineup height height market so certainly prettyarla may taste like you to 돼 it so [00:06:45] very easily yep so many one playing figure one question and why we need that and if [00:06:54] this formula is using both of the app it merger similar ok in the baseball match and [00:07:01] if you see ok the base condition ok when you'll don't have any high ok so if the [00:07:07] node is none means if you don't have any left children or you if you don't have any right [00:07:11] children okay then i am returning minus one here okay means in this case it will return minus one [00:07:18] okay in this case 25 it will return minus one okay if we follow the edge based approach okay [00:07:24] so we don't have any edge right so that's why uh like a uh minus one why minus one can't wait [00:07:30] can't i return like this zero because right so i don't have any anything so i can also return zero [00:07:36] right why i'm returning minus one okay i'll come into that and if the node-based approach if you [00:07:42] see here i'm returning zero here okay so it can make sense to you okay like a okay so you don't [00:07:49] have any notes right you may return zero and if you are returning zero okay if we go in the node [00:07:56] base approach take it i get node-based approaches now if you are returning zero then why you are [00:08:02] writing height 3 okay why you are not writing height as a okay it may be like i told you okay [00:08:08] because it is counting the rule or node also okay that's why you may be okay so can't i write it [00:08:15] like this okay my formula here uh height of a node right height of node height of node [00:08:25] height of node equals to max of okay uh one plus need along a man okay so for the basic look okay [00:08:34] like a other economy in the layman term okay how he will think okay height of the left node or [00:08:42] left subtree or height of uh sorry max of left height okay or right height [00:09:03] i can also write like this right so i can also in the calculate like this and [00:09:08] like this okay so in the layman term if you don't have any height then you return 0 or you calculate [00:09:30] the height of a node you return like a max of left and right right so problem [00:09:37] there is one problem let's say i have one node okay 10 only correct so if you calculate okay [00:09:49] you're returning zero okay because you don't have any nodes right so let's say i'm adding one node [00:09:57] like a thing like this okay and okay so there is one node okay simplify okay so i calculated [00:10:10] okay so there is only one node it may be there left child or right child maybe we have only one [00:10:16] child uh consider it as a left side because uh uls last along okay so left side may we have a one [00:10:24] node left height okay and which is left height will be like a zero okay left height and [00:10:39] right height of root 10 mei bol rahaun pehle mei ake calculate karke ja rahaun [00:10:46] right height will be it will return zero okay when it will return zero it will also calculate [00:10:54] okay so what is the maximum of maximum of left height left right now okay zero okay because it [00:11:01] returns zero okay and right head get now it don't have any nodes zero means if you calculate we [00:11:10] already have the height of 20 as a malaki one in the case of lucky one okay so it is returning zero [00:11:19] if you see correct 10 is written in 0 because 10 height 0 higher okay so it will also okay [00:11:27] what is the like either liquid okay left height we calculate in this manner right and the right [00:11:36] height okay so left height and right height okay so what is the left height uh again like if we [00:11:46] calculate in the research recursive zero because uh height of height of 10 is zero [00:11:57] that's why it is zero okay so what is the right height zero okay it came like this so [00:12:05] if we calculate the max zero is it like that no like a 20 is like he is having the height of one [00:12:12] even though that we are getting zero so that's why we add one plus okay if you like a [00:12:21] uh consider okay we are adding one plus okay then if we see like a there is a edge case here also [00:12:29] like a this one okay i'm also copy that which edge cases this is like a uh edge edge node edge [00:12:38] best approach okay it's best approach so if you see okay if you will go like this so now we will [00:12:47] calculate the height of height of 10 height of 10 equals to 1 plus okay max of height of like a left [00:13:00] side is none okay that's why we are writing minus 1 height of right side is none that's why we are [00:13:07] are writing minus one so max of minus one is minus one so one minus one okay close to zero okay so [00:13:18] that's how it came okay then i will write okay so in this approach okay if you see in this approach [00:13:32] height of a 10 is came as a zero means perfect it came as a profit that's why we set it as a [00:13:39] like a minus one hope you got it like a why we set it as a minus one here [00:13:46] the edge base approach and then why we set it as a zero like a in the [00:13:53] we also like we will go through it okay once evil tree comes [00:13:59] there we will calculate iteratively like a height of the tree right so that's why [00:14:04] now okay now we will go to the portion okay [00:14:14] uh so there is a sample input called 30 20 10 okay which is in a descending order [00:14:23] so if you will calculate it then it will go in the left skewed correct it will go to the left [00:14:31] screwed yes we'll go to the left skewed then okay so there are four cases okay okay a build tree [00:14:43] by the way i return so i've written here yeah so uh yes so another condition condition number two [00:14:54] okay what i said is like a balancing factor height of tree okay so after you calculated [00:14:59] the balancing factor use every must know every node must have balancing factor of minus one zero [00:15:07] or plus one okay if the balancing factor goes out of this boundary then that node is not balanced [00:15:15] you have to balance again okay by uh rotating okay so there are four cases introduced like a ll case [00:15:25] rr lr and rl okay you already i think introduced to it okay so ll uh rr l rl okay correct so [00:15:55] in the uh ll case ll case means what i have shown here like a 30 20 none okay in this [00:16:03] so this will be like you have to do right right rotation so if you apply the right rotation [00:16:16] it will be like 20 so hope you understand like how it will be and [00:16:29] okay so after you rotate okay the rotation after rotation so if i want to show you before [00:16:57] rotation 30 20 and okay so if you insert and follow the bst okay then it will be like this [00:17:15] correct so if you will calculate the balancing factor okay so i'm going in it's the best approach [00:17:21] okay so what is the height of a tree zero okay here doing column here so this is by the way [00:17:35] height of 10 okay height of 10 is zero so like that okay height of 20 height of 30 okay [00:17:57] okay so if you will go to a spaced approach then if you will end up with height balances [00:18:08] too okay so this is out of boundary right so i want to reiterate to that point okay [00:18:19] every node must okay so every node must have balancing factor of minus one zero [00:18:29] or either plus one so here you will see uh it is like a two correct so it is out of boundary [00:18:38] so so we have to see what is the case okay so from this node you will see this is going in a [00:18:45] uh you have to see like a only two below nodes okay how it is going okay so it is going in an [00:18:52] ll case okay because it is going in a left left okay rotation so left left case so you have to do [00:18:58] right rotation okay by considering this as a mid node okay and you have to rotate like this [00:19:06] okay so in a clockwise manner you have to rotate after rotation you can imagine the node is also [00:19:13] balanced and it is follows bst okay how it follows the bst you will see the all the smaller node are [00:19:23] like this all the greater node are in this correct so this is the ll case okay so this [00:19:33] is also the easier one rr case 30 oh sorry 10 20 30 okay so you will see like before rotation [00:20:11] it is like this okay if you will rotate to the anti-clockwise manner by considering [00:20:17] the 20 as a mid node if you will point something there and you rotate just up one like a the 10 [00:20:25] node to the right okay so then it is also will come like this okay also come like this okay [00:20:41] so this is the rr case okay so in the lr case okay so unbalanced node i have to take let's say 30 [00:20:57] 10 20. if you will insert 30 [00:21:04] 10 will go to this and then it will something like this correct 30 10 20 okay so then you [00:21:34] have to do first left rotation okay so we will consider first of all um so you have [00:22:07] to like a t 10 20 okay left rotation okay then right rotation so it will be first like this okay [00:22:51] 10 sorry 20 20 20 and then 10 after you left rotate okay after uh left rotate at node 10 okay [00:23:21] it is like this okay then you have to do like this like [00:23:28] the second rotation like a right rotation at a 30. okay so it's like a if you will see it's like a [00:23:44] ll case right so after doing so it will come like this only um after right rotation yeah [00:24:11] then in the rl case we'll take one example it is okay 10 30 20. okay so if you will insert [00:24:47] then it will be like 10 30 20 okay so 10 30 20. so first of all you have to do the right rotation [00:25:17] okay so after right rotation okay right rotation at 30 so it will be like 10 20 30 so if you will [00:25:48] see this is a uh rr case okay so again it will come to the rr then you have to like a do the [00:25:58] left rotation here so if you will do the left rotation then it will come like this [00:26:05] got it so these are the four cases okay and above this you cannot expect anything any other cases [00:26:18] next cases you can find any bigger trees okay so if you will take okay so we will take one [00:26:31] example okay we will take one example like 30 20 40 10 by the way any doubt so far [00:26:57] I have a doubt with respect to rotation so as we know that if any node which balance [00:27:47] factor goes beyond 1 0 minus 1 then that that particular node is qualified to be rotation [00:27:57] right yeah is my understanding correct yeah correct but how do we determine whether it [00:28:08] should be rotated clockwise or anti-clockwise i mean this is the simpler example then [00:28:14] we know that yes but if we let's say we have thousands of number in that case that's why i [00:28:22] taken like taking this this one in the example i'll show you like uh how you can know i cannot [00:28:31] able to take like a longer for the trial no no yeah it's fine i i just want to get uh understanding [00:28:41] so in that case okay so so what all i take like three notes right so you have to you have to see [00:28:48] see like a below two nodes where it is like a failing and on that you can see where he failed [00:28:59] I don't know [00:29:09] I'm [00:29:13] we have two nodes, 20 and 10, so our rotation starts on 30, right? [00:29:19] Correct, correct. [00:29:20] So, we have 30 to clockwise rotate or anti-clockwise. [00:29:25] How to decide? [00:29:26] You can see which cases are aligned. [00:29:30] Okay, so this case will be given. [00:29:36] Right? [00:29:37] This case will be given. [00:29:40] this is my question is that we decide that we have to rotate [00:29:47] clock wise [00:29:48] you can say [00:29:50] like [00:29:51] let's say in the LL case [00:29:54] this is a tree [00:29:56] skewed [00:29:58] left [00:29:59] okay [00:30:00] in the RR [00:30:02] right side skew [00:30:04] in the LR [00:30:06] LR, something like this, and in the case of RL, something like this, something like this. [00:30:14] So, you have to know from there, which case is. [00:30:19] Okay. [00:30:21] So, by observing the tree, you can be able to know which case it is, then you can apply [00:30:26] with respect to that. [00:30:30] Okay. [00:30:31] And one more doubt is, there are two approaches, [00:30:35] a node-based approach, and then we have the age-based approach. [00:30:39] so while calculating the height [00:30:44] so node based approach we take minus 1 [00:30:47] lift nodes on the left node [00:30:51] because it doesn't have any child [00:30:52] null pointer so we give it minus 1 [00:30:56] node based [00:30:59] I'm sorry yes [00:31:12] I'm sorry [00:31:17] formula i have one plus max of uh left and right one plus no no no there are no one plus [00:31:29] simply like this okay okay okay okay fine fine my bad [00:31:36] formula if you could scroll this is yes fine fine what it thank you thank you [00:31:53] because okay so it's cases are failing now you kindly explain in our case [00:32:22] what you didn't understand uh so bosses our case because you have still doubt you raised [00:32:36] your hand like you don't have any opinions okay so if you will see this example okay [00:32:56] so i'll write first that thing okay so certainly like we will first make one class node okay [00:33:26] then def innate values of sorry itself and you key data people know okay then self dot [00:33:45] key so self dot key equals to the self dot right [00:34:00] no sorry no no a l initial is not to hide zero okay so while considering a lift node [00:34:31] has zero edges so so as i'm also following like it's based approach so yeah so that's [00:34:41] why i given i can also write in the comment a lip node has zero it is so that's why i given [00:34:52] zero here correct and then i'll calculate the height as i already shown ever [00:35:01] i take one node okay so i have to calculate the height of that so i have to insert one node here [00:35:09] in the function if the node is none then return minus one [00:35:20] as is return node dot height okay so in the formula also how i am calculating the node [00:35:38] node dot height equals to one plus max of height of node dot height of node dot left [00:35:59] comma height of node dot right okay so this is how we can calculate the [00:36:19] height of the node and then what else like a balancing cutter okay [00:36:30] one minus three area. One second. You can return zeros. Correct. Otherwise return. Otherwise [00:37:37] we have to return the height of node dot left minus height of node dot right [00:37:59] and then what are the other functions yeah there is one functions would be [00:38:08] insert function in the insert i'll take the root and then the key why i have to take the root [00:38:28] because i have to compare with the root then i have to insert according to that [00:38:34] because we are following the bst that's right so if root is one if root is none [00:38:46] then return the node of t okay and then if t is less than root dot t dot left [00:39:11] If I live is less than, sorry, is greater than root dot k, root dot write equals to almost [00:39:53] note of return root yes okay so another thing i missed i forgot to tell you like after the [00:40:30] rotation we have to also update the heights right so so so let's say this one is in for like a in [00:40:42] the height of tube okay in the headspace approach height of tube after like a rotation okay what is [00:40:50] the height height will be minus one correct so according to that we have to also update the height [00:40:57] right so what we will do um insert oh yeah balance okay okay so there will be a update height also [00:41:17] okay otherwise first we will take the left rotate okay so this is very important so [00:41:35] So we will rotate right, we will rotate, so we will take that node, which we are like [00:42:04] rotating. [00:42:05] Let's say the node is Y, we just have to replace okay, we have to swap operation we have to [00:42:24] do. [00:42:25] okay in that case we will take one temporary variable x equals to [00:42:36] right what it's going to come left this may be nice for now so we will take that [00:42:50] left node and we will place in the temporary and we'll take another node and we will place [00:43:04] the x dot left sorry it's not right here okay then x dot right it goes to y okay [00:43:35] then y dot left goes to subtree and then here i will update the height also i like it iron okay [00:44:00] this one 3d mounted so same like a formula to update the height also you just have to remember [00:44:25] likely which height we have to update one plus max of height of x dot left height of x dot right [00:44:49] then you have to return the x with this this is gonna be the same left rotate of x is gonna [00:45:35] be right y dot left so if you lose up then it will be like a exact opposite of that [00:46:02] So this is the purely opposite of this one. [00:47:13] we will writing those four cases okay so if you will see like a there is no any [00:47:25] i'm writing in a part-wise manner okay not like like also [00:47:35] so we already calculated the balance vector right here this one so we will calculate the balance [00:47:54] from balance equals to balance vector of root okay the balance is greater than one [00:48:29] e is less than root dot left dot key okay then return right rotate okay [00:48:49] This is the LL case. In the RR case, if the balance is less than minus 1, and the key [00:49:22] is greater than right dot k and return the left rotate lr case first there will be a [00:49:51] two case like a lr case mein pehle to left rotate hoega uske pada right rotate hoega right [00:50:00] it balances. So value left rotate over balance must be greater than one. And if he is greater [00:50:25] than root dot left dot k, no, not return. So first, the left rotate. So root dot left [00:50:50] equals to, I will try and wait a second, then return the A to T to Q2. [00:51:19] In the case of add-in, if balance is small, P is .y.k, then then write to .y.k and [00:52:08] um so consider this all our cases under the insert function only because when we will insert then [00:53:27] also we will calculate side by side which case it is so yeah this is all you don't have to remember [00:53:58] calculate the idea. So I don't have to calculate. Yeah. Okay. Okay. So now we can now we can [00:54:51] that example okay and try it on it so this is the example okay if you will like arrange okay [00:55:18] so this will be something like this okay 20 40 uh 10 then 25 okay so this will be something like this [00:55:50] okay okay correct so now the uh so if i will take the edge base then [00:56:30] this will be 0th okay so method is zero here one man okay it's right one so it's going to [00:56:40] okay correct so i calculated the height so i got to know that these are the heights of [00:56:48] correct now which is imbalanced so 30 right again like if you want to calculate so from [00:57:01] node 20 if you will calculate the height height of decade 20 will be 1 plus max of 0,0 so 0,0 [00:57:21] of 10 is minus 1 right. So, if node is none, then minus 1 is minus 1. So, 1 minus 1 will [00:57:55] be zero. Correct. So that's how for 25 also, it's same, the first 25, so I will be like [00:58:25] a one plus max of 0, 0 will be height of 1. Okay. [00:58:31] maximum height like a 20 say [00:58:35] way of each of the branch for the next day [00:58:37] it's like that so that will be one [00:58:40] so [00:58:44] or note that a [00:58:48] so for note that a [00:58:54] I talked that [00:59:00] one plus max of left sub trigger [00:59:05] height with my one hand [00:59:08] mama 0 [00:59:10] ZERO कैसे है, Right Subtree का ZERO ही आएगा, Right, क्यों कुछ नहीं है नहीं है नहीं तरह करके रिखाऊंग, So Just Replace 40 [00:59:24] YEAR, Then Imagine, Correct, So अगर इसके Height आएगा तो हुआ है, Correct, So That's [00:59:37] how this height came okay zero one two then this uh the valid uh avial balance vector [00:59:48] remain like a minus one zero and plus one right right so then um [01:00:04] then we will calculate the height calculator right so there to be all calculated the height [01:00:26] inserted the node to the correct position then we will calculate the balance factor [01:00:31] Okay, so the balance factor is also calculated, correct. And then it's got two, right? Well, [01:01:11] right. One, two, so two is zero. Okay, height of right subject. So these are, you can say, [01:01:23] okay okay so if you calculated the balance vector okay so i hope you can calculate the [01:01:42] balance vector otherwise i can show one balance vector of node 30 okay so you can clear of all [01:01:58] notes if you calculate the balance vector height of a uh left subtree height of a left subtree what [01:02:05] what is the height of left subtree is proof correct okay so height of left subtree [01:02:27] okay then we will calculate the height of [01:02:30] of right subtree will be one two six seconds [01:02:54] balance [01:03:29] same okay no issues we are going to do a note to insert insert another note okay [01:03:43] so this uh i'm sorry this example is already balanced okay so we will like insert another [01:03:51] node into this only okay so we will insert 22. so i'll copy this okay so if you will calculate [01:04:11] like a insert 22 22 25 22 so something like this okay so if you will see so which [01:04:30] case is this okay so because i think you're asking okay so where it is breaking so here it is breaking [01:04:37] right so if you will see from here okay then uh 22 25 20 so we have to first like a uh [01:04:49] like from here okay okay if you will see the node structure something like this this correct [01:04:59] that accent symbol right so if you will go on to the particular node so which case is this rl case [01:05:08] right so node is something like this now if you will insert 22 right so then we have to first [01:05:16] right rotate it then left rotation okay so [01:05:21] so we will like calculate if you will calculate also the balance factor here okay so this is [01:05:32] coming as a uh one taking one and then this will be coming as a two so this one is coming as a [01:05:44] minus 1 correct so at a node uh 25 if you will calculate the balance factor uh it will be like [01:06:00] a plus 1 correct and then uh if you will calculate the node at like balance factor at 20 [01:06:07] then it will become as a minus 1 okay so here if you will like this come as a minus 1 [01:06:16] and then this will be like a break okay yeah break over so node sorry no the balance factor [01:06:40] will be like two okay and minus zero this will become as a [01:06:57] so node 30 is now unbalanced because of 20 is 22 was inserted [01:07:11] and then we must perform the left rotation then the right rotation right [01:07:20] so if you will do the left rotation at 20 okay so just consider like this is like unbalanced okay [01:07:30] so just two nodes only two nodes balance vector calculate so after like a calculate the balance [01:07:41] vector just take the below two nodes 20 and 25 try to balance it okay if you want to balance [01:07:48] so before the rotation it will be like a 20 and 25 right we will consider that only three [01:07:57] three nodes okay then we will do first left rotation again because this is some kind of [01:08:07] not rl case lr case okay lr case so in the lr case we will do the left rotation okay [01:08:17] so okay yeah we are confused then what we are writing left rotate or left rotate [01:08:25] so left rotate x and then y equals to x dot right okay [01:08:41] sub 3 equals to y dot left okay so here x equals to 20 okay and then [01:08:55] y equals to 25 okay that's it and then the subtree will be like subtree will be like [01:09:09] a y dot left viker left man okay so if i will say then it will be 25 sorry 22 even y dot [01:09:24] left x is like a 20 y x dot right so 25 then this is 22 again the then the link changes okay so [01:09:38] the you will see the left rotate here this one again my copy got found again then we are stopping [01:10:01] y dot left will be uh assigned as a x okay if y dot left will be assigned as x [01:10:23] uh so 20 will be replaced by 25 correct so 20 will be replaced by 25 [01:10:43] because y.left ko x kiya and then x.right ko subtree kiya okay [01:10:57] start right ko order subtree kiya to pher ye oopar jayega 20 will be below [01:11:09] 25 will be above only this one 22 okay something like this good for you [01:11:31] then i think height will be calculated so height will be as usual calculated [01:11:36] taken let's say x dot height calculate like x dot left x dot right [01:11:46] it's going to change so it will be like that only okay so it will be like that only yes [01:12:25] so this is how it will look like left rotation then again it will update the height taken [01:12:37] height update uh so if you will see a height of the 20 will be 1 plus max of 0,0 over way one [01:12:48] either you know like how to calculate the height ticket there is no any change okay then we will [01:12:56] do the second rotation, which is right rotation at the node 30. So if we will do, so this [01:13:06] one only right rotation. Okay. So something like this. So 25 will be forever. Okay. Then [01:13:18] And here, okay, two will be here and then 40 will be here. [01:14:09] So now if we will calculate again the balance vector, it will be as it is balanced. [01:14:18] So this will be like a zero if we will calculate the balance, it's a balance that now left [01:14:26] on this side, minus one, okay, it's a balance over zero, okay, 40, 40, it's 30, 20, I'm [01:14:40] so this is how you will calculate so nothing just my other code [01:15:01] so right rotate then I did some x equals to y dot left and subtree will be [01:15:21] x dot right my y will be like 30 okay this this rotation se karna hai na this point se [01:15:35] rotate karna hai na woh pahle pass karna pade ga like rotating from node 20 right first [01:15:40] one okay so that i am passing as a x okay then mein abhi 30 se karna wala tha okay so [01:15:52] that I am passing as a y, okay, like the value which I am passing in that function that I [01:15:59] am talking about, then x will be my, so y I am passing, so x will be my y.left, okay, [01:16:08] y.left means 25, okay, y.left, left, right, so, first, so, 25, then, what is it done, [01:16:24] okay x dot right x dot right my question here on your next dot right is correct my [01:16:33] question okay is correct with the 45 my particular children are okay okay okay [01:16:45] this is correct my question here so that's why it is none then I am swapping the values [01:16:49] okay and then I am swapping the values like x dot right equals to y okay and y dot [01:16:59] left equals to sub tree so this is how i change the values [01:17:05] s dot right may y a jayega merah so y a gaya taken by jopaleta 30 to x dot right question [01:17:23] no game or white dot left making why i call left 2-3 the do so i will left my [01:17:38] so this is why [01:17:41] so y is like 30 [01:17:47] 30 ka left mein kuch nahi hai [01:17:49] 30 ka right mein 40 hai [01:17:51] 30 ka left mein is none [01:17:53] so yeah [01:17:54] so it is balanced [01:17:56] so hope you understand [01:17:58] so this is the main part only [01:18:00] or kuch main nahi hai [01:18:01] height calculate karna mahin hai [01:18:03] after that I will also again calculate the height [01:18:06] again then i'll again like a balance balance factor calculate [01:18:11] again that balance will be calculated then it will stop there [01:18:21] so it's kind of a recursive process so this is only the main part right [01:18:30] protect yourself left protect yourself these are the two main things i think [01:18:36] just so hope you got the pins of ability it's very hectic any doubt from this like the right [01:19:17] rotate and the left rotate and it's my confusion like i just confused like a where to rotate i [01:19:25] just see this pattern again uh in a bullet now because like i you have to see the pattern so [01:19:32] like this is unbalanced so i'll take these three notes but not like that after that i calculated [01:19:39] the balance vector okay this is balanced 20 balance would have to make you zone okay then [01:19:45] i calculated the 30 okay 30 is in balance this then i should catch the 30 okay then i have to go [01:19:50] the two below notes then i did that way so my bad don't go for the pattern kind of things okay [01:20:07] as it's been fused here yeah so any doubt on this? go ahead Anil Jai if you scroll up once [01:20:46] like you have done one rotation rate where you have got the maximum [01:20:54] height not this uh there is a this one only like i did the first rotation the left rotation [01:21:02] then i did this one yeah it's slightly uh okay i think from this example right you are asking [01:21:12] i did this one because uh because i confused that because i've taken [01:21:24] first [01:21:25] yeah, this is what you have [01:21:30] calculated height [01:21:33] just above [01:21:35] height up [01:21:36] you have calculated [01:21:37] 0 [01:21:39] 1 [01:21:40] this one [01:21:42] right [01:21:43] so, we have [01:21:45] health [01:21:46] calculate [01:21:47] 0 0 0 [01:21:48] we have got [01:21:49] 10 [01:21:50] 0 [01:21:51] 25 [01:21:52] 0 [01:21:53] and then 40 pe bhi [01:21:56] here pe koji leaf nahi hai [01:21:57] to woh zero hi like calculated ho raha [01:22:00] per jib 20 me a raha hai [01:22:02] to it has got edge also [01:22:05] and [01:22:06] uuske movement me above [01:22:08] right [01:22:11] max of zero [01:22:13] here pe ap ke pats [01:22:14] 2 no pe zero hai [01:22:15] but here pe 20 pe zero kuyo lik raha hai [01:22:17] mujhe ye poochna tha [01:22:18] because here pe already [01:22:20] a leaf hai na [01:22:21] it has 20 there is already two leaves left and right okay so what your doubt [01:22:32] so at the 10 10 to 25 you don't have any further leave right so that is a like a [01:22:43] null pointer but yeah and the 20 you have 10 and 25 20 have got the left one 20 [01:22:52] also got the right one which is so i have to write two here and anyway i'm asking like why [01:22:58] it is not uh uh why it is not as same as uh the 40 where obviously here right left [01:23:08] right is like a 40 no okay let me repeat it why at 20 you have max of [01:23:23] zero and zero that is the first question the first place even though we have the 10 that is a left [01:23:34] which is already present as a sub leaf 20 there is also like 25 right so why we have max of zero [01:23:44] and zero i first of all i followed the edge space approach okay what is the formula of [01:23:58] height of a node can you tell just a second what is the height of form how i went not [01:24:10] yes okay let me tell you okay what is the height of node I was asking but it [01:24:23] can include in no issues this is the height of it mode okay left height left [01:24:30] left so what is the left height here [01:24:36] if i just calculate from 20 itself [01:24:45] where we are [01:25:04] 10 or 25 [01:25:05] that is 1 leaves we have [01:25:07] where height [01:25:08] height 1 [01:25:09] or 0 [01:25:11] 0 [01:25:14] अगर calculate करके तो [01:25:16] नहीं जहां पे 20 है ना अपने अपने अपने [01:25:19] note 20 को हम select कर रहे है हम note 20 पे height [01:25:22] detect कर रहे है तो note 20 पे [01:25:25] जो left और right proportion है [01:25:28] sub tree का उस पे height [01:25:31] अमारी one आएगी [01:25:33] यह जब calculate करेगा [01:25:36] यह recursive है ठीक है यह recursive process [01:25:39] I can calculate it. [01:25:41] So, it's a return. [01:25:43] Zero, [01:25:45] because we have already calculated [01:25:47] that height is [01:25:49] this node [01:25:51] height, [01:25:53] correct? [01:25:57] I think, [01:26:01] we have no [01:26:03] minus. [01:26:05] Is it the right understanding? [01:26:07] Yeah, [01:26:08] I have to do that. [01:26:15] That is okay. [01:26:16] 40 to be same thing. [01:26:18] We have to leave it. [01:26:19] So, we have to do that. [01:26:20] 20 to be left and right. [01:26:25] So, I have to do that. [01:26:28] So, I have to do that. [01:26:30] So, I have to do that. [01:26:32] I have to do that. [01:26:34] Left and right. [01:26:36] so this is zero [01:26:38] because [01:26:39] in [01:26:41] local [01:26:43] to [01:26:45] local [01:26:52] zero [01:26:56] okay [01:27:00] equation [01:27:02] left [01:27:04] rotation [01:27:06] so [01:27:08] left protest [01:27:12] so [01:27:34] we know that [01:27:42] because i see the pattern [01:27:47] that's what i told right [01:28:03] I think if you have a balance factor check, 30 is how much is it? [01:28:08] 30 is how much is it? [01:28:10] 2 is how much is it? [01:28:12] 2 is how much is it? [01:28:14] 2 minus 0 is how much is it? [01:28:17] So balance factor is how much is it? [01:28:21] Sorry, sorry. [01:28:23] But edge approach or node approach? [01:28:27] oh [01:28:34] okay [01:28:40] ooh [01:28:53] so that's why so that's why so left rotate [01:29:05] I mean [01:29:13] I hope [01:29:14] 20 [01:29:16] 50 [01:29:17] 20 [01:29:42] I am not like that [01:29:49] I am not like that [01:29:53] after rotation [01:29:55] I can imagine in this way [01:29:59] I mean [01:30:02] rotate is nothing just swap [01:30:05] just swap [01:30:08] swap [01:30:10] then [01:30:12] numbers [01:30:15] kind of left rotate [01:30:17] sorry [01:30:20] skewed [01:30:23] one more question [01:30:27] swap [01:30:28] we have [01:30:29] theory [01:30:30] but [01:30:30] different topic [01:30:31] just [01:30:32] relate [01:30:33] I am trying to do this [01:30:35] I am going to try to do this [01:30:37] I have missed the previous concept [01:30:39] but the reason to swap [01:30:41] the existing value [01:30:43] 30 is higher than 25 [01:30:45] Here [01:30:47] If I want to add [01:30:49] 22 [01:30:51] I want to balance [01:30:53] I want to balance [01:30:55] I want to see [01:30:57] what is the nearest possible value [01:30:59] where I can add 22 [01:31:01] the same time मैं देख रहा हूं कि मैं 20 25 के नीचे आड करता हूं जैसे आपने की आये अरुट [01:31:06] तो वह 25 के नीचे मैंने क्यों एड कियो कहीं और भी तो आड कर सकता था [01:31:11] I'm going to be [01:31:13] do [01:31:15] I'm going to be [01:31:19] there [01:31:21] I'm going to be [01:31:25] calculate [01:31:27] as a strategy left [01:31:29] I'm going to be [01:31:31] lower value [01:31:33] save [01:31:35] left [01:31:37] is replacing 25 and then 25 again right head 20 [01:31:48] है है तो जब हम 22 को 25 से रिप्लेस करेंगे और 25 नीचे वाले में आएंगे तो 25 फिर उपर जाएगा वर 25 तो राइट है [01:32:01] 20 का राइट है मतलब आप ऐसे समझ सकते है कि 20 पहले आपने बूला कि 22 जो 25 को रिप्लेस कि लिए त्रू हाँ [01:32:13] 25 replace 20 [01:32:16] which is last page [01:32:18] 25 [01:32:20] 20 replace [01:32:21] 25 already higher [01:32:23] 20 [01:32:25] 25 [01:32:27] 20 replace [01:32:29] 20 [01:32:32] right rotation [01:32:34] because [01:32:35] already [01:32:36] right [01:32:37] 20 [01:32:41] right [01:32:42] 25 [01:32:43] is [01:32:48] a [01:33:12] I can't [01:33:16] I [01:33:40] the [01:34:10] right [01:34:12] is [01:34:14] 25 [01:34:16] correct [01:34:20] balance factor [01:34:22] calculate [01:34:24] this rotation [01:34:26] because [01:34:28] it is a sub tree [01:34:30] rotation [01:34:32] complete [01:34:34] height update [01:34:38] calculate the hole so something like [01:34:44] I know [01:34:47] it's about the height calculate [01:34:51] is about the balance factor [01:34:58] what's going on [01:35:04] if not balance then find the case [01:35:08] and balance it [01:35:11] calculate the height and then calculate the balance factor and then calculate and then [01:35:37] again check if balance or not [01:35:42] this is [01:35:45] I mean [01:35:47] iteration [01:35:48] movement [01:35:51] here [01:35:52] it [01:36:10] now [01:36:14] it's like a loop [01:36:18] right [01:36:22] right rotation [01:36:24] right rotate [01:36:26] so [01:36:28] this [01:36:30] is [01:36:32] this is now you've got the point [01:36:42] this is now you've got the point [01:36:46] We have a code calculate [01:36:48] then we will get it to a result. [01:36:50] Okay, a sequence [01:36:52] Okay [01:37:03] Mm-hmm [01:37:05] One last thing [01:37:07] I know [01:37:09] this question is [01:37:11] here [01:37:15] the output and the code and after left rotation after left rotation [01:37:36] after left rotation [01:37:38] this is one rotation [01:37:40] now the tree [01:37:42] we passed the right rotation [01:37:44] we passed the whole tree [01:37:46] we passed the whole tree [01:37:48] we passed the correct [01:37:50] 30, 25, 40 and 22 [01:37:52] values [01:37:54] now the present [01:37:56] how do we do [01:37:58] just for understanding [01:38:00] present [01:38:02] one array [01:38:04] array [01:38:06] 3.0. [01:38:08] Sorry, sorry. [01:38:10] Stack your form. [01:38:12] Stack your form. [01:38:15] Like 30,25. [01:38:19] value. [01:38:21] 30,25. [01:38:23] Node class. [01:38:25] Node. [01:38:27] That's right. [01:38:29] Left. [01:38:31] Itself is a data structure. [01:38:35] itself is a pop-identic data structure just for my understanding [01:38:43] I just want to write like after the left rotation what is output as a [01:38:51] what is output value only like a tree just for like a [01:38:58] So that's how I can imagine you. [01:39:04] Like you have written that example, 30 space, 40 space, like that. [01:39:10] What I am trying to say. [01:39:12] Now I have written that example, Aditya. [01:39:16] This one, this one, just slow, slow, slow. [01:39:20] 30, 20, 40, right? [01:39:23] This one, you have created a tree. [01:39:25] the same way left rotation [01:39:27] what should be connected [01:39:29] insert [01:39:31] the same thing [01:39:33] now we can read [01:39:35] the tree left rotation [01:39:37] what should be [01:39:39] how we will read that [01:39:43] read [01:39:45] can you write it please once [01:39:47] how's the output would look like [01:39:49] this form [01:39:51] after left rotation [01:39:53] left rotation after left rotation correct after only left rotation [01:39:57] not left rotation [01:39:59] not after left [01:40:01] sir [01:40:05] okay [01:40:10] I'm not sure [01:40:15] can you take a control [01:40:17] can you give me control [01:40:20] you can't understand [01:40:22] okay can you give me control any possibilities no okay we have so slow slows just this this this [01:40:50] either this one so you example tree right this is example tree what i'm asking is [01:40:57] can we read the actual value of output of left rotation in this way can we read it any time [01:41:06] no no no no we cannot read it [01:41:09] मतलब मैंने इस तरह से एक order लिया था तो मैंने इस तरह एक order लिया था [01:41:17] इस तरह order लिया आपने अब हम left rotation के बाद क्या इस तरह का order उस particular example को read [01:41:24] my [01:41:31] output of a left case [01:41:34] tree form [01:41:38] i want to [01:41:47] I started my tree [01:41:49] I had a [01:41:51] and [01:42:08] here [01:42:10] choose [01:42:12] I [01:42:14] do [01:42:16] I [01:42:18] do [01:42:20] I [01:42:28] do [01:42:30] I am [01:42:32] I [01:43:29] I can extend the question, which is a small data. [01:43:33] If we have a different data set, [01:43:37] which is very big, millions of records, [01:43:41] in case we have rotation movement, [01:43:45] like where our blank and where our balance factor is, [01:43:49] then we can easily imprint. [01:43:53] We can assume that this has gone to a perfect level. [01:43:57] to a perfect level because when you debug right level order [01:44:01] in the level order [01:44:05] okay [01:44:11] okay okay thank you thank you so much [01:44:14] okay thank you i'm stopping the control [01:44:31] yeah hi because [01:44:42] So basically, my question was similar to what Anujay asked, but if we combined all the concepts together, it's very difficult to visualize it currently. [01:45:06] I don't want to go in order then I'm going to go in order then I'm going to go in order [01:45:26] in a given tree and then we will calculate the height of given tree correct [01:45:45] and then we will calculate the balance factor or [01:45:50] and then we will have our if else statement will remain if balance then find the case [01:45:58] and then again check if the balance is not so iterative process means case [01:46:03] which is your statement it will execute every time until and unless our tree [01:46:11] we can balance [01:46:17] 0-1 [01:46:20] okay [01:46:23] and [01:46:33] initialize [01:46:36] so do we pass the sequence of elements [01:46:39] which we have made [01:46:42] five or ten number [01:46:45] When we create a node class, do we pass these numbers as an input? [01:46:52] Yes. [01:46:53] Where are we pass? [01:46:56] In the node you can write. [01:47:00] You can write something. [01:47:04] I will tell you a little bit. [01:47:07] high level value in values or something like that value in values of [01:47:21] input language and input of it is string [01:47:36] over for almost a mapping currently around values then we thought a new list of map [01:47:47] and both values in the values for i [01:48:06] value in the values will is a free affair i in values before the simplification [01:48:11] tilde n, starting from 0, index tilde n, means n-1 तक जाएगा, फिर आप insert function को call गर सकते है, insert of root, comma i, [01:48:33] and [01:48:48] ok [01:48:54] I [01:49:05] So in that case, the root will have the first thing to do if we have the tree. [01:49:11] Do you understand what I mean? [01:49:15] Basically, the tree is given. Let's say we have a BST tree. [01:49:19] So that tree will have the node. [01:49:21] So the tree will have some insertion. [01:49:23] The tree will not be given. [01:49:25] Something like that. [01:49:27] The tree will have some insertion. [01:49:29] So first we will make the tree. [01:49:31] How do we create a tree? [01:49:33] How do we create a tree? [01:49:35] Okay [01:49:39] My assumption is that [01:49:41] a tree has been given [01:49:43] and then we are asked to insert [01:49:45] a few elements [01:49:47] Yeah, yeah [01:49:49] I mean a little confusion [01:49:51] Okay [01:49:53] So basically you will [01:49:55] initialize and then you will keep on [01:49:57] inserting [01:49:59] element-wise [01:50:01] left and right adjustment element then I'm in the insert function only I'm initializing this one [01:50:15] yeah node initialize [01:50:23] okay sir a question is so basically we have height calculate [01:50:29] we don't have any tree and we are inserting an element that will become our root element [01:50:41] right and then we will calculate that height [01:50:51] I hope you can understand. [01:51:24] ii [01:52:14] regarding now there is also a visualizer left okay three node uh where my calculate that node [01:52:29] basement this is in balance stop up my height calculate right more of right imbalance may [01:52:45] balance right you to hide the car joe one two three like over node-based approach side [01:52:57] the photo and use the very dicey shan kashrap [01:53:06] it's also for me then the outer level now the right rotation the [01:53:24] original situation based on the pattern balanced factor [01:53:27] based on balanced vector not based on pattern uh my bad i told you like a [01:53:33] choose from the based on pattern okay so based on balance vector [01:53:43] thanks okay that's it for today i think we can close the class yep thank you everyone thanks