# DAA 07 Nov

- **Course:** 5101 - Design & Analysis of Algo
- **Recorded:** 2026-11-07 00:00
- **Transcript:** 381 segments, 6394 words
- **Made by:** deepdml/faster-whisper-large-v3-turbo-ct2

---
So, in the meanwhile, someone is agreeing for moderator. Let us begin the lecture and when the question and answer will start, we will add someone else. So, in the last class, we were discussing about the balance tree or balance M-way tree that is also known as B-tree. So, we have discussed about the properties like the root is allowed to have 2 to M-child and other internal loads except root are strict to have ceiling of m by 2 to m number of child nodes. And obviously, if there is m child, then the key, maximum key will be m minus 1. So, that is one property. And the next property that is quite important for this B tree is the, all the leaves should be placed at same level. That is important. And we have seen to maintain this property, the tree was growing upward. The tree was growing upward in this direction, okay. So, these were the major property that we have witnessed. Then we talk about that this is also called as 2 3 3 because 2 is the key and 3 is the number of child

we could have. So, a B tree of order 3 is also known as 2 3 3, okay. Then we started with an example of a tree with order 5 and the basic indication of the order 5 may be given in the quotient or you can see the maximum number of child in any of the node that will be the indicative factor for the order of the tree. Then we discuss about searching, insertion and deletion. Searching is as similar as M-way tree or you can say that as similar as BST with an extension on the each node having some set of sorted key. Then we talk about the insertion, different cases we have seen. Okay. And then we talk about deletion. Okay. So, in deletion, this was the, you can say, structure. Deletion could be possible when we have more than this much key, m by 2, on internal nodes. Okay. Or on leaf node. And if they have just this one. If it is greater than this one, so we can simply remove the key from the leaf. If it is just this one, then there are three cases we can borrow from the immediate

left sibling, borrow from the immediate right sibling or the last option was interesting where we were performing the merge when both the, when all the siblings have just bare minimum of m by 2 ceiling. So, we have seen a number of cases. Then there are two possibilities of deletion, one was from the leaf node and another is maybe from the internal node, internal node including root, including root. So, this is simple key, whether the element, it is having maximum key or not, if it is, sorry, if it is having more than minimum key, fine, we can simply eliminate it. And if it is not having minimum, then we can ask from the siblings, either left sibling or right sibling. If both the siblings are not having the, are on the minimum of m by 2, so we will merge with either left or right, bringing the root down and forming a new node. So in this way, we have also witnessed that in cascade mode, the tree height was reduced while performing the deletion. But at all point of time,

what we have witnessed that the leaf was maintained at the same level. And in this way, maintaining the leaf and ensuring the m by 2 node on each child, B tree forms a balance variant of of B plus 3, sorry, balance variable of M weight. Now, then if we are removing the internal load, so we can either choose in order predecessor or in order successor. And same way merger will also be done, likewise we were performing the root if there is not, it is not possible to pick out the in-order predecessor or successor from the tree. So, we have seen number of cases in the last class and I hope you people have understand the idea. And I have already shared this puberty. So, there are different cases explained, you can say in a bookish manner. So, you can follow it. Otherwise, you can simply follow what the steps are. So, let us do some two, three more examples and then we will jump to the, I will also try to explain somewhat about this B plus tree. So, if you understand B tree, your

90 percent concept of B plus tree is done. Properties are merely same, but there is one distinction that I will tell. Now, here we have a B tree of order 5. So, I am just revising or reviewing the revising B tree deletion. Now, we have this tree and of course, the order is 5 is mentioned. If it is not mentioned, then we will see the node with have maximum child. So, if you see here, it is having 1, 2, 3, 4, 5. Okay. Now, there is a requirement that we have to delete 95. So, in case of B tree, what was the prerequisite that we should first search the key, then only we can remove it. So, if you see here, it is 95. So, left of 110, then we reach here. So, right of 86 and what we encounter that is 95. And here, if you go m by 2, since it is 5, so we will get what 3 and here number of child is 1, 2, 3, 4. So, it is more than maximum child. So, simply 95 can be eliminated. Then the next node is 226. So, greater than 110 and here we encounter 226.

But if you see here, there are further charts. Okay, if and it is having just minimum, this is having just minimum and lower child as well. So, what we will do, we will either replace with in-order predecessor or in-order successor. But in case of predecessor, that is again having the bare minimum. So, we will choose what, whom, the in-order predecessor. So, 300 will be deleted and that is having the more than minimum key and will replace 226. So, the resultant tree with 300 here and 95 removed from here. I hope this is clear. So, if the node is having more than root, sorry, ceiling m by 2 nodes and if it is leave, simply remove it. If it is internal node, remove with its in order predecessor and in order successor, they have more than minimum number of node, minimum number of keys or minimum number of child. That is, of course, this is for minimum number of child. Now, we want to delete 221 from this string. So, 221 deletion is required. Delete 221. Now, so, where is 221 greater

than 110, less than 300. So here we are encountering 221. Now the criteria is if the node is having bare minimum that is m by 2. So they can ask from its left sibling or right sibling. If you see here the left sibling is having just minimum but right sibling is having more than minimum. So what it will do? It will from right sibling the smallest node will go to the root first and from their root a node will come down that is 300. So, now 300 is here, 440 goes up. Okay, so I will take the query and just complete this then, this page only. Then we want to delete 220, 70 from the, this one, 70. So, where is 70? So, here, here it is 70. Now, if you see the left sibling and right sibling, both are having where minimum. So, what we will do, we will bring down the 65, merge with either left or right. So, I am merging with left because I have picked 65. So, 65 will come down and a new node will be formed. But if you

remove 65, then this entire tree structure will disturb. So, what we will do, root will bring down, a new node will be formed and then 65 will come and form a new node. So, that is the two-step process. So, let me take the query for now and then we will see one more example. Afterward, will explain about B plus 3 and then we will start heap and forest and end the trees. Then we will jump into the scenario of the graphs. Yes, Anant. Anant, I have enabled your mic so you can speak. Okay, fine. So, after a long time, Parambir, I am making you moderator. Okay, so do handle the things. Just give me a moment. Okay, Parambir. So, if anyone ask any doubt, anything so let me know i hope you will do it regularly in the class so okay so this was the interesting aspect when we were deleting 17 then then there is a multi-label changes okay so these type of problems are repeatedly asked in the examination where there is a two-level deletion

because this is one of the challenging thing in the beat. Now let me talk about another interesting problem. So this is a, this is a order 3, order of this 3 is 3, order is 3. So here we can easily see 3, 3. So if you talk about the 3, so the minimum allowed, minimum allowed key is what? 2 and maximum is 3. Root can have 2 to 3 and other node can also have 2 to 3. So this is about the child node. This is about the child. So key will be 1 to 2. So this is your initial tree. Now a question arises, we want to delete 35. So, where is 35? 50, 40, 35. Here we encounter. And what is the minimum child allowed? 2. And here it is having 3. So, we can simply eliminate 35. So, here after elimination of the 35, if you can check 20 and 35, 35 removed. So, 20 is here, no problem. Now, from this tree, we want to delete 65. So, where is 65? Greater than 60, less than 70. Now, here we encountered 65. So, now 65 says I am having the just minimum, so I can take it from my sibling, but sibling

is also having just minimum. So, what we will do, 70 will be bring down, okay, 70 will be bring down, but when we try to bring down the 70, then what will happen, this is having just minimum key and there will be imbalance. So, what we will do, we will merge these two nodes, bring down 60, okay, and then 70 will go down and form a new node so here 70 has been come down forming a new node 65 was eliminated and 60 come down to a company with 57 and this will be the arrangement so 60 come down there will be a joint tree and then 70 and 75 will be placed now so these two things were the interesting aspect. Now, I am not going into the detail of B plus 3, because all the property, okay. So, B plus 3 is an extension, extension of B tree, all the property of B plus 3 is similar, similar as b the difference difference if i say all the information information are stored stored in the leaf okay so the information will be always

stored in the leaf so this means there will be a there will be a pointer from all the internal node to the leaf one important second thing is that all the leaf all the leaf nodes nodes are, all the leaf nodes are linked. So this means that if we are in the very low level, so if I draw like this, so these are the leaf, so they are linked together. All the leaves are linked together. So if let us say I am accessing some information here and another demands come, so I will not go up, I will just move in this direction and search okay so what will happen okay so there will be a link to a leaf where the information is stored okay so two things are there one is all the leaf are interconnected that was not there in the b tree and there will be always a link toward the root okay so for example here i am storing 25 and here also you can witness 25 so this 25 is the address thing and this 25 is the direct connection with the real data.

So all the data related information will be directly linked with the root and intermediate node and root node will have a link toward that node. So you will see the duplicate entries in the B plus 3 because 25 is here located also here is 25. So this is address, this is also the address, but this is a direct linking to the data. So as we were discussing the memory organization, so here is the B plus tree, number of nodes are there. Now here there is index, if we want to access any index, so it will directly map to the address. In this way, fetching time is reduced to order of 1, fetch can be done in order of 1, but of course search will require some time, so as log of n. So if we are at some address, we can directly fetch it. If we are at some address, we can directly fetch the information. So basically, if you see B3 and B plus 3 are almost same, two things differentiate B plus 3 from B3. All the information are stored on the link.

This does not mean what we were writing 65, 35, anything. In this tree, if I say how to form this B3 into a B plus 3, so I will just link this. And here if you see 20, 45, 55, 58, 70, 75, so all these nodes. will help a address copy okay so for example 58 here so of course here will be 58 somewhere here it is let's say 70 here it may be 20 here it may be 45 anything because maybe or that the b3 property is falling but this is linked this 20 is linked this 50 is linked this 70 will be linked okay so all the leaf nodes this will be directly connected with the data directly connected with the data. So, this is how we can easily differentiate B tree and B plus 3 and therefore we may witness duplicate entry in the B tree. So, this property we may witness or we should witness, there will be duplicate n tree in the tree because we know that the internal node or root node will have some value that value is also persist in the leaf and that act as a pointer to that data okay so

i hope you people get a visualization of how p tree looks like okay so let us start a new stuff so uh for a bit just provide access those who have any query otherwise we will start the second last thing in the tree the case is having some query because i'm having your mic uh uh yes hello sir good morning yes good morning sir uh i have a query around b plus trees so as you mentioned that all the leaf node will contain the actual data yeah and the address of the data will be stored in either in root node or the intermediate node right yes so is it a some sort of i mean address that will be pointing to the leaf node or we are talking about the indexing thing that also indexing is basically the address when in our book index page is there the topic of b plus t is located on this page so that is basically the topic name and its corresponding address but the actual data is located on 271 page let's say so that that set of thing is there so

all the information that all the values that we are witnessing in the internal or the root node will be the address only and there will be a pointer to the leaf for that corresponding address okay so let's say if i delete any any data from the leaf node then the linkage link will also so we will not delete the data we will delete that link initially so same deletion of the node will happen and the link will also break and we know that now if there is no pointer to some address so it is automatically eliminated in the memory okay okay yeah thanks okay and view of no queries let us proceed uh sir one more doubt around insertion so when let's say if you want to insert an element into the leaf node so how the traversal will go i mean we should have to start from the root node and then the intermediate node and then eventually it will go into the leaf node or how how that will happen okay so let's say we want to insert so we if you if you have seen so insertion

was primarily happening happening in the leaf part okay so when we try to insert so if we are directly inserting in the leaf so it is direct pointer to it okay and it's in somehow if more nodes are inserted downwards or some leaf level is pumped so in that case so there will be so either the data will be that either we have direct access to the leaf or the access via the internal node so whenever we insert a new node so we will we will just establish a connection as well okay okay and the corresponding address as well in the root node and yes yes so in the root node we will have same B tree structure, 25 and on the other side 25, but their copies are stored in the leaf. So, 10 copies also persist, 2 copies also persist, 25 copies also persist. So, in that way, the tree will be done. So, what I will do, not today, in the next class. So, we will see how deletion and insertion is performed. So, it is easier than B tree. We will see.

okay thanks thank you sir uh yes good morning sir so uh at the end uh you have uh said that this uh b plus tree like if there is a duplicate entry in the tree that will be acting uh like as a pointer to the data yeah what do you exactly mean by that like if you can show any example so actually i had not have those slides okay and the next class so uh basically what will happen if i pick any tree so for example here is 57 okay and in the leaf also you will witness 157 okay either the data directly reside in the leaf or if there is uh internal load that holds the address so that will of course a pointer so it will be 50 kind of getting so if you witness 50 in the root you will also witness 50 in the leaf if you witness 40 in the intermediate root you'll also witness 40 in the leaf okay or if you witness something in the leaves of course it is direct pointer to the address okay okay got it yeah if it is not clear

i agree so in the next class i will show you two three slides what how deletion and how actually this b plus three look likes okay so that i will show now starting on and testing and easier thing because we people have witnessed a number of complicated trees. So, the last one is heaps. So, heap means that, so there is no foundation of who will go in the left, who will go in the right, whether left is less than root or root is less than right, no foundation is there. Just there is one foundation that if I say heap, so heap and heap sort we will discuss. So if I say anything mean heap, what we have to remember, the node on the root will be less than these two, whatever these values are. Maybe this is high, this is low. Then if I pick this node, so this must be less than these two nodes. If I pick this node, then this must be less than these two nodes. So if it is mean heap, then the minimum entry will be stored on the root.

the root will hold minimum value. Minimum of these two, minimum of these two, minimum of these two, similarly minimum of these two and so on. If it is max heap, so maximum element will be stored on the root. Maximum element will be stored on the root. So this is the one property and it allows duplicate entry, it allows left should be greater than right or right should be greater than left. No worries, just you have to remember one thing. The root will always hold the value that is if it is max heap greater than its left child and right child, this will be applicable for all the nodes. And if it is mean heap, the root will hold the minimum value that is less than that of left child and right child. And important part is that heap always try to maintain a complete binary tree. This is important property. There are some questions where people confuse how to think. So, let us say we have a set of number. So,

if we form a heap in that case, we will try to close or be close towards the complete binary tree. So, we will see some example as well for this. So, with the example, we will see. Now, this one interesting thing is written unless stated otherwise we will assume h is maintained in the memory by linear array that is also good so with the help of the linear array and that property k 2k 2k plus 1 what we have we have understand in the sequential representation mainly we will use array for representing the heap but if it is mentioned to use the link so we can use for the link representation as well. A heap h is called as the heap or max heap if each node n of h has the following property. The value of n is greater than or equal to the child. So, equal sign is also, let us say this is 70, this is 60, this is also 70. So, this is allowed because here it is written greater than or equal to and in case of b st m way tree b tree that was

not equal to it was greater than less than okay then accordingly the value n is greater than or equal to the value of any of the descendants that is also important if there is another form this This is called max heap. This is max heap and the mean heap that we will use in the heap sort. So mean heap is defined as the value n is less than or equal to value of any of the node of child of n. That is important. Now let us see. So see, if you see here 97, if you see this is greater than 85 and 95 or 88 is greater than 66 and 55. 95 if you see here they are equivalent and here left is greater than right no worries that is allowed. And 48, 95 is greater than 62, 77, 48 is greater than 25 and 38 and so on. And what you witness that here also there is 38, here also there is 38, 95, 95 repeat entries are there. Here 55 and 55 are there. But if you see we are trying to form a complete binary tree structure here we are trying to form a complete binary tree structure here because it is using

same k 2k 2k plus 1 property and here if you see all the element in this array is completely filled okay now the insertion in the heap is very easy okay insertion in the heap is or what i will do i will try to reconstruct back this heap from this arrangement and see how the structure is form very easy and this is max heap. Let me add one slide and show you. And then we will reconstruct a heap as well. So, let me write down the numbers and then we will construct a heap from that. So, numbers for 97, 88, 95, 66, 55, 95, 48, 66, 38, 48, 55, 62, 77, 25, 38, 18, 40, 36, 26 and 24. Okay, just taking a short, short a minute break then when we are resuming now. Okay, so let us start. So, now we have to construct a heap. So, simple property we have to remember that root should always have the highest value. So, I have started with 97 then following the complete binary so 88 will be the left child 95 will be the right child

no violation the property then comes 66 so it may be placed here okay then 55 then comes 95 so it will be done here then comes 48 because we are also following the complete binary then again 66 is coming no worries okay then 38 is coming 38 is here then we have 48 so 48 will go down the next node is 55 then also no problem so 62 and 77 then here we have 25 and 30 because this is a max heap so nowhere it is violating the property then 18 and 40 will come here 36 and 26 will come here and 24 will become. And if you see here, so we are trying to as close as complete binary tree. So, that is important, complete binary tree, we should be as close to the complete binary tree. So, this is the algorithm primarily. So, just one case if If you see here item, if it is less than or equal to 3 parent, then item will be accommodated. So simple algorithm it is, now here is another how we can form. So here 44 is coming, then 30, then if you can say when we are inserting, now one important

thing with this example that this was already a heap from which we have recreated it. But if we encountered here, so 44 I insert no violation of the property, then I am inserting 30. So of course, it can go to the left because we have to follow the complete binary tree property then comes 50. So 50 will be first added here. But this is violation because 50 is higher value so it would go to the rule. So there will be a swap operation and this this swap operation for adjusting to the root is called as the heapy find. be buying operation to restore the property of the heap. Now next element comes 22. So, 22 can be easily accommodated here, then comes 60. So, what will happen? 60 will be first accommodated here, but this is clear violation. So, it will be swept with 30. So, 60 comes here, 30 comes here, then 60 will be swept with the 50. So, 50 comes here and 60 goes up. So, this is the resultant. And again, this operation will be a heapify operation. Now, next thing is the next

value that is 55 appears. So, 55 will go here, but there at no point of time, the higher value will be on the lower part. So, 55 will swap 60. Now, I have swap 60. Now, 55 will be here and 40 will be here. Then the next node come that is 77. So 77 will be initially added here, but it will be swept swept and now 77 goes up and so on. Then again new 55 come and it will be accommodated easily. So now a question that here arises, whenever we insert, so what we have to take up in our mind. So the simple thing is that we will always consider that the root should be the highest value irrespective of the left and right. At any point of time if you see, so if you see 77 highest one, if you see this tree, this subtree, 55 is highest in this subtree, 60 is highest in this one, 50 is highest. So, everywhere we are fulfilling the property of the heaps. Now, we will see another thing. So, this already we have did. Now, deletion is very

interesting. Deletion is very interesting in here. So, we will always delete the node from the root. So, see what if I delete 95, then during deletion, the last node will replace the first node, the last node will replace the first node and or the root node and then Then we will perform the heapifying operation. So, what basically we will do remove root, remove root or not remove it is replace root with last element and restore the heap. Okay, heapifying, heapify operation we will perform and restore the root. Okay, then again remove it. So, what, what, what I am saying. So, after heapifying we are getting 85. So, first First I have removed 95, next I will remove 85, then it will be replaced with 15, then again restore things. So if we keep on removing and storing these removed values, so what we will form? We form an element arranged in descending order. Similarly, if we keep on deleting, if it is a mean heap, and if we keep on deleting the

root and doing the heapifying then again next root will be deleted and next to next root will be deleted. So, in that case what we will observe, we will observe a list will be formed that is in ascending order and this ascending and descending order is nothing but this is called as the heap sort. Heap sort is basically the iterative deletion of the element from the heap. So, it will be always root and if it is mean heap, we will get ascending order. If it is max heap, then we will get descending order and that is called as the heap sort. So, here it is saying application of sorting that is heap sort. Suppose an array with n element is given, a heap sort algorithm, sort a consists to build a heap out of a repeatedly delete the root. So, first thing is that building a heap. So, it will simply build and then we will keep on deleting the root, keep on deleting the root. So, deletion will be of course order of 1,

but heapifying will take log of n time, maximum. So, let me draw one tree and do the heapifying. Just give me a moment. Last tree I will draw and I will try to perform the heap sort. So, let us say we have 77, 55, 60. So, let us say we have a list of number and we want to sort. So, form a heap then keep or repeatedly delete the root and we will get a sorted one. That is the primary aim of the heap sort. 50, 30, 20. This was the tree and here it is 55. Now, Now, I remove 77, it will be replaced with what 20. So, resultant trees 20, 55, 60, 50, 30, 44, 55 and I have stored 77 in a list. Now this is a violation, so what we will do, which one is highest, so this 60 will replace 20 and resultant will be 60, 55 and when 20 comes here, so it will be replaced with 55, 30 and here we will have 44 and 20, because same heapifying, then 60 will be eliminated. So, 60 is eliminated with whom? With again 20, last node. So, then 155, either this or

that is totally up to you. So, we will prefer left one. So, here comes, then 50 will come here, then 20 will come here 30 will be added here and we will have 55 44 because this node is already removed and how this 20 comes here because 20 will be first swept with 55 then 20 will be served with 50 okay then 55 will be removed so it will be now stored here and it will be replaced with 44. So, the resultant tree will be 44, 50, 55, 20, 30 and of course, 44 was removed. Now, there will be a swap operation. So, resultant tree will be 55, 50, 44, 20 and 30. Okay. Now, this 55 will be replaced with whom? It will be replaced with 30. so 30 50 and that 55 comes here 44 20 so again swap will be performed the result entry will be 50 30 44 20 then 50 will be removed and added here and it will be replaced by 20 so 20 30 44 So, this will be swept, the result entry will be 44, 30 and 20. Then in the next step, 44 will be replaced with 20, so 20, 30 and 44 will be placed here.

Then there will be a swept, so will form 30, 20, 30 will be added here, then last 20 will be added here and this list is descending sorted. When we will use mean heap, then it will be ascending sorted. So, this is nothing but this is heap sort. So, I hope you people are getting the idea. And the last, the complexity discussion if we talk about. So, we have talked the deletion is taking order of n. But heap we find in each step taking log of n and if we are performing for n element, then it will be n of log of n. So, it is as equivalent to the merge sort, quick sort another algorithm that we have witnessed. So, now the last topic for the tree is forest. So, as we know forest is nothing but the collection of the tree. Similarly, same definition is applicable here. So, a forest is an undirected graph in which two vertices are connected to at most one path. So, two trees, then this the connection, this is connection. A spatial equivalent of forestry is an undirected acyclic

graph, all of whose connected component are trees. So, simply you can understand there is another tree that is connected and then this is undirected, acyclic. A cyclic graph is called as the tree. A cyclic graph is a tree. So, one more tree is remaining. So, let me take your doubt. If someone has any doubt, then let me know. Otherwise, we will start the last tree of the tree segments. okay no one is saying doubt or either all are in the festive mode or whatever I think okay to us is asking something yeah one time we can show that deletion hi sir am I audible yes yes yes you are getting so focus for the deletion one more time okay this one yes sir this one so your doubt is clear in this no no actually so when you remove the 20 like 20 you removed right so there you do did the hippie fight to remove i have removed 77 by replacing with 20 the last element okay and then we are doing ep5 so that it will go to its specific position so why we chosen that

last element 20 like because if you remember now we were following the complete boundary tree okay so there should not be the gap while placing the element in the array so best option is to pick that one and stop it so so we'll always be picking the like the last element the leaf element something like that to be yes that is the property that we have to be okay okay Arun is having whether uh yes sir just hello yes sir please good morning sir good morning sir can you again explain ep5 something i'm not on the ep5 is let's say keep 20 is replacing 77 that you understand now 20 is placed on the root and what we have said that the maximum elements should always be there on the root. And if you compare with the lower element, it is less than both the element. So, we will prefer the highest one to swap it. So, 20 will come here and 60 will go here. Now, when we see 20 again, we are having 45 and 55. So, it will be replaced with 55 here

and 20 will be here. So, 20 is now going to its specific position. So, that is called as the heap define. Just place the node to a specific location so that there is not a violation of this property of heat getting okay that means always root load always maximum value yeah root will be always maximum that is only one property that we have to maintain in heat okay okay thank you sir someone was also raising hand okay uh yes because so already your mic is enabled uh no sir actually last time last one okay fine okay uh so what i will do yes uh so number of interrupts are coming and in view of the festival as well uh so what i decided so to to close here okay and red black tree we will just we will see the properties okay not beyond that so that we will uh see in the next class so that probably happen on the next saturday because tomorrow is Diwali so happy Diwali to everyone and we'll close the session today

happy Diwali sir thank you happy Diwali sir thank you

