Fenwick tree point updates

Easy to Advanced Data Structures Fenwick tree/Binary indexed tree
4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

I want to talk about Fenwick trees and point updates. So let's dive right in. But before we get to that, absolutely, make sure you checked out the Fenwick tree range query video that I posted last, just to get the context of how the Fenwick tree is set up and how we're doing operations on it. Okay, so just to refresh your brain on how we actually did a prefix sum, and how we did those range queries. So what we did was, we started at a value at some index doesn't matter which and then we continuously removed the least significant bit until we hit zero. That's a cascading down effect that gave.

So let's say we started at 13 or 13, the least significant bit was one, so we removed one, and we got 12. And then we found out that the least nificant bit of 12 was four. So we removed four and then the least significant of eight was eight, then we reach zero. And once we reach zero, we know that we're done. So doing a point at date is very analogous to this. Instead of removing, we're going to be adding the least significant bit as you'll see.

So for instance, if we want to add a value at index nine, then we have to find out all the cells which are responsible for nine because remember, cells are responsible for a range of responsibility. So if we start with nine, we find the least significant impact 910 so 10 finally significant bit, is has a value of two, then we add it to 10 then find the least significant bit 12 at 12. So it's for now 16. And then we would do the same thing, and then we're out of bounds. So we will know to stop. So if I draw a line outwards from nine, all the cells that I hit are the ones I have to update.

So remember, those lines represent a range of responsibility. So the lines that I hit are the ones that I got when I added the least significant bits. Okay. So for to add some constant x at position six in the Fenwick tree. Which cells do I need to modify? So we start sex, and then we find the least significant bit six and add sex.

So get eight, find the least significant bit of eight and add 816. So if I draw a line out from six, then indeed the cells that I do hit are six, eight and 16. So the required updates for our Fenwick tree are that we need to add x to position six, eight and 16. So the algorithm is really really simple. So if we have a Fenwick tree, stored in an array of size n, then while I, so is the position we originally want to update, so i is less than n. We're going to add x to the tree at position I and add on The value of the least significant bit of AI. And that's it where the function LSB is the least significant bit of AI.

And there are built in functions to do that usually. And we're going to have a look at some source code. Alright, so that was point updates. And now we actually need to construct the Fenwick tree. And we're going to do that in the next video. So guys, if you learned something, please hit the like button and share this video.

And that's all for now. I'll catch you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.