This project implements a Two-Three Heap data structure that supports efficient operations on multiple integer sets. The structure allows performing the following operations:
1 i x– Inserts the elementxinto the sub-multiseti(duplicates are allowed).2 i– Extracts and prints the smallest element in sub-multiseti.- If multiple elements have the same minimum value, the one inserted first is removed.
3 i x– Decreases the value of thei-th inserted element byx.4 i j– Merges all elements from sub-multisetiinto sub-multisetj.- After this operation,
ibecomes empty.
- After this operation,
- Initial Conditions: There are
Nsub-multisets, all initially empty. - Guaranteed Conditions:
- There is at least one element in sub-multiset
ifor operation2 i. - There is no underflow when performing operation
3 i x.
- There is at least one element in sub-multiset
The Two-Three Heap is a mergeable heap optimized for operations that require frequent merging. The key components of the implementation are:
- Each new node is wrapped inside a tree of dimension
0and added to the heap. - If the heap already contains a tree of the same dimension, merging occurs.
- The heap property ensures that the minimum value is always accessible.
- The smallest root across all trees in
heaps[i]is selected and removed. - If multiple roots have the same value, the one with the smallest insertion order is chosen.
- The removed node's children are reintegrated into the heap.
- The value of a specific node is decreased by
x. - If the node violates the heap property (i.e., it becomes smaller than its parent), a swap is performed to restore the property.
- This propagates up the tree until the heap order is restored.
- Trees from
heaps[i]are merged intoheaps[j]while maintaining the heap structure. - The binomial heap representation ensures efficient merging.
| Operation | Complexity |
|---|---|
Insert (1 i x) |
O(1) |
Extract Min (2 i) |
O(log N) |
Decrease Key (3 i x) |
O(log N) |
Merge (4 i j) |
O(log N) |
Below are performance comparisons of the 2-3 Heap against other heap-based data structures.
The 2-3 Heap shows improved performance in scenarios where merging is frequent.
Compile the C++ source file using g++:
g++ -O2 -std=c++17 main.cpp -o heap./heap < input.txtN Q
<operation> <arguments>
...


