128# include <tclap/CmdLine.h>
134using namespace Aleph;
169template <
typename Set>
179 auto start = chrono::high_resolution_clock::now();
182 auto end = chrono::high_resolution_clock::now();
183 result.
insert_ms = chrono::duration<double, milli>(end - start).count();
190 start = chrono::high_resolution_clock::now();
193 auto* p = set.search(x);
195 cerr <<
"ERROR: element " << x <<
" not found!" << endl;
197 end = chrono::high_resolution_clock::now();
198 result.
search_ms = chrono::duration<double, milli>(end - start).count();
201 start = chrono::high_resolution_clock::now();
204 end = chrono::high_resolution_clock::now();
205 result.
remove_ms = chrono::duration<double, milli>(end - start).count();
208 cerr <<
"ERROR: set not empty after removals!" << endl;
212 cout <<
" " << name <<
":" << endl;
213 cout <<
" Height: " << result.
height << endl;
215 cout <<
" Avg path length: " << fixed << setprecision(2)
228 cout <<
"=== Basic Operations Demo ===" << endl << endl;
234 cout <<
"Inserting elements: 5, 3, 7, 1, 4, 6, 9" << endl;
235 for (
int x : {5, 3, 7, 1, 4, 6, 9})
238 cout <<
"Set size: " << avl_set.
size() << endl;
239 cout <<
"Elements (in order): ";
240 avl_set.
for_each([](
int x) { cout << x <<
" "; });
244 cout << endl <<
"Searching for 4: "
245 << (avl_set.
search(4) ?
"found" :
"not found") << endl;
246 cout <<
"Searching for 8: "
247 << (avl_set.
search(8) ?
"found" :
"not found") << endl;
250 cout << endl <<
"Contains 7: " << (avl_set.
contains(7) ?
"yes" :
"no") << endl;
251 cout <<
"Contains 10: " << (avl_set.
contains(10) ?
"yes" :
"no") << endl;
254 cout << endl <<
"Minimum: " << avl_set.
min() << endl;
255 cout <<
"Maximum: " << avl_set.
max() << endl;
258 cout << endl <<
"Removing 3..." << endl;
260 cout <<
"Elements after removal: ";
261 avl_set.
for_each([](
int x) { cout << x <<
" "; });
273 cout <<
"=== Different Tree Types Demo ===" << endl << endl;
281 vector<int> data = {50, 25, 75, 10, 30, 60, 90, 5, 15, 27, 35};
283 cout <<
"Inserting same data into different tree types:" << endl;
285 for (
int x : data) cout << x <<
" ";
286 cout << endl << endl;
296 cout <<
"Tree heights (lower is better balanced):" << endl;
303 cout << endl <<
"After searching for 5, 10, 15 (watch Splay change):" << endl;
307 cout <<
" Splay height after searches: "
309 cout <<
" (Splay moves accessed elements toward root)" << endl;
320 cout <<
"=== Ranked Operations Demo ===" << endl << endl;
322 cout <<
"Ranked trees maintain subtree sizes, enabling O(log n) operations:" << endl;
323 cout <<
" select(i) - get i-th smallest element (0-indexed)" << endl;
324 cout <<
" position(x) - get rank/position of element x" << endl << endl;
326 cout <<
"Available ranked tree types:" << endl;
327 cout <<
" - Avl_Tree_Rk : AVL with rank (strictly balanced, deterministic)" << endl;
328 cout <<
" - Treap_Rk : Treap with rank (randomized balance)" << endl << endl;
334 vector<int> data = {100, 50, 150, 25, 75, 125, 175, 10, 200};
342 cout <<
"Set contents (sorted): ";
343 avl_rk.
for_each([](
int x) { cout << x <<
" "; });
344 cout << endl << endl;
347 cout <<
"Positional access - select(i) returns i-th element:" << endl;
348 cout <<
" Index AVL_Rk Treap_Rk" << endl;
349 cout <<
" ----- ------ --------" << endl;
350 for (
size_t i = 0; i < avl_rk.
size(); ++i)
352 cout <<
" " << i <<
" "
353 << setw(4) << avl_rk.
select(i) <<
" "
354 << setw(4) << treap_rk.
select(i) << endl;
358 cout << endl <<
"Element ranks - position(x) returns index of x:" << endl;
359 cout <<
" Value AVL_Rk Treap_Rk" << endl;
360 cout <<
" ----- ------ --------" << endl;
361 for (
int x : {10, 50, 100, 150, 200})
363 cout <<
" " << setw(3) << x <<
" "
369 cout << endl <<
"Practical use - Finding median in O(log n):" << endl;
370 size_t mid = avl_rk.
size() / 2;
371 cout <<
" Median (middle element): " << avl_rk.
select(mid) << endl;
374 size_t p25 = avl_rk.
size() / 4;
375 size_t p75 = 3 * avl_rk.
size() / 4;
376 cout <<
" 25th percentile: " << avl_rk.
select(p25) << endl;
377 cout <<
" 75th percentile: " << avl_rk.
select(p75) << endl;
380 cout << endl <<
"Count elements < 100: " << avl_rk.
position(100) << endl;
391 cout <<
"=== Functional Programming Features ===" << endl << endl;
394 for (
int i = 1; i <= 10; ++i)
397 cout <<
"Original set: ";
398 set.
for_each([](
int x) { cout << x <<
" "; });
399 cout << endl << endl;
402 auto evens = set.
filter([](
int x) {
return x % 2 == 0; });
403 cout <<
"Filter (even): ";
404 evens.
for_each([](
int x) { cout << x <<
" "; });
408 auto squares = set.template maps<int>([](
int x) {
return x * x; });
409 cout <<
"Map (square): ";
410 squares.
for_each([](
int x) { cout << x <<
" "; });
414 int sum = set.template foldl<int>(0, [](
int acc,
int x) {
return acc + x; });
415 cout <<
"Fold (sum): " <<
sum << endl;
418 cout << endl <<
"Predicates:" << endl;
419 cout <<
" All positive? " << (set.
all([](
int x) {
return x > 0; }) ?
"yes" :
"no") << endl;
420 cout <<
" Exists > 5? " << (set.
exists([](
int x) {
return x > 5; }) ?
"yes" :
"no") << endl;
421 cout <<
" All <= 10? " << (set.
all([](
int x) {
return x <= 10; }) ?
"yes" :
"no") << endl;
432 cout <<
"=== Performance Comparison ===" << endl;
433 cout <<
"Testing with " << n <<
" elements (seed: " <<
seed <<
")" << endl << endl;
437 cout <<
"Nothing to benchmark: n=0\n\n";
445 for (
size_t i = 0; i < n; ++i)
446 data.push_back(rand());
449 sort(data.begin(), data.end());
450 data.erase(
unique(data.begin(), data.end()), data.end());
454 cout <<
"Nothing to benchmark: all generated keys collapsed to an empty set\n\n";
459 for (
size_t i = data.size(); i > 1; --i)
460 swap(data[i - 1], data[rand() % i]);
462 cout <<
"Actual unique elements: " << data.size() << endl << endl;
465 vector<TimingResult> results;
467 cout <<
"Standard trees (no rank support):" << endl;
468 results.push_back(benchmark_set<AvlSet>(
"AVL Tree", data,
verbose));
469 results.push_back(benchmark_set<RbSet>(
"Red-Black Tree", data,
verbose));
470 results.push_back(benchmark_set<SplaySet>(
"Splay Tree", data,
verbose));
471 results.push_back(benchmark_set<TreapSet>(
"Treap", data,
verbose));
472 results.push_back(benchmark_set<RandSet>(
"Rand Tree", data,
verbose));
474 cout << endl <<
"Ranked trees (with select/position support):" << endl;
475 results.push_back(benchmark_set<AvlRkSet>(
"AVL_Rk", data,
verbose));
476 results.push_back(benchmark_set<TreapRkSet>(
"Treap_Rk", data,
verbose));
480 cout << left << setw(18) <<
"Tree Type"
481 << right << setw(12) <<
"Insert(ms)"
482 << setw(12) <<
"Search(ms)"
483 << setw(12) <<
"Remove(ms)"
484 << setw(10) <<
"Height"
485 << setw(15) <<
"Avg Path" << endl;
486 cout << string(79,
'-') << endl;
488 for (
const auto&
r : results)
490 cout << left << setw(18) <<
r.name
491 << right << fixed << setprecision(2)
492 << setw(12) <<
r.insert_ms
493 << setw(12) <<
r.search_ms
494 << setw(12) <<
r.remove_ms
495 << setw(10) <<
r.height
496 << setw(15) << setprecision(2)
497 << (double)
r.internal_path_length / data.size() << endl;
501 cout <<
"Notes:" << endl;
502 cout <<
" - Height: tree height (log2(" << data.size() <<
") ~= "
503 << fixed << setprecision(1) <<
log2(data.size()) <<
")" << endl;
504 cout <<
" - Avg Path: average path length from root (ideal ~= log2(n))" << endl;
505 cout <<
" - Splay tree optimizes for access patterns, not balance" << endl;
506 cout <<
" - _Rk variants have slight overhead for maintaining subtree sizes" << endl;
507 cout <<
" - Use _Rk trees when you need select(i) or position(x) operations" << endl;
515int main(
int argc,
char* argv[])
520 "Demonstration of DynSetTree with different BST implementations.\n"
521 "Shows how Aleph-w allows swapping data structure implementations\n"
522 "through template parameters.",
525 TCLAP::ValueArg<size_t> nArg(
"n",
"count",
526 "Number of elements for performance test",
527 false, 100000,
"size_t");
530 TCLAP::ValueArg<unsigned int> seedArg(
"s",
"seed",
531 "Random seed (0 = use time)",
532 false, 0,
"unsigned int");
535 TCLAP::SwitchArg allArg(
"a",
"all",
536 "Run all demonstrations (not just performance)",
540 TCLAP::SwitchArg verboseArg(
"v",
"verbose",
541 "Show detailed tree statistics",
545 cmd.parse(argc, argv);
547 size_t n = nArg.getValue();
548 unsigned int seed = seedArg.getValue();
549 bool runAll = allArg.getValue();
550 bool verbose = verboseArg.getValue();
553 seed = time(
nullptr);
555 cout <<
"DynSetTree - Multiple BST Implementations Demo" << endl;
556 cout <<
"==============================================" << endl << endl;
568 cout <<
"Done." << endl;
570 catch (TCLAP::ArgException& e)
572 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() << endl;
Dynamic set backed by balanced binary search trees with automatic memory management.
const Key & min() const
Returns the smallest key contained in the set according to the criterion comparison given.
long position(const Key &key) const
Returns the infix (ordered) position of the key.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
const Key & max() const
Returns the largest key contained in the set according to the criteria comparison given.
size_t remove(const Key &key)
Removes a key from the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
Key * search(const Key &key)
Find an element in the set.
Key & select(size_t i)
Returns the ith node in infix position.
Node * get_root_node() const
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
void demonstrate_functional_features()
TimingResult benchmark_set(const string &name, const vector< int > &data, bool verbose)
void demonstrate_ranked_operations()
void demonstrate_tree_types()
void demonstrate_basic_operations()
void run_performance_comparison(size_t n, unsigned int seed, bool verbose)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log2_function > > log2(const __gmp_expr< T, U > &expr)
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
bool verbose
Flag enabling verbose output.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
size_t internal_path_length
Dynamic set implementations based on balanced binary search trees.