So, in the last class, we were discussing about the trees, just giving an overview. So, we were discussing about how tree could be represented. We were discussing how tree could be represented in memory. So, we identified there are primarily two mechanisms. One was link representation. Okay. So, that is you can say a form of sorry, a form of doubly linked list, doubly linked list where left field point toward the left child and right field point toward the right child and in between there will be a info part. So that was a link representation. Then we have seen how the representation could be stored in the memory and there was a little bit confusion about this avail space. So avail is nothing but it is the addition of the available memory on the top on the top means that the very recent deleted space will be the first part the pointer will point toward the very recent deleted space or very recent available space okay then we talk in the sequential representation so we were following the policy of k 2k 2k plus 1 on each node and accordingly the location of the node is determined in a linear array and before this class we have already discussed that while doing this sequential representation when your tree is sparse then number of space will be wasted in the tree okay so when we have arrangements like when your tree is dense or we can say when your tree is complete or nearly complete or not complete it is dense then we will prefer what we will prefer the sequential representation okay sequential representation okay then we will use sequential representation sequential representation means that representation via array and when we are having a sparse tree sparse means that the connections are not fully occupied or tree is very away from the you can say complete tree then we will prefer the link representation okay but usually uh in the lab as well when you will do the coding stuff then we will primarily cover the representation via the array array representation is more viable okay and easy to go but of course there if there is a link representation so you can use it after discussing these aspects we have to start working on the traversal of the binary tree so the traversal means that we have to visit all the node okay so uh in that we have seen there are three traversal mechanism that is called known as pre-order in order and post order as the name is specified pre-order means that we should cover root first then left then right child okay and this policy will be adopted on each and every node of the tree okay so you need not to consider entire tree at once you have to just check the three node that is left child right child and the parent node and for pre-order the thing is we have to use parent that is root left right okay so basically um on the previous lecture there was a confusion that you have to visit note exactly once that i will also discuss little bit today then we will go ahead with the next one that is binary search tree so in pre-order pre-order the traversal pattern is root left right pre means root should come first then in order when i talk about then in means left root right and when i say post order as the name is space left right root so this means that in pre-order the root will appear at the first position and in post order the root will appear at last position and in order specify the root is coming in between the left and right side okay we have also witnessed that using these two information that is pre-order plus in order or in order plus post order we can recreate the entire tree structure so rather than storing the actually tree structure that what we are witnessing we can store in form of two arrays and we can gain back the tree again when as and when required okay now on that day there was a question okay sir you said in traversal okay in traversal you have to visit have to visit all the nodes exactly once. But when we are doing a traversal, like I put A, then B, then going to D. So, how this could be possible? So, now let us think how this could be implemented. So, I am just taking a simple tree. So, I want to specify, identify the pre-order primarily. So, pre-order says root left right. So, now we are in the root and we have traversed it, visited. Now, we will do what? We will visit root left right. We will visit left child and put right child into the stack. This is important. So, we are not visiting, just removing all the right child and its sub-children in the stack. Now, we will visit B because B is the next node. Then what we will do? We will visit D and put the right child into the stack. Now, when we have reached to this end, then we will pick first child from the stack that is E. The next one will be the C. So, this is how we can get back the result. So, I am not saying VGT just put all the right child into the stack. Now, one more thing that this is a stack data structure that you people had studied. This structure has a property that it is one ended that is insertion and deletion can be performed from the top and whatever the element will be on the top will be processed first. This is as similar as the chapati container in our home where the first the last chapati being cooked is eaten first. Okay. Similarly, when I have to do the post order, then what is the target? That is left, right and root. This property we have to follow. So, we will initially put, we have, because the access thing is root. And if we find there is left and right child, then what I will do? push root into the stack then then if there is any right push it into the stack then we will traverse we will reach to the b still b is having some child b is pushed then e is pushed into the stack and last d will be pushed but d is the last element so what we will do d then we will pop e then we will pop b then we will pop c then we will pop a so there in stack there are two operations okay that is called as the push and pop push means insertion okay and pop means deletion okay pop pop means deletion so in this way this post order is maintained without so whenever you visit a node you you will find okay or we can say that this there was a marker that this node is the start node and that marker will also indicate the node is then you will reach to this address without visiting a and if i found these are engaged then i will put these two into the stack okay so that is there similarly in order you can do via the stack in order say that left root right so when i when i am at the address of a then i will uh go to the b and it say that what left root right so right and root will be put push into the stack okay then we have visited b's address not b and what we see both the fields are engaged then what we will do we will push the root and write chat into the stack then sorry of course write will go first then root then we will pop up one by one and we will get back this series so this is how so because we were doing manually but if you go with the implementation then post order and pre-order are done okay then post order and pre-order are done with stack or even in order all these traversals so the implementation if I write a line the implementation of traversal or basically you say tree traversal is done via stack data structure and stack data structures are used in the our implementation when we need some delay in operation okay stack data structure is used when we need delay some operation over other okay so if we want i have to delay some operation over other over other then we will preferably choose the stack data structure let's say there are real world scenario there are some process and some process a b c and we want that a should executed first then b then c but we have received the result from c first then b and then a then what we will do we will push back the result from the c onto the stack because this will help in delaying the operation until we have received the output from the operation a so this is why the stack is famous because it if we want to delay some operation then we have to put the the stuff or put the result or put the outcome into the stack so it will regularize the operation and follow the policy of last in first out this is also known as lipo those person who will be last will be pop out so like chapati container in our home okay so similarly other we have discussed we have also built up the tree okay and now uh i guess one hand raise is there i will check it out and then we will start one of the interesting trees okay and basically the primary trees structures are going to be start and with the binary search tree okay there is one question from um uh yes anupam you can ask your query anupam i'm audible okay we are because we have just started so we'll take the queries okay when we have complete some aspect of binary search tree okay in the very beginning taking questions uh may delete oh yeah anupam so uh you are raising hand you can ask okay i hope there is some problem with your end i will take up your query uh let us begun on the binary search tree. So, now up till so far whatever we have discussed, so we have identified there is one category of tree that is called as the binary tree where there is a possible two movement in the lower direction that is left and right or we say that there are two possible childs that are left child and right child. But we never impose a condition, what could we go in the left and what could be go in the right so for this in case of binary search tree okay before starting the formal definition i will show you one thing binary search tree has some property what is the property the left is less than a root which is further less than right this property will be pulled and if you see there is no equality sign no equality sign okay so this means that duplicate entries entries are not allowed in bst if the famous name is binary search tree or it is also called as bst okay so duplicate entries are never allowed in binary search tree. Now, one more thing is that if we have a list of element where we want to remove the duplicate entry, then best way is to construct a BST and automatically the duplicate entry will be removed. This is the best part that BST is used in the day-to-day operation primarily for removing the duplicate entry. Let's say we have the submissions from our students and we want to identify which submission is duplicate or then or we have to remove the duplicate submissions then we can simply create a binary search tree maybe rule number wise or something and automatically the duplicate entries will be removed so for example if we have a list of number 2 7 3 1 31 41 29 44 47 63 i am not putting duplicate entry because we know that BST will not contain any duplicate entry. Now what I say because we are constructing binary search trees so this property left is less than root which in turn less than right but there is no equalities and this is important part. Later on we will see one interesting thing that is B plus tree that may allow duplicate entry but before B plus tree all the trees will never allow the duplicate entry likewise bst avl tree rb tree mv tree b tree we will identify that these never allow the duplicate entries okay we will see some data structure like heap they allow duplicate entry but they had their own different property okay now if i say i want to construct a binary search tree from this then what i will do i will put 2 as first element that is root then 2 7 because left should less than root now 7 will go here now i'm talking about 3 so it is greater than 2 so go on the right but it encountered 7 then it will be placed on the left then one come so of course one is a smaller one will be put here then here it is seven and here it is three okay just give me a moment okay so um devasis i will take everyone's query don't worry just we are starting so we will take now the next number appears that is 31 so it will go to the root root say you are greater than me so you should be placed on my left then it go to seven since seven says uh you are greater than me then you could be placed on my right. So 31 will be placed here. Then 41 comes root because entry point will be always the root. So it says you are greater than me go to my right. 7 say you are greater to me go to my right. 31 say you are greater to me so go to on my right. Now 29 comes. So 29 2 says go to my right. 7 says go to my right. 31 say you are less than me then you should be place on my left. Then 44 comes, so it will be placed here. Then 47 come, it will further placed here. Okay, then 63 come, it will be further placed here. And if the 71 come, then it will be further placed here. So what we had identified in the left direction, there is very few nodes, but in the right direction, it is increasing very high. So this is also the problem of BST that is called as the skewness. Skewness problem that is tree is going skewed in one specific direction. Okay. Tree is going to be skewed in one specific direction but this we will see later on but important property here is that while constructing a BST we have to ensure that So, the left should be less than root and that is further less than the right. So, now a question arises, how to maintain or how to balance this tree? So, that we will see in the next part that is AVL part. But let us formally talk about what are the definitions and how we will perform the operation of insertion, deletion, searching and traversal. because in data structure we know that there are four primary operation that is insertion, deletion, traversal and searching. So insertion, deletion, traversal and search operations are there. That is very common. Now, if you talk about traversal or searching, we are not modifying the tree structure we are not modifying the tree structure so the property will not change the property that is left should be less than root that should be further less than right so this property is this property is not hampered whereas insertion and deletion okay so let's say we insert some node or delete some node may violate the BST property, may violate the BST property, not problem, property and thus we have to restore the tree property by performing certain operation that we will do later on, okay. That we will do later on, okay. That we will see how we can restore those property upon insertion and deletion okay now here's the formal definition how this works so one of the most important data structure in computer science because entire three data structures depends merely upon how BST will behave or we can say that this is you can say base of all three data structure. If I talk about AVL, if I talk about RB tree, if I talk about MV tree, if I talk about B tree, so every time I will refer this property of BST will be carry forwarded with additional of something else. So, you can say that is a very base of the tree data structure. Now the structure enables one to search for and find an element with an average running time of big O of log of n. This is important because in linear linked list, the timing is order of n, but here we are achieving in order of log of n and we know that this is more expensive. Although each node in binary search tree may contain entry record of the data the definition of binary tree so what what basically happened here i am saying only two is stored seven is stored 31 is stored so this may hold array of value okay not a single element this may hold array of value okay so that we will also see one the tree that is called mv tree so because in in most of these examples what i will discuss will consider the single element on each node but of course i am saying that it is not element it is a node so node may have any number of entries but preferably during explanation and everywhere we will discuss single element node so uh these are some examples so so uh if you talk about the more formal definition so the binary tree tree is called binary search tree it is a binary search tree is always a binary tree the maximum allowed child is two this is zero one or two if each node n of three has following property n is greater than every value on the left sub tree and n is less than every value on the right sub tree so that is simple left is less than two that is further less than right okay so on every node if this property is fulfilling you can simply say that this is a bst so left less than root less than right okay so for example if you see this example 38 so here we are having what 14 23 18 8 and here 56 45 8 other now here if you see 14 so 8 is on the left 23 and 18 on the right when i talk about the node 23 80 that is less than 23 so it is space on the left okay so if you talk about 82 70 is here 56 45 and 82 is here okay so this property will hold so this is not applicable for only the numerical value but you can also use in the alphabetical order like Harris, Cohen, Levis, Kell, Rubin, Brown, Green, Davis. So all the alphabetically sorted names are appearing on the left which are less than the root and that is further less than this one. So less means that here in alphabetical order they appear prior to that next that element okay so this is the binary tree now binary search tree property i hope it is clear left less than root less than right no duplicate entries are allowed and it may simplify the search time to log of n rather than order of n in case of linked list or in case of array also when we want to search some element so what basically we do we will compare each entry one by one one by one and whenever we a result is found we declare we have got that result but here let's say i am searching some element then what i will do i will go to the root and from the root i had an idea whether i should go in this direction or in this direction so at each step the size the search space is reducing at each step the search space is reducing and this is nothing but this is a logarithmic order log of n okay now we had this tree okay one by one we will insert new node okay and see whether the property of the tree is violating or not okay so let me draw this tree again so insertion i hope is easy just you have to ensure or maintain the property of the binary search team okay so we had this node 38 14 56 23 45 82 charge is 70 or 23 we had one more node that is 18 now we have to insert 20 40 60 50 33 55 and 11 in this way okay so we know that whenever we want to insert or delete we will first start from the root node and try to identify where I should place the node. One important thing in all the tree structure is that whenever you want to insert any node, you want to insert any node in BST, then we will first search because we know that duplicate entries are not allowed. Then if not found, we will add new element. Okay. And similarly in deletion, if we found, then we will remove otherwise we will declare the element is not present. So if i want to insert 20 so we have to search where 20 may be located so 38 and then we are going to 14 14 say 20 we should go to my right then it come to 23 then it say you should should be placed on my left then we visit 18 and what we found that there is no further node then we will insert 20 here. Okay. Its location is on the right of 80. Now next node come 40. So it will go to 38. 38 since you are greater than me. So you can be placed on my right. Then it go to 56. 56 says since you are less than me then you can be placed on my left. Then it go to 45 and say you are 40. so you could be placed on my left so 40 is inserted here okay so first we have to search whether that node is available or not if it is not available then only we can do the insert now next that we want to insert is 60 so 38 says go to my right 56 says go to my right so it go to 82 82 says go to my left the 70 say go to my left and here 60 is accommodated okay now it comes 33 so 38 says go you can go to my left 14 says you can go to my right and 23 says there is no availability of 33 because i do not have any node on my right so 33 could be placed on the right of 23. The next node is 55. So 38 says you can be placed on my right. 56 says you could be placed on my left. 45 says you could be placed on my right. 55 will be added here. Now the next node is 11. So I will first start with 38. 38 says you should go to on my left. Then 14. 14 say you should go on my left because you are 11 that is less than me then it go to 8 8 say okay there is no node that is whose value is 11 so you could be placed on my right so 11 will be placed here okay so this is how we could do the insertion in binary search tree okay so we have seen the insertion in two way one was we have started with a scratch value and next one we had a pre-existing tree in which we want to insert something and if you see at no point of time we are violating the binary search tree property that is left less than root less than right okay and none of the element are same we are adding only the unique elements okay now uh so complexity of uh searching so number of comparison in DST is bound to the depth of tree if you see what we were doing 38 then 14 then 23 then 18 then 20 so it could be maximum or the depth number of comparison will count to number of depth it will never be equivalent to the number of elements so here that maximum depth of this tree is how much one two three four okay label four okay so we could only perform so label is zero one two three four so height will be five obviously the height is five no height is four because four four nodes are coming in maximum and here it is one two three 4. So height of this tree is 4. So maximum number of comparison for inserting the new node will be in the order of this 4. So how many comparison we could perform? 1, 2, 3, 4, 5. So it is order of n plus 1 or so. So these many number of comparison you can perform and that is not equivalent to the number of element in the tree. So comparison will be equal to the log of n, if n number of node log of n, okay. So, the number of comparison in BST is bound to the depth of the tree. So, if your tree is having larger depth or higher depth, then your searching time will definitely increase. And we know that searching is prerequisite for insertion and deletion operation because we are not allowing duplicate entry in the tree. Then, this comes from the fact that we proceed down a single path of a tree. How long we can proceed? The running time of a search will be proportional to the depth of the tree. We are given n data, a1, a2, a3, an. And suppose the item are inserted in order of binary tree. So, fn will be obviously log of n. So, deletion of duplicate element. okay so this is application so scan the element from a1 to al from left to right for each element a chi compare with a1 a2 a3 that is compare ak with those of preceding ak and ak does not occur among then delete ak this is approach one okay okay so what basically this application is showing if we want to insert or are identifying the duplicate element from an element so we can simply form a BST and it will remove it or if you talk about the conventional mechanism what we people are knowing so what we will do we had an array of element if you see one then compare with remaining one whether the one is appearing on in any other place or not then let's say we have three then we will compare three with all the existing element and declare that so this become a tedious process okay so to avoid those complexity uh this binary search streak will be the best solution because it will never allow a duplicate element now insertion i hope is a little bit clear so we'll see a deletion in a while i'm just taking a two three minute break then coming back okay and then we will start the deletion afterward we will take the query from your side okay after deletion okay so now let us start with the discussion on the deletion in the binary tree so what are the three locations where this deletion could be performed first we will discuss this then we will see how deletion could be happened okay because we are dealing with a binary search tree so of course it is a binary tree so we know that the prerequisite of deletion release and prerequisite the requisite is search so that is one common thing next thing is that we could delete a node with zero child okay then we could delete a node with either left child or right side one child case okay and third one is we have to delete a node with you so this is the node that to be removed, this is a node that to be removed, this is a node that to be removed and this is node that to be removed. Not talking about the zero child. So, if there is no child, maybe parents are there, no child, then what we will do? Simply remove it. Simply remove it. Now, next thing is that if we have one child, Then what we will do? We know that entry, so the internal node and root node are not deleted, they are replaced. So, what I will do? I will first remove this. Okay. Then it will be stored into some temporary storage and will going to replace this value. So, So, the node will be replaced with its left or right child, whatever is available. Okay, child, whatever is available. Okay, now talking about this case, in this, we have two options, left or right. But here what we follow? Here we follow in order successor. So, in order successor will replace this node. So, maybe there are further other branches. So, accordingly the in order successor will be picked and that will replace the value because if we randomly replace such value, so there will be change in the structure of the tree. And one more thing that is called cascading okay so for example if we have this situation and we have to remove this node so number of child is one so as per our understanding what we will do this node will be replaced by this one but replacing this one is again a replace uh replacement with respect to the case number three so this is case one this is case two and this is case three so here it is the case is two but if you want to remove this then it become a case three so this is called cascading let's say the removal of node seems to be very easy but it involves multiple cascade cases so for example if i have to remove this then basically what i am doing i am removing this and this is case number one then it is replacing this then it become a case number two so this is how we can say that the deletion operation in the binary search tree is a cascaded if your tree is very large and if you are removing any intermediate node then this cascade thing will appear if you are removing the node at leap then no cascading simply remove it no no left and right check remove it but if there is left and right check then it becomes the intermediate node okay or we can say that root node or intermediate node So, there are three types of node in tree. Okay. One is root that already we understand that is the initial point or the from where the tree primarily start, then there are intermediate nodes. So, intermediate node are node having one or two child not zero child. Okay. And root is also a special type of intermediate node. Okay. And then we have leaf node, leaf node that does not contain any child so in case of your targeted value is a leaf node so we have to simply remove it and no cascading anything will happen but if you are removing a root or intermediate node in that case you can go through the either case number two or case number three okay so when it is case number two so you have to replace the node with its child and that may also lead to removal of another side. So, maybe the situation is like this one. If I want to remove this, it will remove this one. If I want to remove this, it will remove this one. If I want to remove this, then it will remove. So, this is called as the cascading. One after another after another. So, one change will change entire structure of the tree. So, this is called as the cascading operation in tree deletion. Now, here already we have discussed all these cases case one case two case three so n has no child then n can delete from t by simply replacing the location of n in the parent by null pointer okay if n is exactly one child so you can delete by simply replacing the location by the one child and if it is two child then you have to use what in order successor successor means that the next node in the in order one important thing is that in some books or some literature, it is also recommended to use in order predecessor. So if there is some in order predecessor question, so do not confuse because that is also allowed in some literature, but Corman usually follow in order successor. In both way, your property of binary search tree will not hampered. So this is one example. And this is the same structure that we were following. This is the same memory structure that we were following. So I will try to explain also if you are not getting at a point, you are not getting the how this is happening, then don't worry. Because primarily, our aim is to establish how deletion could be performed in the tree. So let us consider this tree as an example where if you see 60 25 75 15 50 66 33 44 are arranged likewise okay and if you see in the memory we are starting with the 60 60 is the you can see start mode it left is located at 2 right is located at 7 so left is 25 and right is 75 okay then for 25 its left is located at 8 if if you see 8 is 15 and its right is located at 10. So, if you see 10, 50, okay, 15, there is left only, there is no right. So, 0, left is located at 1, that is 33 and 4 to 33, the 9 is located on, 44 is located on the location 9 and there is no left side. So, this is the simple property, okay. Now, and here is one more pointer that is called avail. So, it is pointing at the 5th So if we remove node, so the pointer will shift to that location. That is simple understanding. Now we want to remove 44, the case of 1. So 44, so 44 can be easily removed. And if you see here, now 44 was located where? If you see at location 9, now avail is pointing to the location 9. So 44 was at location 9 and now we have removed 44. So this location become empty and added on the top of the avail list. Okay, then this is done. Now from this example, let us consider the case number 2. So this is the base example from here we are considering different cases. So case number 1 is this one, now case number 2, case number 3. So now talking about the removal of the 75. So if you see 75, so it is having the left child. So of course, it becomes the case number 2 for our estimation so to remove 75 we have to remove 66 that will going to replace the 75 so this is happening so if you see in here so 75 is removed and this is at location 7 and if you see here avail is now start pointing to the location 7. so whenever any deletion will happen so avail will be be start pointing to that location. So this is insertion and deletion will be happen from the top that is from where the node is started. This is important property of avail. Avail indicates the available list. Now, third case from this one, that is case number three, if we want to remove the node 25 and if you see 25 is having both left child and right and if I want to remove it, then what I will do? So, who is in order successor of 25? So, if you see in order successor means that left root right. So, here if you see the in order of this tree is how what what is the in order that is left 25 left 15 25 33 44 50 60 66 and 75 this is in order so who will be the in order successor of 25 so here if you can see it is 33 So, this 25 will be removed from the 33 and if you see 33 is also holding one node. So, this is case number 2 and this is case number 3. So, what will happen? So, 31 child, 44 will come here, 30 will go up and replace 25. So, 33 is here now, 44 is here now and accordingly the table is changed. So, this is how we can perform deletion operation in DST. So, seeing the case is 1, 2 or 3 and we also know that the case 1 and case 2 may have cascading. Okay and case 0 is not having cascading because we are removing the node from the leaf because it does not have any child. Okay now let me take your queries then we will start the another tree that is called avial tree and that resolves the problem of binary surgery. So, we will see what are the problems and how avial tree can resolve this now i will take the queries yes you are audible please i i have two queries sir uh you say that bst doesn't take duplicates now if we have an array which has duplicates for example it can be marks of students or anything that can have duplicate values in that case when we construct the bst the duplicates will be removed correct yeah yeah yeah when we reconstruct the array using the bsts those duplicate values were valid because they were linked to each and every student then that data will be lost how does that how is that okay so very good question but i am saying that i want to remove duplicate element then only i will apply so i will use roll number of the student if let's say some student has done five times same assignment submission by accidentally or anything so i have to consider just only one assignment okay so in that case bst will be applicable i'm not saying if the maybe in your class uh most of the people are getting let's say nine marks out of ten or something so of course that entry is valid so in that case we will never apply bst but if we are know that there might be the duplicate entry and i have to remove it then only the bht is applied so of course data loss is there but we are having unique entry from all the individual i am not saying the marks because marks may be similar in cbsc we are seeing 12 13 lakh students so most of the action let's say are getting 95 in the map so in that case that is uh the role number of the students should be unique not the marks okay so the second question which i had was that if you can just show the last slide where you were doing the deletion operations yes yes so now in this uh a where we delete 44 uh in this table why is 75 missing at uh entry number seven ah it should be there i just accidentally missed it okay so it should be there it should be there now visit enable mic for those who are raising you know devas is just yeah just to enable debashi's mic yeah hello hello yes good morning good morning sir good morning sir actually my question is regarding quiz actually this will be available from 19th so what would be the syllabus for this so syllabus will be whatever we have covered uh till today or i guess till last class if i don't forget don't nothing beyond this one okay so from um this asymptotic notation still tree because we have covered these many things before this quiz thing same patterns similar thing will be asked likewise what so you people are doing good really good okay so you will also do good so but this quiz will be a little bit you can say you can say complexity wise will be a little bit tough not more tough if you have done the classes seen the lecture so you will do all hundred percent so will it be from us from starting or from the uh from asymptotic analysis or you can say from insertion as well yeah asymptotic not before the asymptotic because this is quiz now mid same quiz so it should cover the mid same but in ends and what i will do i will um consider the syllabus uh whatever we have given in the uh meets them beyond that one okay and sir how would it be different from the assignments uh the pattern will be same but that the the the way of asking question or the you can say label of the question will be upgraded okay and now in quiz you are having 25 questions in assignment we were uh getting 20 questions in quiz you will have uh 25 questions and and to in to somewhere induce some simplicity i have also asked two false questions okay some very few questions but but questions are a level ahead of whatever we have seen okay any next one yes yes good morning sir good one uh sir uh if i just uh they can say go through the slide number 10 if that is okay yes yeah so in this uh when we say there is no children in case one right so uh and you explain in the ninth slide where you draw the diagram of this uh case one is sorry so there's zero zero child is also being called as one child or it will be a kind of a zero child if there's no no left and right one so so means that both the pointers are nil okay but in the ninth slide when you were just giving the diagram i think there was a right child which you explained uh is that is uh like yeah yeah this one the zero child is there nine nine child means that just one node one there is only one node yes the same case in the one child also that we have uh one root and one node that is left right so if i want to remove the root then it will become case two if i want to remove a leaf then it will become case zero one okay for the leaf side it will become case one so any leaf node will be case one intermediate node with one child is case two intermediate node or root with two child will be case number two okay okay uh my second question is sir uh if we go in the slide number 10 uh not the site and sorry eight eight actually okay so here you mentioned uh the approach a right so is it like we have multiple approaches uh or just there is only one approach here actually one more thing is the approach is that if we do not study binary search tree so what we will do we will form an array if one element is appearing then we will search in the entire array and identify no duplicate then remove then we pick second element search with others and say that there is not no duplicate so that is the approach a so that will be complicated like if we have to search it this is whether this is appearing again or not okay and the approach b is the binary search technique that is whatever we have discussed okay my third question is sir in conclusion to the parameters this question so uh as an ideal application is this a kind of approach we can apply on something as a merge uh much process like we have duplicate contacts so the contact has a label header as a name which is individual one but the numbers becomes duplicate so that's where we say there are you can say process occurs that you find to get a individual number for a unique mobile number or something like that yeah yeah yeah so is that something becomes a real time yeah so if you basically use some software or some sort of thing so on the back end we don't know what it is using so maybe it is using binary search tree okay okay so that is a kind of a unique identifier okay so there is function is unique or something so on the back end maybe binary search trees run we don't know because we never explore sure sir and in respect to this uh fn of order of log log to power any so is it occurs for each of the uh you can say each of the case where we say the insertion can be uh you can say case one of case three uh if we just say that or it could be on the running time only no it is uh how much time you could take for inserting a node so inserting a node means that what the comparison will be maximum to the depth okay maximum to the depth and depth of a tree could be log of order n only so it is possible to do the insertion and deletion this will take this much time because deletion and insertion is taking what we have identified the location then add that node adding will take order of one but the main time is consumed in the searching whether that element is available or not so the search time is log of n n so so we can say it would be either of deletion or insertion either of deletion or insertion if we search the node is available we we are removing then on log of n so that's why this binary tree are famous because it provides the computational complexity of searching insertion and deletion of order of log of n okay and if the tree becomes skewed so that we will see in the next part then this order of log of n reaches to order of n so how to remove that skewness and other thing will be considered in the avl tree sure sir uh and one last thing sir if you can go scroll up while appearing this i think 50 is missed so this 50 would be under 45 and then there is a right again as 55 is it yeah so 56 50 is less so go to 45 45 say uh you can be placed here i guess in place of yeah okay so that i missed so but but don't worry that that is exactly what yeah good morning sir yeah good morning yeah so my doubt is uh one place you have written binary search tree and one place i have seen in the slide it is written as binary stored tree so maybe uh so uh when we have started bst now so it's it is uh not it is binary search stream only and somewhere i have also read so this is binary sorted tree or binary search tree two things are sorted means that it is same property bst so in some books and literature if it is written so i have just referred it but binary search tree is very famous so bst basically name if you see bst it is a binary search tree or it is also known as binary sorted tree so binary sorted means that of course same property it is followed okay and the next doubt is on the last slide where you are i mean telling the relation of the second case okay yes sir here so if you will see the first case sir the tree and the table is not matching let's say uh in table second number if you will go info is the 25 yes in yes in this in this table only case number one case number one table if you will see about tables above this yeah about table right so in this if you will see the row number second info 25 left 8 and right 10 but if you will see the tree corresponding this that is 25 15 and 50 that i am not able to find 8 and 10 into the tree basically no no so 8 and 10 are the addresses so if you go to address 8 so you will find 50 if you go to address 10 you will find 50. so this table is the memory so memory holds the address and these are the info part and left and right are basically the links now so here it is linking that on it number location 8 the left child is present and on location 10 the right child is present this is the one last question in this one when you have told that uh what node we are having two chairs okay yeah and we have deleted the node so can you please explain the next one so if i had to remove 25 so because it is having two child so this will be replaced likewise in in case 1 with its in order successor. Therefore, if you see I have written in order. So, in order successor of 25 is whom? 33. So, this node will be replaced with whom? 33. Getting? So, for removal of 33 again what will happen? It is having one child. So, 44 will replace 33. 33 will be stored into the temporary location and it will now replace the 25. So, resultant tree will be removal of this here. 33 is coming and 44 here. So, two deletions. So, that is why I have told that it is a cascading successor node okay yeah in order successor so this is in order in order of the in order successor node basically okay thank you okay yeah thank you so i guess the queries are done obviously uh can we proceed uh sir i think uh uh loganath also wants to okay okay please i have allowed your mic yeah good morning sir good morning sir my question about the complexity let's see uh search and deletion right if best cases if we take it then it will be both same right log n yeah yeah and let's see i'm deleting a maybe the node so that case sir because it will cascade so that case the uh complex would be log n or something else because so it will be log n plus 10 log n plus 9 or something but it will never be so you can say it is n plus some constant then it will obviously end so i agree with your thing that it will never be exactly log n it will be something more than that but order will remain log n okay okay i think that's fine okay now we will see another interesting thing you know a situation arises okay where i have to insert a then b then c then d then z then the tree will be like this one so this is called right skewed it is a skewed toward the right words similarly the tree may be be skewed toward leftward. If I get the value like z, y, x, dot, dot, dot, till a, then how we can claim the log of n will hold here because it is not becoming a tree, it is now becoming a linear array. And we know that the searching time in linear array is order of n. So, hold the order of n. So, this is called as the unbalancing property. So, though we are maintaining the property of left less than root less than right but the tree is now unbalanced because it is skewed in one direction okay so the the the mechanism or the way why we have used bht is that it's was log of n it's searching time complexity of log of n but this will never remain log of n when your tree becomes skewed. So this problem or this challenge is called as the unbalancing problem in the tree. And so the disadvantage of skewed binary tree is that worst case time complexity may reaches to order of n. And so all the exercise what we have done in making the BST will be lost because now we can do similar in case of linear search. So, this arises the need to maintain the binary search to be balanced height. Balance height means that left height and right height should be not exactly same but should be comparable. Then only your log of n things could be preserved. So, by doing so, it is possible to obtain a search operation, a time complexity of log of n. So, the one the most popular tree or you can say that the balanced binary tree is Adelson, Walensky, Landis that is avial tree. This is avial. So anyone is having doubt because I mean, okay, fine. So what is avial tree? So avial tree say that I am a binary search tree, but I will impose some additional conditions such that both left and height, height will be balanced. So, an empty binary tree is an avial tree. Okay. So, let TL and TR be the left and right subtree. Okay. And HTL and HTR to be the height of TL and TR. Okay. So, this is left, this is left subtree, this is right subtree and height of left subtree and height of right subtree. Then AVL estimate the difference between height of left and right, and it should be less than 1. Or basically, AVL say the difference would be 0, 1 or minus 1. When I do the subtraction between the height of left tree and right tree, so it should be either 0, 1 or minus 1 for all the nodes. And if there is a violation in this, then AVL tree will try to balance it. So we will see how it will balance. But if you see this example, A, C, G. So, if you left height is for this node is 0, right height is 0. So, 0 minus 0 is 0. So, basically one more thing for leaf node, the balance factor is 0. So, this is called as the balance factor. So, difference between left height and right height is called balance factor. So, balance factor allowed by AVL is either 0, 1 or 2. 0, 1 or sorry 0, 1 or minus 1 not 2. Okay. So, if you see A is leaf node balance factor is 0. D is leaf node balance factor is 0. Not talking about the G. So, left height is 1, right height is 0. So, 1 minus 0, 1. Not talking about the C. Left height is 1, right height is 2. So, 1 minus 2 that is minus 1. So, now here if see it is minus one it is zero it is zero it is one so that is allowed because zero one and minus one should be the balance factor okay so now if we do the searching or traversal there will be no change in the structure so avial tree property will remain intact while performing the operation of searching and traversal. So, searching avial tree for an element is exactly similar to the method using binary search tree. Because avial tree is itself, avial tree is nothing different is a BST with a special property that balance factor should be 0, 1 or minus 1, 0, 1 or minus 1. And insertion in the avial tree will change the property. Let us say we want to insert e in this so c right g d and e should be inserted here now let us consider how what is the balance factor a leaf node 0 e leaf node 0 for d left height is 0 right height is 1 so it will be minus 1 for g if you see left height is what 2 right height is 0 so balance factors now become 2. For C, if you see, left height is 1 and right height is how much? 1, 2, 3. So, 1 minus 3 that is minus 2. So, here is a clear violation. Here is a clear violation because this is BST but not an avial tree. So, this now this tree become unbalanced. So, in tomorrow's class, we will see how we can balance the avial tree by performing different rotations. There are some rotations like LL, RR, LR, RL. So, these rotations are used to balance the tree after doing the insertion. Afterwards, we will see deletion also because deletion will also change the balance factor. So, there we will see R0, R1, R minus 1. So, we will try to cover some of them so that you people can understand how the deletion is performed in avial tree and what are the challenges that we may encounter when the balance factor disturbs. Okay, so the clear indication that 3 is not balanced in AVL whenever we are not having the balance factor of 1, minus 1 or 0. Okay, so because rotation itself is a little bit tricky, so we will cover in the tomorrow's class i am stopping here and so if someone has query then i'm happy to take it otherwise we'll close the session okay the number of queries are there so one by one abhishek enable so those who have done can leave uh leave the things okay because i just out your mic i said uh so can you go to the slide number 11 this one yes yes so in this case uh the before deletion phase um like i'm sorry for case one when we are deleting 44 then away is nine which is the pointer of 44 yeah but but in case two when we are deleting the intermediate node uh this case replacement is also happening where like the leaf note is replacing 66 i guess we have deleted sorry 66 is is replacing 75 so in this case uh the avail is 7 which is the pointer of 75 but 75 position is already taken by 66 right so why yeah i got your so why it is happening because we know that this 66 will remain at its location. Now, the parent pointer of 75 will start pointing 66. So, the 60 left child, if you see 60 left child is now here 2 and at 2 we are having 60 right child is 4 and at 4 now we are having 67. So, address pointer change. So, there will no swap operation of actual value just the pointer will start pointing to 66 as its right child and 75 become a node without any pointer address and that means it is now having a that is called a dazzling pointer and its location is marked empty okay okay please go ahead hi sir uh actually regarding that pre-order post-order traversal you use you told us regarding the stack implementation yeah yeah yeah yeah so it is clear or you are having some doubt in that unable to get your voice so obviously we can take up new and when the network issues please go ahead very good morning sir yeah sir basically I have a question like when we are doing this in order successor like when we are trying to delay there is one term you used called cascading so can you just tell the meaning of that because cascading what I have got as of now we have like cascading delete so like deleting one record to delete the another yes yes yes yes so so in this term what does the cascading means so let's say i want to remove 50 yeah okay so 50 removal required removal of what 33 and 33 removal required removal of 44 so this will be cascaded now to remove 50 i have to remove 33 to remove 33 i have to remove 44 so 50 deletion was not simple because it's releasing required what one two three and let's say if i want to delete 60 so 60 will be replaced with its in order successor in order successor is whom 66 so if we have to first remove 66 then 60 will be removed so cascading that that okay so basically here we are not removing the other things right we are just trying to replace it up so how the word cascading the term cascading so cascading will come because we are not removing 75 here basically the address link searches so if if there is no pointer to a value so it will be what it will be automatically removed by the memory so that thing is happening yes but so the cascading effect is like one changes the other changes right yeah like you told like when you're trying to delete so you told the key cascading means that one changes other so if i remove if i remove the pointer of 50 it is it will remove the pointer of 33 it will also remove the pointer of 44 so that that sense cascading okay okay so i got this another question i have uh when you are trying to explain the avl tree uh can you just go the balance factor thing yeah yeah so here the balance factor i could see the modulus of operator with this like left and right so so how could it go to minus one that is what i'm trying to get yeah totally i agree with this so here it should be less than or equal to 1. This is the basic formula, but for AVL, the case is 0, 1 or minus 1, where we will not apply this module. There is a formula of balance factor for AVL, these are the allowed value. So, this is just a representative purpose. For AVL, it is strict to 0, 1 or minus. So, if you use the modulus operation, so you should keep this is should be less than or equal to 1. But let us say what less than equal to? So, obviously, we will get two values there 0 or 1 so if you do modulo over 0 1 or minus 1 you will get 0 or 1 getting if you do modulo and we are restricting a limit of less than or equal to 1 so obviously two options are there only 0 or 1 and if you don't apply the model then you will get 0 1 or minus 1 next question is from Suraj. Sir, can you show me the slide number 11? I have one case scenario in my mind. In this case, if 75 has two child 66 and 68, and we are trying to delete the 75. So who will take the place of 75, 66 or 68? So in order successor of 75 is whom? Because in order means that left root right. So 68 will replace okay so when it has only two child 66 68 so always left one will be take in order always right one will take because in order successor will be the right one left root right in order we can also use replace with 66 so both are allowed but to remove the confusion point i strict to the in-order successor so in case of 25 we know the next route or the in-order route will take the place yeah yeah but in days in 25 case it is 59 50 only so we will always take the right truth success as a success yeah yeah yeah so this is a sir noted formula or this is something we just follow for the easy of the process no so if you if you replace with any other node now so there will be change in the property what we have left less than a root less than line and and using these two things in order succession and or predecessor the the structure of the tree remaining intact so that is our ultimate aim we should ensure that after deletion or after insertion the three properties should be restored okay sir understood that thank you sir good morning sir morning yes sir i want to suppose uh in my yesterday one problem i given to me my system manager i need to improve the performance okay so in one case i'm reading the data in one case i'm writing that data okay okay so in that scenario uh what i can want to say what is the best approach there means because this is the binary search now uh we can do much either take input either take output we're doing in order pre-order post or whatever it is so this is for performance only so when we are doing the input to the system going to output with that system so what is the way i can decide i will go and what like yeah how i can decide all those things okay so um what means uh you are targeting what could be the uh how you can take the input and how what would be the output kind of this is upon the input scenario so i whether i will go the preorder in order post order now in the output scenario ultimately i'm searching the data correct in both the cases yeah so what is the my best what is the best criteria will i go in order so just if you remember when in databases if you have heard about so we are using b3 b plus three or or there is one lsm trees okay why we are using because it's x so our ultimate aim is what our searching our insertion our deletion should be faster as fast as we can so for faster we have to shift to the three three data structure okay and we are imposing different different properties so that our access will be faster faster as as we will keep on adding more and more records so bht will ensure that if we are searching something greater than root so we will not bother about the left portion whatever the nodes are stored we will bother about only the right portion then afterward we will go down down down then the trees then the number of search space basically will be minimized or compact in each iteration okay one last one last question from Gyan Jan yeah good morning sir good morning Sir, my question is that if we delete this node, which is 2 child, then we can delete this in order predecessor. Hello. Yes. which will be the solution for the use of the property. Okay sir, sir, I have to understand this in order, preorder. It is a form of traversal. we will discuss that we will discuss that we can adopt that we will discuss on this okay sir thank you sir i think we have taken all the questions okay okay thank you thank you have the same so thank you thank you thank you so much thank you so much