Fenwick Tree construction

WilliamFiset · Intermediate ·⚡ Algorithms & Data Structures ·9y ago

Key Takeaways

The video discusses the construction of a Fenwick tree, a data structure used for efficient range queries and point updates, and presents a linear time complexity algorithm for building the tree.

Full Transcript

all right welcome back everyone let's talk about Fenwick tree construction we've already seen how to do range queries and point updates in the last two videos but we haven't even seen how to construct the Fenwick tree yet and the reason I've kept this for last is you can't understand the Fenwick tree construction without having previously understood how uh Point updates work all right so let's dive right in so we could do the naive construction of a Fenwick tree so if we're given an array of values a and we want to transform this into a Fenwick tree what we could do is initialize our Fenwick tree to be an array containing all zeros and add the values into the Fenwick tree one at a time using Point updates to get a total time complexity of order n log n however we can do better we can actually do this in linear time so why bother with n log n all right so in the linear construction we're going to be given an array of values we wish to convert into the Fenwick tree a legitimate Fenwick tree not just uh the array of values themselves and the idea is we're going to propagate the values through throughout our Fenwick Tree in place and we're going to do this by updating the immediate cell that is responsible for us eventually as we passed through the entire tree everyone's going to be updated and we're going to have a fully functional Fen tree at the end of the day so it it kind of relies on this cascading idea so you you propagate some to the parent who's responsible for you and then that parent propagate its value to its parent and so on so it's just kind of like almost delegating the value so let's see how this works oh one more thing before that so if the current position is position I then the immediate cell above us which is responsible for us so our parent let's say that is J and J is given by I plus the least significant bit of I all right so if we start at one well the least significant bit of uh one is one so the parent is at position two so notice that there was a four at position two but we're going to add to four the value at I which is three so now position two has a value of seven now we want a b position two so find out which is responsible for position two so two plus the least significant bit of two is four so four is actually responsible for two or is immediately responsible for two so go to index 4 and add the seven then who's responsible for three well that's four so go to position four and add the value at index three now who's responsible for four well eight is responsible for four so go to position 8 and add the value of four so now in position 8 we have four so now we're at five and then you see how we keep uh doing this just updating our parent the immediate cell responsible for us so now seven is updating eight but now nobody well eight doesn't have a parent because our Fen tree is too small it only has 12 cells but the parent that would be responsible for eight is 16 and 16 is out of bounds so we just ignore it it's not relevant so now we keep going so I is nine 9's least significant bid is one so J is 10 that's where the parent is so keep propagating that value 10's parent is 12 11's parent also 12 and now we have the same sort of situation we had with eight where we have an out of bound situation so we ignore it so the values that are there right now are the values of the Fenwick tree and with these values we can do range queries and point updates not with the original array that we had so let's look at the construction algorithm itself in case you want to implement this we will have a look at some source code in the next video but if you're using another language that I'm not using this could be helpful so given an array of values we want to turn into a Fenwick tree let's get its Val the length which is n and I recommend you actually clone or make a deep copy of the values just so that you don't accidentally manipulate the values array while you're constructing your Fenwick tree um that could be problematic because we're doing all this stuff in place so clone the values array and then start I at one and go up to n and then compute J so the parent so which is I plus the least significant bit of I do an if statement to check if J is less than n that might actually be less than or equal to n actually now I'm thinking about it because everything is one based in in a Fen tree yeah I'm pretty sure that should be less than or equal to n all right so in the next video we'll be going over some source code so guys stay tuned for that and I hope you learn something hit the like button subscribe and I'll catch you in the next video

Original Description

Related Videos: Fenwick tree range queries: https://www.youtube.com/watch?v=RgITNht_f4Q Fenwick tree point updates: https://www.youtube.com/watch?v=B-BkW9ZpKKM Fenwick tree construction: https://www.youtube.com/watch?v=BHPez138yX8 Fenwick tree source code: https://www.youtube.com/watch?v=eHSQjEtVUGA Data Structures Source Code: https://github.com/williamfiset/algorithms My website: http://www.williamfiset.com =================================== Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5 A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 57 of 60

1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

The video teaches how to construct a Fenwick tree in linear time complexity, which is essential for efficient range queries and point updates. The construction algorithm relies on the cascading idea, where each node propagates its value to its parent node.

Key Takeaways
  1. Initialize the Fenwick tree with an array of zeros
  2. Compute the least significant bit of each index
  3. Update the parent node of each index using the least significant bit
  4. Repeat the process until all nodes have been updated
  5. Check for out-of-bounds situations and ignore them
💡 The linear time complexity algorithm for constructing a Fenwick tree relies on the cascading idea, where each node propagates its value to its parent node, allowing for efficient range queries and point updates.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →