 What's up guys, my name is Michael and welcome to my YouTube channel. Today we are going to go over finwick trees. I'm going to try to explain it how it works and then how how we got there and then pretty much what's the purpose of it and why why we do it. Okay. So let's say I have an array of a and the array of a contains these elements five two nine negative three five twenty and they are all indexed from zero one two three four five. Let's say I'm going to ask you now sum up the numbers from two to five right from the index of two to five now you might be wondering okay that's easy I just loop through the numbers from two to five so if I'm right here I'm just going to add numbers to the indexes at the numbers at the indexes two three four and five so I'm going to add nine plus negative three nine plus negative three plus five plus twenty and that'll just get me to thirty one right nine plus negative three six plus five is eleven eleven plus twenty is thirty one and that'll be okay right that's all right but in large numbers if in large numbers that takes o of n time right o of n of if you have a size of n else take o of n because you have to go through that many number of numbers right okay so what is a one solution you could do you could create a prefix sum so if I were to create a prefix sum called t t is my prefix sum I'm going to start with the numbers five and I'm going to keep adding by the current number and then that'll be my total sum at the current index so five plus two of the seven seven plus two is sixteen sixteen plus negative three thirteen thirteen plus five is eighteen eighteen plus twenty thirty eight and that is our prefix sum so now if you ask me how what if I want to sum up the numbers from two to five all I have to do because I have the sum at from zero to five which is thirty eight I just have to take this sum right so we have the sum from zero to five our prefix sum at five thirty eight subtract it from the sum at zero and one and then we would have our remainder sum from two three four five so that would be the prefix sum of t of five minus t of one and that would be thirty eight minus uh yeah thirty eight minus seven which is going to give us thirty eight minus seven is going to give us thirty one and that's the same thing as if you sum from two to five so if you want to sum using prefix sum you would from if you want to sum prefix sum from i to j right it would just be like i'll be t of j the sum of t of j minus t of i minus one right but what if I want to add something like what if I updated this array you need to become I don't know from negative three I added one right now then I have to regenerate this prefix sum over and over again and that's not good so what is a way you could avoid doing this like uh regenerating over and over again to avoid that uh what you could do is you could just split your prefix sum in half right and then restart another prefix sum on the other half of the array so if I have like five two nine and negative three five twenty I'm going to restart my prefix sum at three four and five so I would have my first prefix sum t one five seven sixteen and then my next prefix sum is going to be negative three plus five which would be two plus twenty which would be twenty two and what this does is that now whatever I update on t one I don't have to update at t two so then now if I update t one value I don't have to update t two and then that would save us time from adding and subtracting the only issue here now is that summing between the prefix sums would be much of more hassle so like if I want to sum from one to three right if I have to sum from one to three I would have to basically do a t two a t one of two right let's take the 16 right and then subtract from t one to zero which would give me um 11 right that'll give me 16 minus five would be 11 and then I would have to add that with the first value of my first prefix sum so I had to add this 11 plus t two of the first value which would be zero and that would give us 11 minus eight three would be give us eight right so that would give us eight and that's that's what this does to that would be the sum from one two and three right if I'm adding sums from one two and three between the two prefix sums that would be more hassle but it would help us saving our updates and that's basically what finwick trees do we are going to split the array into multiple halves over and over again and then that'll be the sums from those halves okay so I'm going to draw that out all right guys so now what I did was I just redrew the array vertically from scratch and then I added more values so like I had like 10 negative seven two three and then I also showed the indexes the current indexes which is one two three four five six seven eight nine ten and those are just the indexes of the array for each number right I'm indexing from one because it's more easy to understand this way so what I also did was I also wrote the equivalent binary of the corresponding index so for zero zero zero one that's the the binary representation of the index one for zero zero one zero that's a binary representation of index two for zero zero one one that's a binary representation index three for zero one zero zero that's a binary index uh binary representation index four for zero one zero one that's a binary representation of index five for zero one one one uh zero one one zero that's a binary representation of index six zero one one one seven one zero zero zero one zero zero one one zero one zero okay so now how do we create the finwick tree well first of all we're going to look at each bit that is set so from the right most bit, we see one is set, right? This, this bit is set as one. And we're going to go downwards to see all the corresponding ones that are set. So we have this one is set this one set, this one set, this one set, this one set. And those are the corresponding ones that are set. Maybe I should change your brush. Hold up. It's better. Yeah, this is better. So these are the ones that are currently set. And we're what we're going to do is we're just going to write the corresponding odd number, not not odd number, the corresponding array value for those of these values that have the first bit set. So this one has the first bit set, so we're going to set that as five. So our 253 has a bit set, so that'll be nine. So we're just going to set that as nine. Five has this bit set. So we're going to set that five is equal to five. Seven has this bit set. So we're going to set our value to have the same array as the original values that will be 10. Nine has this bit set. So we're going to have the original value of two. Okay, so these, as you can see, half of the values are the exact same values as this and they are all odd, right? The first values are all odd. So half of them are going to be the exact same values as your original array. So once we're done looking at all the first bit set, we're going to look at the second bit now. So for every second bit, we're going to add to the first corresponding bit. So like let's say this the second bit, this bit is set, right, this bit from this column down. So you see this one, this one, this one, this one, these are all sets set have the bit set. So we're going to add it corresponding to the first value. So now two plus five is going to be seven. So we put seven here. Okay, so now let's look at another one. For it does not have this bit sets, we're skipping that six, six does have this bit set. So we're going to add the corresponding value of five and six at index five and index six. So that's 20 plus five, which is 25. So we're going to put that here. Okay, so now we have 25 there. Now we're going to look at the last one. This one 10 has this bit set, and that's value three. So we're going to add three plus two, which is five. See, so now now we have those bit sets. So right now we have all the bits that are first column that are set, we wrote the original value, the second column, we just summed up the first two. Alright, now let's let's look at the third column. So now we have four, this bit is set. So we are we're actually going to add all the values from one 123 to four. So that's five plus two, five plus two, plus nine, plus negative three. And that's seven plus nine, 16 minus three is 13. So this is 13. So that's T of four. So this is going to be 13. And let's look at another corresponding bit that's set. That's it actually. Third bit that are set. Yeah, that's it. We're not going to look at these values, I think for now. For now, let's not look at seventh bit. Yeah. Let's look at the last bit. So last bit that is set, all these values, eight, nine and 10. So let's look at eight. So for eight, we are going to because this bit is set, we're going to add all the values before eight. So we're going to do like a five plus two, plus nine, plus negative three, plus five, plus 20, plus 10, whoops, plus 10, five, 20, 10, negative seven. And then let's use a calculator for this, because I don't want to compute this by hand. So five plus two, plus nine, plus negative three, plus five, plus 20, plus 10, plus negative seven. That's 41. And yeah, that's 41. So we put that 41 down here. And that's basically our tree. For now. And if you want to check your tree evaluation, you could use this online tool that you could put a visual go.net finwick tree. What this does is that it actually creates shows you the representation of the finwick tree. So if you look at this, you would have I inputted the values, let's actually input the values. So our values were five, two, nine, negative three, five, 20, 10, negative seven, two, three. So we do go. It actually creates the finwick tree and then you could check your values. So let's see. Okay, so based on this, you see that we have five, seven, nine, 13, five, 25, 10, 41, two, five. And let's look at our answers. So we have five, seven, nine. So that's right, 13, five, 25. Do they have 10? Let's see if they have 10, they do have 10, 10, 41, two and five. So yeah, it is the exact same answer as their finwick tree representation. So that's how you create your finwick tree. All right, guys. So let's say we want to sum up the values from one to seven now. So like, let's say I want to find the sum of seven from one to seven, right? Well, let's see what we would do normally. So normally you would just add up all the values from one to the index one, two, three, five, six, seven. And let's actually compute that. So just from summing up, summing it up normally, the answer would be 48. This is just summing it up normally and go through it. But as you can see here, you don't actually have to sum up the values normally like this. So like if I want to find the sum of one to seven, all I have to do actually is literally just do take the sixth. So like, look, if I do T of seven, plus T of six, plus T of, let's see, four. Yeah, T of four. So this would be 10 plus 25 plus four, which would be 13. And this would give us 10 plus 25 plus 13. That'll give us 48. Right. So how did I get these numbers? Well, you don't actually have to actually compute least like sum it up from one to three or five, six, seven. All you have to do is just go through each bit that is in your current number that's set, and then unset the bit and then add the value that's unset to the current value that you have in your array. So like look to your current value in your answer. Okay, so let's say I want to do sum of seven. So sum of seven, which is one to seven. So the binary equivalent of this is 0111. So this is going to be some of 0111. And basically, I'm going to take this bit and set it to zero. So then I'm going to do what I'm going to do is I'm going to add it with sum of 0110. And now this bit is set to zero. Then I'm going to take this bit and set this bit to zero. So I took the first bit and set a zero second one take second bit set to zero. So I'm going to that's going to add some of 0100. Right. Then I'm going to take the third bit and set this to equal zero. So that's going to be plus some of 0000. And that's just zero. So then this is going to give us some 011. 011 is 10. So this is going to have 10 plus 25 or 0100. What's 0100, 0100, 0110. That's 25 10 plus 25. And then plus 10 plus 25 plus let me just move this up real quick. Yeah. Let me see. Can I minimize this? I think I could. Yeah, I can minimize it. Yeah. Yeah. So now that gives us 10 plus 25. And then set this bit as a 0100. 0100 is four. And T of this was 13. So we're going to plus 13. And that's going to give us 48 in the end, which is the exact same number as our original number. So yeah, that's how you would sum up numbers for that. In a Feynman tree. So if you want to basically sum up these numbers, the code would actually wouldn't be that difficult. It's just you end it by you flip the last bit, and then you end it. So that would set the last bit to maybe I'll type the code out later. How you would do that. But yeah, that would be it. So now let's talk about updating. All right, guys. So basically, I just redrew the whole array and the corresponding finitary horizontally now, because I need more space to draw. So basically, now let's say we want to add 10 to add index four. So let's say I want to add 10 at index four. So at index four, I'm going to add 10. So this is like updating it. So what would I do? Well, basically, you do the same thing. Instead of adding, instead of removing the least significant bit, you add a bit in the beginning every time. And until you reach the end of your length of your array, then you stop adding it. So basically, that's how you propagate through your finwick tree. So let's say I want to convert. Let's I want to add 10 to add index four, right? So what that would do is that first, let's draw out the binary representation of index four. So this is this is index four, right? Four. So the binary representation index four zero one zero zero, right? And this is what we're going to do is we're going to take this value, T of this at index four, and then we're going to add by 10. Okay, so whatever at T at index four, we're going to add it by 10. So this is going to be 23. Right. And our original value at index four, we're adding it by 10. So this is going to be seven. I don't know how I draw it, make this become seven. Yeah. Now this is seven. Okay, so negative three is now become seven because we're adding by 10. So 10 plus negative three is seven. And T at four, we can have it now becomes 23 because we added added 10. Right. So yeah, T of four is 13. And then we plus 10, which gives us 23. So now what we're going to do is we are going to set the bit at the far left. So we're going to keep setting the bits at the far left and then add those values by 10 also. So originally, this bit is set at four. So now we're going to set this bit, set this bit. So that's T of one, zero, zero, zero. And we're going to add 10 to this. So T of one, zero, zero is now eight. Right. It's eight. So we're going to add 10 to this. So T at eight is 41. So we're going to add 10. So 41. This is at 10 is now 51. So we do 41 plus 10 is 51. And this now becomes 51. And now what we're going to do is we're going to set the bit on the left side again. So now we have T. So originally now it's this one, this was set. Now we're going to add another one. So now we're going to have one and add four zeros now to the end. So one, two, three, four. Right, rad another one. Yeah. So this we're going to add 10 to this one. And this is actually zero one, two, three, four. This is actually 16. So this is the value of 16. So because T of 16 doesn't exist, it's greater than our original array of T. We're done. Okay, so now we are finished. Finished because T of 16 is greater than our array. Right. I should be 16. 16 is greater than our array. 16 is greater than our array. Then we're stopped. So yeah, that's basically how you would update values in your finwork tree by adding values like 10 and stuff like that. So yeah, this is a pretty basic overview of how finwork trees work. I might create another another tutorial about finwork trees actually that is more in depth and I might actually draw out the tree itself. But this is a quick tutorial. I hope you guys enjoy this video. Rate, comment, subscribe. I'll check you guys later. Peace.