Fenwick Tree (Binary Indexed Tree) Implementation in Bash

This document explains how to implement a Fenwick Tree (also known as a Binary Indexed Tree) using Bash. A Fenwick Tree is a data structure that provides efficient methods for calculating prefix sums and updating elements in an array. It is widely used in scenarios where there is a need for fast cumulative frequency tables.

Program Structure

The program is structured into several key functions:

  • initialize_tree: Initializes the Fenwick Tree with a given size.
  • update: Updates the Fenwick Tree with a value at a specific index.
  • prefix_sum: Calculates the prefix sum from the start of the array to a specific index.

Implementation


#!/bin/bash

# Initialize the Fenwick Tree
initialize_tree() {
    local n=$1
    fenwick_tree=()
    for (( i=0; i<=n; i++ )); do
        fenwick_tree[i]=0
    done
    array_size=$n
}

# Update the Fenwick Tree at a specific index
update() {
    local index=$1
    local value=$2

    while [ $index -le $array_size ]; do
        fenwick_tree[index]=$((fenwick_tree[index] + value))
        index=$((index + (index & -index)))
    done
}

# Calculate the prefix sum up to a specific index
prefix_sum() {
    local index=$1
    local sum=0

    while [ $index -gt 0 ]; do
        sum=$((sum + fenwick_tree[index]))
        index=$((index - (index & -index)))
    done

    echo $sum
}

# Example usage
initialize_tree 10

# Update the tree with some values
update 1 5
update 2 3
update 3 7
update 4 6
update 5 9
update 6 -4
update 7 2
update 8 1
update 9 8
update 10 10

# Calculate some prefix sums
echo "Prefix sum up to index 5: $(prefix_sum 5)"
echo "Prefix sum up to index 8: $(prefix_sum 8)"
echo "Prefix sum up to index 10: $(prefix_sum 10)"
    

Explanation

The program implements a basic Fenwick Tree with the following components:

Initialization

The initialize_tree function sets up the Fenwick Tree with a specified size. The tree is represented by an array, fenwick_tree, which is initialized to all zeros. The array size is stored in array_size.

Updating the Tree

The update function updates the Fenwick Tree at a specific index with a given value. The update process involves adding the value to the appropriate elements in the tree by following the update rules of the Fenwick Tree, which ensures that the prefix sums remain accurate.

Calculating Prefix Sums

The prefix_sum function calculates the sum of elements from the start of the array up to a specific index. It does this by summing the values in the Fenwick Tree that contribute to the prefix sum of the desired index, using the tree’s structure to quickly aggregate these values.

Example Usage

The example usage section demonstrates how to initialize a Fenwick Tree with 10 elements, update the tree with various values, and then calculate the prefix sums up to specific indices (5, 8, and 10). The program outputs the computed prefix sums for these indices.

Conclusion

This Bash implementation of a Fenwick Tree provides a simple yet powerful way to perform efficient prefix sum calculations and updates in an array. Fenwick Trees are particularly useful in scenarios where frequent updates and prefix sum queries are required, such as in competitive programming and data analysis tasks.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)