Fenwick Tree or Binary Indexed Tree

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024

Комментарии • 286

  • @CSSFACE
    @CSSFACE 2 года назад +32

    To get the parent of n, you can also find it by calculating n & (n-1) which is a little simpler than the 3 step process described in the video (2s complement, AND with original, then subtract from original)

    • @Marzex1x
      @Marzex1x 4 месяца назад

      it is simpler but at the end of the day they're both 1 liners

    • @SergioS-ji1hv
      @SergioS-ji1hv 4 месяца назад

      ​@@Marzex1xbeing a one liner literally doesn't mean anything, use the one that's widely better understood

    • @Marzex1x
      @Marzex1x 4 месяца назад

      @@SergioS-ji1hv doesn't really matter at the end of the day it comes down to personal preference

  • @jyotikumari-bl2kf
    @jyotikumari-bl2kf 8 лет назад +400

    @Tushar Hey Tushar, it will be good if it is mentioned why 5 is expressed as 2^2 + 2^0 and not 2^0 + 2^2. The idea here is to go to the parent and then next x number of elements. I got confused when i saw the video. Then later i figured it out that it is expressed as parent index + some power of 2.

    • @saikatsarkar858
      @saikatsarkar858 8 лет назад +13

      Precisely,I was wondering about the exact same thing until i came across your comment.Thanks a ton! :)

    • @jitendranagar8962
      @jitendranagar8962 7 лет назад +1

      initially confused ....how updating a index i to index j some time 2^x + 0 and some time 0+2^x.....But you made it clear thanx

    • @kartikchauhan5498
      @kartikchauhan5498 6 лет назад

      You need to go up.

    • @SergeyGrebenkin
      @SergeyGrebenkin 6 лет назад

      I was also surprised unless I read your comment

    • @manhxxo
      @manhxxo 6 лет назад +4

      In this case, 2^2 will be the dad node and 2^0 is the rightmost bit 1. For example: 11 = 1011= 2^3 + 2^1 + 2^0 which is 2^3 + 2^1 will be its dad node(1010 = 10) and 2^0 is the value of rightmost bit 1 of the node (0001).

  • @amazingseries
    @amazingseries 8 лет назад +70

    Nice and short videos. Another simple way to obtain the parent of index x is x & (x -1)

    • @Hetp111
      @Hetp111 5 лет назад +1

      Yes, I was thinking the same...! More info: www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/

    • @PrateekRathore
      @PrateekRathore 5 лет назад +2

      Is the any cool trick to find the next number in fenwick tree ??

    • @stablesort
      @stablesort 4 года назад +3

      Well said. A more detailed explanation could be found in another tutorial: ruclips.net/video/uSFzHCZ4E-8/видео.html

    • @ishanraj5961
      @ishanraj5961 4 года назад +1

      @@PrateekRathore the next of i will be i+ (i&(-i) ).

    • @tarunkumar2269
      @tarunkumar2269 4 года назад +2

      @@ishanraj5961 that is what tushar said

  • @mysterymidas8574
    @mysterymidas8574 6 лет назад +6

    One of the best explanations I've ever come across for Fenwick Tree!

  • @AmanGarg95
    @AmanGarg95 9 лет назад +19

    Its nice that you actually focus on analysis and also point out the petty things that form the basis of the code eg. the next hop and the examples.
    This is in contrast to other algo preachers who just mutter out the process without specifications as if it were imminent. Thanks, and please don't stop making videos.

  • @lavanyam3224
    @lavanyam3224 19 дней назад

    After watching a couple of BIT explanations from some good channels, I lost the hope of COMPLETELY understanding it. But you made it so easy and clear in 20min!! Legend you are! ❤

  • @krokenstiv8777
    @krokenstiv8777 Год назад

    finally i found a real expert on Fenwick Tree, I actually implemented it in Java using this lecture. Thank you very much!

  • @juliettetworsey3060
    @juliettetworsey3060 2 года назад +2

    Thank you so much for this and thank you for breaking everything down to a level that makes it easy to understand a binary indexed tree. Awesome explanation!

  • @jinyang4796
    @jinyang4796 2 года назад +1

    This video is just too good! Thank you so much!!!!!!

  • @farhadpagdiwala1789
    @farhadpagdiwala1789 2 года назад

    Very good explanation on the fenwick tree concept and how to create and update the tree.

  • @eryash15
    @eryash15 6 лет назад

    Perfect brother, if one wants to learns can learn from this lecture or cannot learn at all..... All concept perfectly covered in 20 min.

  • @FinanceStoryTime
    @FinanceStoryTime 6 лет назад

    One of the best explanations on Fenwick Tree on the internet. Well worth the 22 mins I spent on watching.

  • @ayushjindal4981
    @ayushjindal4981 3 года назад

    For filling up the tree, we can create a cummulative sum array and using that we can fill the nodes...it will take O(n) time to fill the entire tree.

  • @anatoliistepaniuk8217
    @anatoliistepaniuk8217 7 лет назад +5

    Wow, This is a really useful content! Thank you for the value you create!

  • @shubhamkapoor6954
    @shubhamkapoor6954 4 года назад +1

    we can find parent also by first subtracting number by 1 and then do bitwise and of this number with original number
    eg :- 10 is 1010 subtract 1
    1001 now do bitwise and it with 10
    1010 & 1001 = 1000(8), which is the result.

  • @VaibhavRaheja
    @VaibhavRaheja 7 лет назад

    Only some people have the talent to explain such complex concepts in such an easy manner. And Tushar is one of them.

  • @ihtemad
    @ihtemad 4 года назад +6

    you can get parent in a bit easier way:
    Parent = n&(n-1)

  • @muditkhandelwal2008
    @muditkhandelwal2008 6 месяцев назад

    @Tushar you should also explain how to get range sum for lower bound > 0. Eg. RangeSum(2, 6)
    Here is the snippets for finding range sum:
    int findSum(int low, int high) {
    if (low > high) throw new Error("Invalid range");
    int index = high+1;
    int upperBoundSum = 0;
    while (index > 0) {
    upperBoundSum += tree[index];
    index = getParent(index);
    }
    int lowerBoundSum = 0;
    if (low != high) {
    index = low;
    while (index > 0) {
    lowerBoundSum += tree[index];
    index = getParent(index);
    }
    }
    return upperBoundSum - lowerBoundSum;
    }

  • @cjoe1909
    @cjoe1909 2 года назад

    Hi, thanks very much for the instruction. I am now fully understand the algorithm and can implement myself during interview.

  • @NextyTV
    @NextyTV 9 лет назад +1

    That's your second video that I watched and it was very helpful. Just came here to say thank you.

  • @aisakyunhotahai8130
    @aisakyunhotahai8130 3 года назад +6

    Never seen him smiling, I guess he is Robot

  • @amanpreetsingh8628
    @amanpreetsingh8628 8 лет назад

    I have heard a lot that this topic is very tough but you really made it easy for me . Thanks a lot.

  • @madhukiranattivilli2321
    @madhukiranattivilli2321 3 года назад

    FT creation takes linear time but not linearithmic time.
    This is the way to do in linear time :
    -- fill original array values at indices [0, n-1] into FT array indices [1, n]. FT is created of size n+1, and FT[0] is n/a
    -- Propagate values up in a cascading approach. 1 is 0001. 1+lsb(1)=1+1=2 (lsb: least significant bit, obtained as x & - x). So, 2 is responsible for 1. In other words, 2 stores the prefix sum of 2 array indices -- itself and 1 below. How? 2 is 0010. lsb(2)=2. lsb(x) represents the prefix sum of how many array indices it is responsible for. So, add FT[1] to FT[2]. Next, handle 2. 2+lsb(2)=2+2=4. Add FT[2] to FT[4]. 3+lsb(3)=3+1=4. Add FT[3] to FT[4]. 4+lsb(4)=4+4=8. Add FT[4] to FT[8].
    -- Thus we don't propagate a number all over the FT in O(log2(n)) time. Instead we propagate only to the immediate higher number who is responsible for current number (example: 4 is 8's responsibility. 5 is 6's responsibility, etc)
    -- Thus we construct the FT from i/p array in O(n) linear time

  • @user-xs3ky8gy5s
    @user-xs3ky8gy5s 3 года назад

    Excellent.... Excellent....Excellent ...Thank you very much

  • @NehhalKalnadTheGreat41
    @NehhalKalnadTheGreat41 5 лет назад

    Good expln.
    those who are still confused, please read some article and then again refer the vdo

  • @idiot7leon
    @idiot7leon 4 года назад

    Thanks, @Tushar. You don't know how great&helpful your videos&tutorials are to me!

  • @jineshdhruv6151
    @jineshdhruv6151 6 лет назад

    Because of your videos, my Data Structures concepts are getting clear and strong...... Thank you very much... please also post videos about how to apply data structure logic for any given problem. I don't know about others but sometimes I fail to understand which data structure I should use to solve a problem.

  • @mohit2072
    @mohit2072 7 лет назад

    You amazing man... When I saw some hackerearth notes..i just ignored it coz it was quite long and reading becomes useless..and here i saw ur video and i dun knw how just time passed and finally understood it...thnx and will go thru every video of urs coz these are the hottest topics in competitive coding world

  • @Ronakrktanna
    @Ronakrktanna 8 лет назад +3

    Thank you so much for an amazing explanation of a complex data structure. :)

  • @tahanimachowdhury
    @tahanimachowdhury 8 лет назад +2

    You are awesome as usual. I was struggling with Fenwick Tree for a long time and now it is clear. Thank You for your efforts. :) ^_^

  • @psurya3053
    @psurya3053 Год назад

    gotcha bro , u are the best one on the youtube , there are tons of creators who tried to explain same topic , i couldnt understand , u are the best.
    thank u so much

  • @pritamchakraborty7163
    @pritamchakraborty7163 9 лет назад +9

    In order to do a range query [x,y], should we need to get prefix sum [0,y] and [0,x) and subtract them?

    • @hackermub2598
      @hackermub2598 5 лет назад +3

      well yes.. because BIT is just a prefix sum that can also be updated

    • @caret4812
      @caret4812 5 лет назад +3

      why asking, you already know the answer lmao

  • @mr6462
    @mr6462 3 года назад

    Thanks for the visualization and the explanation! I think Fenwick Tree is beautiful :)

  • @catherinewang1191
    @catherinewang1191 7 лет назад

    Big fan of your video blogs.. Best explanation ever.

  • @vineetsharma7287
    @vineetsharma7287 3 года назад

    One of the best videos on Fenwick Tree!!!

  • @prasenjitmondal673
    @prasenjitmondal673 9 лет назад +1

    It would be great if some problems can be solved using BIT from competitive programming challenge.

    • @charvakpatel962
      @charvakpatel962 8 лет назад

      +Prasenjit Mondal
      codeforces.com/problemset/problem/597/C
      have fun

  • @koyalkardevanshu5146
    @koyalkardevanshu5146 8 лет назад +4

    Thank you for putting this up. Aap infi machate ho.......infi respect

  • @lakshayvirmani5877
    @lakshayvirmani5877 8 лет назад +2

    Best Explanation. Simply Amazing. Thanks a lot! :D

  • @mohit2072
    @mohit2072 7 лет назад +1

    Also for range other than 0,y let say we need to get range from x,y then just we gotta do is (0,y)-(0,x-1) :p I hope many may thought abt it so likhdiya ;p

  • @helinw
    @helinw 6 лет назад +1

    Thanks for the video, would be nice if you could explain more about how was the rules you mentioned generated, it helps memorizing the rule.

  • @ayushsingh7987
    @ayushsingh7987 2 года назад

    Thank you sir it is very helpful video

  • @SmileyMPV
    @SmileyMPV 7 лет назад +1

    I'm certain you can create the tree in O(n) if you use a faster algorithm. I think I found a way to calculate the values one by one where they take O(1) time amortized.

    • @gamingbutnotreally6077
      @gamingbutnotreally6077 Год назад

      yes creating the initial tree can be done in O(n) this is quite well known.

  • @xinranwan9390
    @xinranwan9390 2 года назад

    Just for heads-up, 15:04 he means add 1, not 2, to the original number.

  • @amandeepsingh1730
    @amandeepsingh1730 3 года назад

    Amazing! You just remove my fear about fenwick tree.
    Thanks

  • @parthamishra09
    @parthamishra09 3 года назад

    lovely stuff. thanks for explaining it so patiently. much appreciated.

  • @prudvim3513
    @prudvim3513 8 лет назад

    Thank you very much for such a nice and lucid explanation.!!!

  • @dluxhu
    @dluxhu Год назад

    This is technically not a binary tree, because one node can have multiple child nodes. But other than that, it's smart. Thanks for the explanation!

  • @manthansheth880
    @manthansheth880 8 лет назад +1

    Awesome work! Thanks a lot and keep up the good work.

  • @rockstar86374
    @rockstar86374 4 года назад

    what I figured out from the above video is.
    if you want to find the sum of the ith element, then go to its parent and from there take the smallest 2^power element present in I and take the sum of that much element.
    For example ,{ () represent power}
    for 1 = 2(0) parent is 0 take 2(0) elements from there that is (0,0)
    for 2 = 2(1) parent is 0 take 2(1) elements from there that is (0,1)
    for 3 = 2(1)+2(0) parent is 2 take 2(0) elements from there that is (2,2)
    for 4 = 2(2) parent is 0 take 2(2) elements from there that is (0,3)
    for 5 = 2(2)+2(0) parent is 4 take 2(0) elements from there that is (4,4)
    for 6 = 2(2)+2(1) parent is 4 take 2(1) elements from there that is (4,5)

  • @taruneemeesala8052
    @taruneemeesala8052 4 года назад

    Get next element explanation is intuitive. If current element is represented as parent index + next x elements (as power of 2), we need to increase the range of next x element by power of 2. Example: 6 = 2^2+2^1 then next element=2^2+2^2=8

  • @freezefrancis
    @freezefrancis 8 лет назад +3

    Great work Tushar :)

  • @KshitijJollyGuitar
    @KshitijJollyGuitar 8 лет назад +1

    Nicely explained. Thank you :)

  • @readogamer3515
    @readogamer3515 4 года назад

    Great explanation

  • @AnkitKumar-zu7cn
    @AnkitKumar-zu7cn 5 лет назад

    Very clear explanation! Thanks for pulling this up!

  • @Apptica
    @Apptica 9 лет назад

    It is the best explanation i think....every information is so clear and simply explained.....Do you have any tutorial regarding how to perform range updates in fenwick tree

    • @Apptica
      @Apptica 9 лет назад +1

      ***** Yes updating all elements in given range

  • @AlayDhagia
    @AlayDhagia 3 года назад +1

    you sir are a legend

  • @SonuSonu-tk5pk
    @SonuSonu-tk5pk 6 лет назад +1

    awesome explanation sir ji ...
    u r great ...

  • @RohanLokeshSharma
    @RohanLokeshSharma 8 лет назад +2

    one of the best explanations of Fenwick Tree out there!
    Great work,

  • @atishayjain5912
    @atishayjain5912 5 лет назад +1

    Why would a segment tree take 4n space in the worst case? A tree with n nodes, has, n/2 leaf nodes, so shouldn't the worst case be 2n?

  • @shreyasshetty5051
    @shreyasshetty5051 4 года назад +1

    Thanks a lot sir

  • @pengjin7912
    @pengjin7912 8 лет назад

    Thank you so much for this work! It really helped me to understand the BIT!

  • @prasenjitmondal673
    @prasenjitmondal673 9 лет назад

    Very Nice Explanation.

  • @hassanramadan2611
    @hassanramadan2611 3 года назад

    Thank You for this amazing tutorial!

  • @arijit48
    @arijit48 7 лет назад

    Thanks, Tushar ... Your videos r well-presented and a great help :) !!!!

  • @talalal-hamidi3093
    @talalal-hamidi3093 9 лет назад

    Thank you for this great job.
    Keep going Helped me so much

  • @themohameDkhalifa
    @themohameDkhalifa 9 лет назад +3

    in filling up the tree, why didn't you say that 3 = 0 + 2^1 + 2^0 and start from index 0 just like you did in 2?

    • @bhavukmathur2709
      @bhavukmathur2709 8 лет назад

      What I figured out is that for all the powers of 2 (2,4,8), he started from 0. Else he started in the descending order of powers of two. Just a rough guess.

  • @arhankundu2516
    @arhankundu2516 4 года назад

    Thanks for the awesome explanation!

  • @nafizbasaran3639
    @nafizbasaran3639 День назад

    Basic formula for get next : next(i) = 2*i - (i & (i-1))

  • @mudithead
    @mudithead 8 лет назад +1

    Great video again. What are your thoughts on using Binary Indexed trees vs segment trees? I can use BIT for solving all problems which segment trees can? I find segment trees hard to implement in interviews..

  • @halseywalker5015
    @halseywalker5015 2 месяца назад

    Thank you ,master

  • @kidou123456
    @kidou123456 6 лет назад

    Thank you very much, Tushar. This helps a ton!

  • @hanaagebril9795
    @hanaagebril9795 3 года назад +1

    I dont understand the part of index value evaluation, based on what? I mean the evaluation of each index in the fenwick tree for 1, 2, 3, .... and so on you started with 0 with some and 2 with others. also I can see that as an example the index 6 may have more than representation , the one you mentioned 3 = 2ofpower1+2ofpower0 not started by 0 as the 1 and 2, please can you make it clear more to me?

    • @hanaagebril9795
      @hanaagebril9795 3 года назад +1

      I got it. It depends on the binary bits count representation of the number in other words the number 4 represents by 100 in binary which has one active bit at position 3 with power 2 which equal 2 of power 2 then its value will start by 0 followed by the 4[2 of power 2] and for number 6 it represents by 110 which has more than one active bit at positions 2 and 3 or power of 1 and 2 respectively which equals to [2 of power 2] + [2 of power 1] and need to start from 0 for that number.

  • @akshayanm
    @akshayanm 2 месяца назад

    In the video, you said the TC of building the BIT is O(nlogn). If we use a prefix sum array just for making the tree, we can reduce it to O(n) right?

  • @jamesqiu6715
    @jamesqiu6715 2 года назад

    convoluted explanation !

  • @al-aminhossain9534
    @al-aminhossain9534 7 лет назад

    Realy It was very helpful to me. Thanks boss.

  • @rajatjain409
    @rajatjain409 8 лет назад

    superb explanation in a much simple way. :)

  • @shishirmohire9789
    @shishirmohire9789 6 лет назад

    I learnt lot of things from you man thanks.

  • @rahulshetty3738
    @rahulshetty3738 7 лет назад +6

    I think getParent for index i can be simply i&(i-1)..

    • @goutkannan
      @goutkannan 7 лет назад +1

      I was thinking the same , CTCI had a question involving this i guess.

    • @manojpandey1855
      @manojpandey1855 5 лет назад

      Correct.. that would be much simpler.

  • @theultratraveller2024
    @theultratraveller2024 9 лет назад +5

    b'coz of ur explanations..... i'm able to code way better for competitve prog......thankx!

  • @victormarciliopeixoto
    @victormarciliopeixoto 9 лет назад +1

    wow! awesome, very didactic!

  • @ayushsethi1085
    @ayushsethi1085 9 лет назад

    heyy!! i must say that you are doing a great job!! i just wanted to confirm that if the number is a power of two then we take the summation from starting(ie index 0) to the number and in rest of the cases we are dividing the number in the decreasing power of 2s ?

  • @user-pv1pb3tt3x
    @user-pv1pb3tt3x 7 лет назад

    great and clear explanation, thanks!

  • @adhoc3018
    @adhoc3018 3 года назад

    You are an inspiration.

  • @themagicianadamagic7830
    @themagicianadamagic7830 8 лет назад

    It was really useful. (If you have time and patience could you upload the efficient way of filling the tree up. I am really intrested in that.)

  • @gdargdar91
    @gdargdar91 2 года назад

    Better ways:
    1. To get the parent of i: binary AND i with (i - 1).
    2. To get next of i: binary OR i with (i - 1) and add 1 to it.

  • @174ashish
    @174ashish 8 лет назад

    great explanation sir !!

  • @risureviewcompany
    @risureviewcompany 9 лет назад +1

    sir, wotever u r doing is awesome .. respect !! you always save my time with better explaination.. :)

  • @Onewaystyx
    @Onewaystyx 6 лет назад

    explained completely fine. Stupid people gonna be stupid. You have to think for yourselves a little people.

  • @Marzex1x
    @Marzex1x 4 месяца назад

    much love brother

  • @CodingIsEasy372
    @CodingIsEasy372 8 лет назад

    Excellent tutorial!! Will u please make a video on fenwick tree with range updates and range queries.

  • @yusufawad6779
    @yusufawad6779 8 лет назад +2

    For sure you should go to MIT or any great place your are one of the best

  • @balajicherukuri
    @balajicherukuri 8 лет назад +1

    /**
    * To get parent
    * 1) 2's complement to get minus of index
    * 2) AND this with index
    * 3) Subtract that from index
    */
    private int getParent(int index){
    return index - (index & -index);
    }
    /**
    * To get next
    * 1) 2's complement of get minus of index
    * 2) AND this with index
    * 3) Add it to index
    */
    private int getNext(int index){
    return index + (index & -index);
    }
    Can you explain reason behind this? I can see it works but dont get why?
    BTW you are awesome.

    • @varunsinghania3683
      @varunsinghania3683 8 лет назад

      Refer my ans to @Chris Z above :)

    • @sohrab1987
      @sohrab1987 8 лет назад

      to get the rightmost bit flipped, just do x=(n&n-1)
      e.g. :
      7-> 7&6 = 6
      9-> 9&8 = 8

  • @achboldjugdersuren6883
    @achboldjugdersuren6883 4 года назад

    I signed into my account to say thanks.

  • @Venkat2811
    @Venkat2811 9 лет назад

    Sorry previously I was wrong so i removed it to avoid confusion.
    while updating only difference should be passed as val to that method.

  • @gaoyansong5428
    @gaoyansong5428 8 лет назад +1

    very clear! thank you !

  • @partheshsoni6421
    @partheshsoni6421 4 года назад +1

    Some Indian guy on RUclips! 😎

  • @nihalsrivastava
    @nihalsrivastava 8 лет назад

    Hey, Nice work, although most efficient way to find parent is N AND N-1... Correct me if i am wrong.

  • @nhan1503
    @nhan1503 8 лет назад

    Thank you so much. Keep up the good works :)

  • @mayurkulkarni755
    @mayurkulkarni755 8 лет назад

    Excellent tutorial!! Keep it up bro

  • @user-pk6gk6yg6c
    @user-pk6gk6yg6c Год назад

    this is the best video