219#include <tclap/CmdLine.h>
222using namespace Aleph;
229 cout <<
"\n" << string(60,
'=') <<
endl;
230 cout <<
"Trie: Basic Operations" <<
endl;
231 cout << string(60,
'=') <<
endl;
235 cout <<
"\n--- Insertion ---" <<
endl;
237 vector<string> words = {
"cat",
"car",
"card",
"care",
"careful",
"cart"};
239 cout <<
"Inserting words with common prefix 'ca':" <<
endl;
240 for (
const auto&
word : words)
242 bool inserted =
root.insert_word(
word);
243 cout <<
" " <<
word <<
" -> " << (inserted ?
"inserted" :
"exists") <<
endl;
247 cout <<
"\nTrying to insert duplicate:" <<
endl;
248 cout <<
" cat -> " << (
root.insert_word(
"cat") ?
"inserted" :
"already exists") <<
endl;
250 cout <<
"\n--- Search ---" <<
endl;
253 cout <<
"Searching for words:" <<
endl;
257 cout <<
" " <<
word <<
" -> " << (found ?
"FOUND" :
"not found") <<
endl;
260 cout <<
"\n--- Statistics ---" <<
endl;
261 cout <<
"Total words stored: " <<
root.count() <<
endl;
263 cout <<
"\n--- All Words ---" <<
endl;
264 cout <<
"Words in lexicographic order:" <<
endl;
266 for (
size_t i = 0; i <
all_words.size(); ++i)
269 cout <<
" " << (i + 1) <<
". " <<
word <<
endl;
280 cout <<
"\n" << string(60,
'=') <<
endl;
281 cout <<
"Prefix Search: Autocomplete Feature" <<
endl;
282 cout << string(60,
'=') <<
endl;
288 "apple",
"application",
"apply",
"approach",
290 "banana",
"band",
"bandana",
"bank",
"banner",
291 "car",
"card",
"care",
"careful",
"careless",
"career",
292 "cart",
"cartoon",
"carton"
295 cout <<
"\nBuilding dictionary with " <<
dictionary.size() <<
" words..." <<
endl;
299 cout <<
"\n--- Prefix Search Demo ---" <<
endl;
305 cout <<
"\nPrefix '" <<
prefix <<
"' matches:" <<
endl;
310 cout <<
" (no matches)" <<
endl;
313 for (
size_t i = 0; i <
matches.size(); ++i)
316 cout <<
" - " << match <<
endl;
321 cout <<
"\n--- Simulating Autocomplete ---" <<
endl;
324 cout <<
"\nTyping simulation (showing suggestions):" <<
endl;
326 while (
input.length() <= 4)
329 cout <<
" User types: '" <<
input <<
"'" <<
endl;
330 cout <<
" Suggestions (" <<
suggestions.size() <<
" matches): ";
335 if (i > 0) cout <<
", ";
340 cout <<
" ...(" << (
suggestions.size() - 5) <<
" more)";
358 cout <<
"\n" << string(60,
'=') <<
endl;
359 cout <<
"Practical Example: Simple Spell Checker" <<
endl;
360 cout << string(60,
'=') <<
endl;
366 "program",
"programming",
"programmer",
"progress",
"project",
367 "computer",
"compute",
"computing",
"computation",
368 "algorithm",
"algorithms",
"algorithmic",
369 "data",
"database",
"datum",
370 "structure",
"structures",
"structural",
371 "the",
"they",
"them",
"there",
"their",
"these",
372 "hello",
"help",
"helper",
"helpful"
375 cout <<
"Loading dictionary with " <<
dictionary.size() <<
" words..." <<
endl;
379 cout <<
"\n--- Spell Check Demo ---" <<
endl;
385 cout <<
"\nChecking: '" <<
word <<
"'" <<
endl;
389 cout <<
" Status: Correct!" <<
endl;
393 cout <<
" Status: Not found - might be misspelled" <<
endl;
403 cout <<
" Did you mean: ";
407 if (i > 0) cout <<
", ";
426 cout <<
"\n" << string(60,
'=') <<
endl;
427 cout <<
"Practical Example: Shell Command Autocomplete" <<
endl;
428 cout << string(60,
'=') <<
endl;
434 "cd",
"ls",
"pwd",
"mkdir",
"rmdir",
"rm",
"cp",
"mv",
435 "cat",
"less",
"more",
"head",
"tail",
436 "grep",
"find",
"locate",
"which",
"whereis",
437 "chmod",
"chown",
"chgrp",
438 "ps",
"top",
"htop",
"kill",
"killall",
439 "ssh",
"scp",
"sftp",
440 "git",
"gitk",
"github",
441 "make",
"cmake",
"gcc",
"g++",
"gdb",
442 "python",
"python3",
"pip",
"pip3",
443 "apt",
"apt-get",
"apt-cache"
446 cout <<
"Loading " <<
commands.size() <<
" shell commands..." <<
endl;
450 cout <<
"\n--- Tab Completion Simulation ---" <<
endl;
456 cout <<
"\n$ " <<
input <<
"<TAB>" <<
endl;
461 cout <<
" (no completions)" <<
endl;
465 cout <<
" -> " << c <<
" (unique match)" <<
endl;
469 cout <<
" Possible completions: ";
472 if (i > 0) cout <<
" ";
488 cout <<
"\n" << string(60,
'=') <<
endl;
489 cout <<
"Trie Structure Visualization" <<
endl;
490 cout << string(60,
'=') <<
endl;
496 cout <<
"\nInserting: ";
497 for (
size_t i = 0; i < words.size(); ++i)
499 if (i > 0) cout <<
", ";
501 root.insert_word(words[i]);
505 cout <<
"\nTrie structure:" <<
endl;
506 cout <<
" root" <<
endl;
507 cout <<
" |" <<
endl;
508 cout <<
" c" <<
endl;
509 cout <<
" |" <<
endl;
510 cout <<
" a" <<
endl;
511 cout <<
" /|" <<
endl;
512 cout <<
" t r ($=end)" <<
endl;
513 cout <<
" | |" <<
endl;
514 cout <<
" $ ($) d" <<
endl;
515 cout <<
" |" <<
endl;
516 cout <<
" $" <<
endl;
518 cout <<
"Words: cat, car, card" <<
endl;
519 cout <<
"Notice how 'c', 'a' are shared!" <<
endl;
521 cout <<
"\nTree string representation: " <<
root.to_str() <<
endl;
531 cout <<
"\n" << string(60,
'=') <<
endl;
532 cout <<
"Performance Analysis (n = " << n <<
" words)" <<
endl;
533 cout << string(60,
'=') <<
endl;
542 vector<string> roots = {
"act",
"form",
"port",
"ject",
"duct",
"spect",
"scrib",
"struct",
"mit",
"vers"};
545 for (
int i = 0; i < n; ++i)
548 roots[(i /
prefixes.size()) % roots.size()] +
551 if (i % 3 == 0)
word +=
"ed";
552 if (i % 5 == 0)
word +=
"ly";
553 if (i % 7 == 0)
word +=
"ing";
554 words.push_back(
word);
557 cout <<
"\nGenerated " << words.size() <<
" words for testing" <<
endl;
560 auto start = chrono::high_resolution_clock::now();
562 for (
const auto&
word : words)
565 auto mid = chrono::high_resolution_clock::now();
569 for (
const auto&
word : words)
572 auto end = chrono::high_resolution_clock::now();
574 auto insert_us = chrono::duration_cast<chrono::microseconds>(
mid - start).count();
575 auto search_us = chrono::duration_cast<chrono::microseconds>(end -
mid).count();
577 cout <<
"\nResults:" <<
endl;
578 cout <<
" Words in trie: " <<
root.count() <<
endl;
581 cout <<
" Found: " << found <<
"/" << words.size() <<
endl;
584 start = chrono::high_resolution_clock::now();
593 end = chrono::high_resolution_clock::now();
595 auto prefix_us = chrono::duration_cast<chrono::microseconds>(end - start).count();
597 cout <<
"\nPrefix search (10 prefixes):" <<
endl;
608 TCLAP::CmdLine
cmd(
"Trie (Prefix Tree) Example",
' ',
"1.0");
610 TCLAP::ValueArg<int>
nArg(
"n",
"count",
611 "Number of words for performance test",
false, 1000,
"int");
612 TCLAP::SwitchArg
basicArg(
"b",
"basic",
613 "Show basic operations",
false);
614 TCLAP::SwitchArg
prefixArg(
"p",
"prefix",
615 "Show prefix search / autocomplete",
false);
616 TCLAP::SwitchArg
spellArg(
"s",
"spell",
617 "Show spell checker example",
false);
618 TCLAP::SwitchArg
cmdArg(
"c",
"commands",
619 "Show command autocomplete example",
false);
620 TCLAP::SwitchArg
structArg(
"t",
"structure",
621 "Show trie structure visualization",
false);
622 TCLAP::SwitchArg
perfArg(
"f",
"performance",
623 "Run performance analysis",
false);
624 TCLAP::SwitchArg
allArg(
"a",
"all",
625 "Run all demos",
false);
638 int n =
nArg.getValue();
651 cout <<
"=== Trie (Prefix Tree): Efficient String Storage ===" <<
endl;
671 cout <<
"\n=== Summary ===" <<
endl;
672 cout <<
"Tries excel at:" <<
endl;
673 cout <<
" - Fast prefix searches (autocomplete)" <<
endl;
674 cout <<
" - Memory-efficient storage of strings with shared prefixes" <<
endl;
675 cout <<
" - O(k) operations where k = word length" <<
endl;
676 cout <<
"Use cases: autocomplete, spell checkers, IP routing, dictionaries" <<
endl;
680 catch (TCLAP::ArgException& e)
682 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
Prefix tree (Trie) node for storing character sequences.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
static void prefix(Node *root, DynList< Node * > &acc)
Trie (prefix tree) implementation.
void demo_basic_operations()
Demonstrate basic trie operations.
void demo_prefix_search()
Demonstrate prefix search - the trie's killer feature.
void demo_trie_structure()
Show trie structure visualization.
void demo_command_autocomplete()
Practical example: Command-line autocomplete.
void demo_spell_checker()
Practical example: Spell checker suggestions.