AntInLoop

Binomial Heap Data Structure Code Implementations

Production-ready Binomial Heap Data Structure data structure implementations in C++, Python, Java, and JavaScript on AntInLoop.

Study Binomial Heap Data Structure Guide ยท Launch Visualizer

CPP Implementation

#include <vector>
#include <cmath>

// A Bloom filter is a probabilistic data structure that is used to test whether an element is a member of a set.
// False positive matches are possible, but false negatives are not.

class BloomFilter {
private:
    std::vector<bool> filter;
    int filterSize;

    // A simple hash function
    int hash(int item, int i) {
        return (item + pow(i, 2)) % filterSize;
    }

public:
    // Constructor to initialize the Bloom filter
    BloomFilter(int size) {
        filterSize = size;
        filter = std::vector<bool>(filterSize, false);
    }

    // Insert an item into the Bloom filter
    void insert(int item) {
        for (int i = 0; i < 3; i++) {
            filter[hash(item, i)] = true;
        }
    }

    // Check if an item exists in the Bloom filter
    bool search(int item) {
        for (int i = 0; i < 3; i++) {
            if (!filter[hash(item, i)]) {
                return false;
            }
        }
        return true;
    }

    // Check if the Bloom filter is empty
    bool isEmpty() {
        for (bool bit : filter) {
            if (bit) {
                return false;
            }
        }
        return true;
    }

    // Get the size of the Bloom filter
    int size() {
        return filterSize;
    }
};