The Wayback Machine - https://web.archive.org/web/20240811133513/https://www.geeksforgeeks.org/fenwick-tree-vs-segment-tree/
Open In App

Comparison between Fenwick Tree vs Segment Tree – with Similarities and Differences

Last Updated : 15 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Fenwick Tree (Binary Indexed Tree) and Segment Tree are both data structures used for efficient range query and update operations on an array. Here’s a tabular comparison of these two data structures.

Similarities between the Fenwick tree and Segment tree

Here are some of the areas where Fenwick Tree is similar to Segment Tree:

  • Array type: Both Fenwick Tree and Segment Tree are based on 1D arrays.
  • Construction time: Both structures require O(N * log(N)) time to construct.
  • Query time (range sum): Both structures offer O(log(N)) time complexity for range sum queries.
  • Update time (point): Both structures support point updates in O(log(N)) time.

Let us see these in the form of a table.

Feature Fenwick Tree (BIT) Segment Tree
Array type 1D array 1D array
Construction time O(N*log(N)) O(N*log(N))
Query time (range sum) O(log(N)) O(log(N))
Update time (point) O(log(N)) O(log(N))

Difference between Fenwick tree and Segment tree

Now let us look into the areas where Fenwick tree is different from Segment Tree:

  • Purpose: Fenwick Tree is primarily used for efficient prefix sum queries and point updates, while Segment Tree is more versatile and can be used for a wide range of range query and update operations.
  • Space complexity: Fenwick Tree has a space complexity of O(N), whereas Segment Tree typically requires O(4*N) space.
  • Update time (range): Segment Tree efficiently supports range updates with O(log(N) + K) time complexity, where K is the size of the range.
  • Query range type: Fenwick Tree is primarily used for prefix sum queries, whereas Segment Tree can handle arbitrary range queries.
  • Range updates: Segment Tree supports efficient range updates, while Fenwick Tree does not.
  • Lazy propagation: Segment Tree supports lazy propagation for efficient range updates, which Fenwick Tree does not require.
  • Additional operations: Segment Trees can be customized for various operations beyond simple range queries and updates, whereas Fenwick Trees are typically used for prefix sum calculations.
  • Memory-efficient: Fenwick Trees are more memory-efficient than Segment Trees.

Let us see these in the form of table:

Feature Fenwick Tree (BIT) Segment Tree
Purpose Primarily used for efficient range queries (sums) and point updates. Primarily used for efficient range queries and updates.
Space complexity O(N) O(4*N)
Update time (range) Not efficient O(log(N) + K)
Query range type Prefix sum queries Arbitrary range
Range updates Not efficient Supported
Lazy propagation Not applicable Supported
Additional operations Not common Customizable
Memory-efficient Yes No

Conclusion:

In summary, Fenwick Trees are more memory-efficient and are suitable for prefix sum queries with point updates, while Segment Trees are more versatile and support a wider range of operations, including efficient range updates and custom operations. The choice between them depends on the specific requirements of your application.



Similar Reads

Binary Indexed Tree/Fenwick Tree meaning in DSA
Binary Indexed Tree (BIT), also known as Fenwick Tree, is a data structure used for efficiently querying and updating cumulative frequency tables, or prefix sums. A Fenwick Tree is a complete binary tree, where each node represents a range of elements in an array and stores the sum of the elements in that range. Below is a simple structure of a Bin
3 min read
Two Dimensional Binary Indexed Tree or Fenwick Tree
Prerequisite - Fenwick Tree We know that to answer range sum queries on a 1-D array efficiently, binary indexed tree (or Fenwick Tree) is the best choice (even better than segment tree due to less memory requirements and a little faster than segment tree). Can we answer sub-matrix sum queries efficiently using Binary Indexed Tree ?The answer is yes
15+ min read
Order statistic tree using fenwick tree (BIT)
Given an array of integers with limited range (0 to 1000000). We need to implement an Order statistic tree using fenwick tree. It should support four operations: Insert, Delete, Select and Rank. Here n denotes the size of Fenwick tree and q denotes number of queries. Each query should be one of the following 4 operations. insertElement(x) - Insert
9 min read
Fenwick Tree (Binary Indexed Tree) for Competitive Programming
In the world of competitive programming, speed is everything. Fenwick Tree (also known as Binary Indexed Tree), created by Peter M. Fenwick. They're like secret weapons for programmers, especially when you need to quickly find cumulative frequencies in problem-solving. This article breaks down Fenwick Trees in simple terms—how they're built and why
15+ min read
XOR of elements in a given range with updates using Fenwick Tree
Given an array A[] of integers and array Q consisting of queries of the following two types: (1, L, R) : Return XOR of all elements present between indices L and R.(2, I, val) : update A[I] to A[I] XOR val. The task is to solve each query and print the XOR for every Query of 1st type, using Fenwick Tree.Examples: Input: A[] = {2, 1, 1, 3, 2, 3, 4,
13 min read
Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree
Given an array A[ ] consisting of non-negative integers and a matrix Q[ ][ ] consisting of queries of the following two types: (1, l, val): Update A[l] to A[l] + val.(2, K): Find the lower_bound of K in the prefix sum array of A[ ]. If the lower_bound does not exist, print -1. The task for each query of the second type is to print the index of the
14 min read
Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
Prerequisites: Fenwick Tree (Binary Indexed Tree)Given an array of N numbers, and a number of queries where each query will contain three numbers(l, r and k). The task is to calculate the number of array elements that are greater than K in the subarray[L, R]. Examples: Input: n=6 q=2 arr[ ] = { 7, 3, 9, 13, 5, 4 } Query1: l=1, r=4, k=6 Query2: l=2,
15+ min read
Find middle point segment from given segment lengths
Given an array arr[] of size M. The array represents segment lengths of different sizes. These segments divide a line beginning with 0. The value of arr[0] represents a segment from 0 arr[0], value of arr[1] represents segment from arr[0] to arr[1], and so on. The task is to find the segment which contains the middle point, If the middle segment do
6 min read
Comparison between Height Balanced Tree and Weight Balanced Tree
What is Height Balanced Tree? Self-Balancing binary search trees are the height-balanced binary tree is one for which at every node, the absolute value of the difference in heights of the left and right subtree is no larger than one. An empty tree is height-balanced. A non-empty binary tree T is balanced if: The left subtree of T is balanced.The ri
4 min read
Comparison between Heap and Tree
What is Heap? A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Types of Heap Data Structure: Generally, Heaps can be of two types: Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of its children. The same property must be recursively true for all sub-t
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg