This repository contains a high-performance, purely functional Set data structure implemented using a self-balancing AVL Tree in F#.
Unlike standard library collections, this implementation focuses on efficient set-theoretic operations (Union, Intersection, Difference, Symmetrical Difference) using the Split/Join algorithm, providing a solid foundation for both sequential and parallel data processing.
The project is built on the principles of immutability and persistent data structures. Every modification returns a new version of the set, while maximizing node sharing to optimize memory usage.
-
Basic Operations:
add,delete,containsare implemented with$O(\log n)$ time complexity. -
Set-theoretic Operations:
union,intersection,difference,symmetrical differenceutilize the Split & Join approach.- Efficiency: This reduces complexity to
$O(m \log (n/m))$ where m is the size of the smaller set.
- Efficiency: This reduces complexity to
-
Parallelism: Recursive set operations are implemented using
Parallel.Invoke.
- Balance Factor: For every node, the height difference between left and right subtrees is at most 1.
- Rotations: Four types of rotations (LL, RR, LR, RL) are performed automatically.
- .NET SDK 10.0+
- Fantomas
# Restore local tools (Fantomas)
dotnet tool restore
# Build the entire solution
dotnet build -c Releasedotnet testdotnet run -c Release --project benchmarks/AVLSet.Benchmarksopen AVLSet.Library
// 1. Create sets from sequences
let setA = [1..10] |> List.fold (fun s v -> AVLSet.add v s) AVLSet.empty
let setB = [5..15] |> List.fold (fun s v -> AVLSet.add v s) AVLSet.empty
// 2. Perform set operations
let unionSet = AVLSet.union setA setB
let interSet = AVLSet.intersection setA setB
// 3. Check membership
if AVLSet.contains 7 interSet then
printfn "Intersection contains 7"
// 4. Parallel operations for large data
let opts = System.Threading.Tasks.ParallelOptions(MaxDegreeOfParallelism = 4)
let largeUnion = AVLSet.parallelUnion opts setA setBThe AVLSet module provides a comprehensive interface:
| Function | Signature | Description |
|---|---|---|
| add | 'a -> AVLTree<'a> -> AVLTree<'a> |
Adds an element. |
| delete | 'a -> AVLTree<'a> -> AVLTree<'a> |
Removes an element. |
| contains | 'a -> AVLTree<'a> -> bool |
Checks membership. |
| union | AVLTree<'a> -> AVLTree<'a> -> AVLTree<'a> |
Standard union ( |
| intersection | AVLTree<'a> -> AVLTree<'a> -> AVLTree<'a> |
Standard intersection ( |
| difference | AVLTree<'a> -> AVLTree<'a> -> AVLTree<'a> |
Standard difference ( |
| symmDifference | AVLTree<'a> -> AVLTree<'a> -> AVLTree<'a> |
Standard symmetrical difference ( |
| parallel(Union/Intersection/Difference/SymmDiff) | ParallelOptions -> AVLTree<'a> -> AVLTree<'a> -> AVLTree<'a> |
Multi-threaded set-theoretic operations. |
| (union/intersection/difference/symmDiff)Traversal | AVLTree<'a> -> AVLTree<'a> -> AVLTree<'a> |
Set-theoretic operations via tree traversal. |
/src
└── AVLSet.Library
├── AVLSet.Library.fsproj
└── Library.fs
/tests
└── AVLSet.UnitTests
├── AVLSet.UnitTests.fsproj
└── Tests.fs
/benchmarks
└── AVLSet.Benchmarks
├── AVLSet.Benchmarks.fsproj
├── Benchmarks.fs
└── Program.fs