A C++ probability & statistics library built around a template-based Binary Search Tree (BST) and a set/multiset abstraction. It supports:
- BST-backed set / multiset storage (with duplicate counting)
- Logical operations (intersection, union, difference, symmetric difference)
- Statistical calculations (mean, variance, standard deviation, range, mode)
- Discrete probability utilities (PMF/CMF)
- Utility helpers like normalization (standardization) and duplicate removal
Documentation reference: see
DOCUMENTATION.MD.
- Generic node storing:
datacounter(occurrence count for multiset behavior)left/rightchild pointers
- Template BST with:
- insertion, search, deletion
- balancing
- height calculation
- deep copy semantics (copy constructor + assignment operator)
- Intersection: keeps common elements with minimum counts
- Union: keeps elements with maximum counts
- Difference (
A - B): subtracts counts / removes elements based on membership - Addition / symmetric-like operation (
A + B): union plus additional handling of intersection counts (see docs)
- average / powered average
- variance
- standard deviation
- range
- mode
- PMF: probability mass function as
std::vector<double> - CMF: cumulative mass function as
std::vector<double>
- switch between SET and MULTISET modes
- remove duplicates (convert to SET)
- normalize / standardize:
(x - μ) / σ
bst.h/bst.tpp— template-based BST implementationmultiset.h/multiset.cpp— set/multiset wrapper + probability/statistics operationsDOCUMENTATION.MD— detailed architecture + method list + example usageREADME.md— this file
This project appears to be a header + source library (not packaged). The typical workflow is:
- Add the repository (or the needed files) to your project.
- Include
multiset.h(and compile/linkmultiset.cpp) in your build.
#include "multiset.h"
#include <vector>
int main() {
multiset<int> set1(MULTISET);
multiset<int> set2(MULTISET);
// Insert elements
set1.insert(5, 3); // add 5 three times
set1.insert(10, 2);
set2.insert(5, 2);
set2.insert(10, 3);
// Set operations
multiset<int> intersection = set1.intersect(set2);
multiset<int> unionSet = set1.unite(set2);
multiset<int> difference = set1 - set2;
// Statistics
double avg = set1.average();
double var = set1.variance();
double stdev = set1.standard_deviation();
int mode = set1.Mode();
// Probability functions
std::vector<double> pmf = set1.pmf();
std::vector<double> cmf = set1.cmf();
// Comparison
if (set1 >= set2) { /* set1 is superset */ }
if (set1 == set2) { /* sets are equal */ }
return 0;
}If you want, I can add a
Makefileor CMake example once you tell me your preferred build system.
A template-based BST storing DataType values and tracking:
nodes_count: number of unique nodestotal_count: total number of items including duplicates (sum of counters)
Common public operations:
insert(const DataType&, uint32_t counter=1)search(const DataType&)remove(const DataType&)balance()height()display()get_nodes_count()get_total_count()- deep copy + cleanup (
copy ctor,operator=, destructor)
enum mode { SET=0, MULTISET=1 };- SET mode: only one copy of each element allowed
- MULTISET mode: duplicates allowed via internal counters
intersect(const multiset&)unite(const multiset&)operator-(const multiset&)(difference)operator+(const multiset&)(addition / symmetric-like behavior)
operator>=(superset or equal)operator<=(subset or equal)operator==(equality)
insert(const DataType& d, uint32_t c=1)remove(const DataType& d)search(const DataType&)count(const DataType&)adjust_count(const DataType&, uint32_t &new_count)replace(const DataType& old_data, const DataType& new_data)clear()
Central tendency:
average()average(const double& power)Mode()
Dispersion:
variance()standard_deviation()range()
Probability:
pmf()cmf()
Utilities:
display()(prints like{(data,count) ...})set_mode(...)remove_duplicates()normalize()(standardize)
| Operation | Time Complexity | Space |
|---|---|---|
| Insert | O(log n) avg, O(n) worst | O(1) |
| Search | O(log n) avg, O(n) worst | O(1) |
| Delete | O(log n) avg, O(n) worst | O(1) |
| Balance | O(n) | O(n) |
| Height | O(n) | O(n) recursion |
| Operation | Complexity |
|---|---|
| Intersection | O(n₁ log n₂) |
| Difference | O(n₁ log n₂) |
| Union | O(n₁ + n₂ + merge) |
| Statistical calculations | O(n) |
| Equality / containment checks | O(n) |
- Requires a C++ compiler with template support (modern compilers recommended).
DataTypeshould be comparable (BST ordering depends on comparison operators).