223#include <tclap/CmdLine.h>
226using namespace Aleph;
233 cout <<
"\n" << string(60,
'=') << endl;
234 cout <<
"Trie: Basic Operations" << endl;
235 cout << string(60,
'=') << endl;
239 cout <<
"\n--- Insertion ---" << endl;
241 vector<string> words = {
"cat",
"car",
"card",
"care",
"careful",
"cart"};
243 cout <<
"Inserting words with common prefix 'ca':" << endl;
244 for (
const auto &word : words)
247 cout <<
" " << word <<
" -> " << (inserted ?
"inserted" :
"exists") << endl;
251 cout <<
"\nTrying to insert duplicate:" << endl;
252 cout <<
" cat -> " << (trie.
insert_word(
"cat") ?
"inserted" :
"already exists") << endl;
254 cout <<
"\n--- Search ---" << endl;
256 vector<string> to_find = {
"cat",
"car",
"care",
"cap",
"dog"};
257 cout <<
"Searching for words:" << endl;
258 for (
const auto &word : to_find)
261 cout <<
" " << word <<
" -> " << (found ?
"FOUND" :
"not found") << endl;
264 cout <<
"\n--- Statistics ---" << endl;
265 cout <<
"Total words stored: " << trie.
count() << endl;
267 cout <<
"\n--- All Words ---" << endl;
268 cout <<
"Words in lexicographic order:" << endl;
269 auto all_words = trie.
words();
270 for (
size_t i = 0; i < all_words.size(); ++i)
272 string word = all_words[i];
273 cout <<
" " << (i + 1) <<
". " << word << endl;
282 cout <<
"\n" << string(60,
'=') << endl;
283 cout <<
"Prefix Search: Autocomplete Feature" << endl;
284 cout << string(60,
'=') << endl;
289 vector<string> dictionary = {
"apple",
"application",
"apply",
"approach",
"apt",
290 "aptitude",
"banana",
"band",
"bandana",
"bank",
291 "banner",
"car",
"card",
"care",
"careful",
292 "careless",
"career",
"cart",
"cartoon",
"carton"};
294 cout <<
"\nBuilding dictionary with " << dictionary.
size() <<
" words..." << endl;
295 for (
const auto &word : dictionary)
298 cout <<
"\n--- Prefix Search Demo ---" << endl;
300 vector<string> prefixes = {
"app",
"ban",
"car",
"cart",
"xyz"};
302 for (
const auto &
prefix : prefixes)
304 cout <<
"\nPrefix '" <<
prefix <<
"' matches:" << endl;
308 if (matches.size() == 0)
309 cout <<
" (no matches)" << endl;
312 for (
size_t i = 0; i < matches.size(); ++i)
314 string match = matches[i];
315 cout <<
" - " << match << endl;
320 cout <<
"\n--- Simulating Autocomplete ---" << endl;
323 cout <<
"\nTyping simulation (showing suggestions):" << endl;
325 while (input.length() <= 4)
328 cout <<
" User types: '" << input <<
"'" << endl;
329 cout <<
" Suggestions (" << suggestions.
size() <<
" matches): ";
332 for (
size_t i = 0; i < suggestions.size()
and shown < 5; ++i, ++shown)
336 string s = suggestions[i];
339 if (suggestions.size() > 5)
340 cout <<
" ...(" << (suggestions.size() - 5) <<
" more)";
346 else if (input ==
"ca")
348 else if (input ==
"car")
360 cout <<
"\n" << string(60,
'=') << endl;
361 cout <<
"Practical Example: Simple Spell Checker" << endl;
362 cout << string(60,
'=') << endl;
367 vector<string> dictionary = {
368 "program",
"programming",
"programmer",
"progress",
"project",
"computer",
"compute",
369 "computing",
"computation",
"algorithm",
"algorithms",
"algorithmic",
"data",
"database",
370 "datum",
"structure",
"structures",
"structural",
"the",
"they",
"them",
371 "there",
"their",
"these",
"hello",
"help",
"helper",
"helpful"};
373 cout <<
"Loading dictionary with " << dictionary.
size() <<
" words..." << endl;
374 for (
const auto &word : dictionary)
377 cout <<
"\n--- Spell Check Demo ---" << endl;
379 vector<string> to_check = {
"program",
"progam",
"algoritm",
"helllo",
"data",
"computer"};
381 for (
const auto &word : to_check)
383 cout <<
"\nChecking: '" << word <<
"'" << endl;
387 cout <<
" Status: Correct!" << endl;
391 cout <<
" Status: Not found - might be misspelled" << endl;
394 for (
size_t prefixLen = word.length() - 1; prefixLen >= 2; --prefixLen)
396 string prefix = word.substr(0, prefixLen);
399 if (suggestions.size() > 0)
401 cout <<
" Did you mean: ";
403 for (
size_t i = 0; i < suggestions.size()
and shown < 3; ++i, ++shown)
407 string s = suggestions[i];
423 cout <<
"\n" << string(60,
'=') << endl;
424 cout <<
"Practical Example: Shell Command Autocomplete" << endl;
425 cout << string(60,
'=') << endl;
430 vector<string> commands = {
431 "cd",
"ls",
"pwd",
"mkdir",
"rmdir",
"rm",
"cp",
"mv",
"cat",
432 "less",
"more",
"head",
"tail",
"grep",
"find",
"locate",
"which",
"whereis",
433 "chmod",
"chown",
"chgrp",
"ps",
"top",
"htop",
"kill",
"killall",
"ssh",
434 "scp",
"sftp",
"git",
"gitk",
"github",
"make",
"cmake",
"gcc",
"g++",
435 "gdb",
"python",
"python3",
"pip",
"pip3",
"apt",
"apt-get",
"apt-cache"};
437 cout <<
"Loading " << commands.
size() <<
" shell commands..." << endl;
438 for (
const auto &
cmd : commands)
441 cout <<
"\n--- Tab Completion Simulation ---" << endl;
443 vector<string> partial_inputs = {
"g",
"gi",
"apt",
"ch",
"py"};
445 for (
const auto &input : partial_inputs)
447 cout <<
"\n$ " << input <<
"<TAB>" << endl;
451 if (completions.size() == 0)
452 cout <<
" (no completions)" << endl;
453 else if (completions.size() == 1)
455 string c = completions[0];
456 cout <<
" -> " << c <<
" (unique match)" << endl;
460 cout <<
" Possible completions: ";
461 for (
size_t i = 0; i < completions.size(); ++i)
465 string c = completions[i];
478 cout <<
"\n" << string(60,
'=') << endl;
479 cout <<
"Trie Structure Visualization" << endl;
480 cout << string(60,
'=') << endl;
484 vector<string> words = {
"cat",
"car",
"card"};
486 cout <<
"\nInserting: ";
487 for (
size_t i = 0; i < words.size(); ++i)
496 cout <<
"\nTrie structure:" << endl;
497 cout <<
" root" << endl;
498 cout <<
" |" << endl;
499 cout <<
" c" << endl;
500 cout <<
" |" << endl;
501 cout <<
" a" << endl;
502 cout <<
" / \\" << endl;
503 cout <<
" r* t*" << endl;
504 cout <<
" |" << endl;
505 cout <<
" d*" << endl;
507 cout <<
"Words: cat, car, card" << endl;
508 cout <<
"Notice how 'c', 'a' are shared!" << endl;
509 cout <<
"The '*' marks word endings stored as node state, not child nodes." << endl;
512 if (not structure.empty()
and structure.front() ==
'\0')
513 structure.replace(0, 1,
"<root>");
514 cout <<
"\nTree string representation: " << structure << endl;
522 cout <<
"\n" << string(60,
'=') << endl;
523 cout <<
"Performance Analysis (n = " << n <<
" words)" << endl;
524 cout << string(60,
'=') << endl;
529 vector<string> words;
532 vector<string> prefixes = {
"pre",
"post",
"un",
"re",
"in",
"ex",
"sub",
"super",
"anti",
"auto"};
533 vector<string> roots = {
"act",
"form",
"port",
"ject",
"duct",
534 "spect",
"scrib",
"struct",
"mit",
"vers"};
535 vector<string> suffixes = {
"ion",
"ment",
"ness",
"able",
"ible",
536 "ful",
"less",
"ive",
"ous",
"al"};
538 for (
int i = 0; i < n; ++i)
540 string word = prefixes[i % prefixes.size()] + roots[(i / prefixes.size()) % roots.size()] +
541 suffixes[(i / (prefixes.size() * roots.size())) % suffixes.size()];
549 words.push_back(word);
552 cout <<
"\nGenerated " << words.
size() <<
" words for testing" << endl;
555 auto start = chrono::high_resolution_clock::now();
557 for (
const auto &word : words)
560 auto mid = chrono::high_resolution_clock::now();
564 for (
const auto &word : words)
568 auto end = chrono::high_resolution_clock::now();
570 auto insert_us = chrono::duration_cast<chrono::microseconds>(mid - start).count();
571 auto search_us = chrono::duration_cast<chrono::microseconds>(end - mid).count();
573 cout <<
"\nResults:" << endl;
574 cout <<
" Words in trie: " << trie.
count() << endl;
575 cout <<
" Insert time: " << insert_us <<
" us" << endl;
576 cout <<
" Search time: " << search_us <<
" us" << endl;
577 cout <<
" Found: " << found <<
"/" << words.size() << endl;
580 start = chrono::high_resolution_clock::now();
582 size_t total_matches = 0;
583 for (
const auto &
prefix : prefixes)
586 total_matches += matches.
size();
589 end = chrono::high_resolution_clock::now();
591 auto prefix_us = chrono::duration_cast<chrono::microseconds>(end - start).count();
593 cout <<
"\nPrefix search (10 prefixes):" << endl;
594 cout <<
" Time: " << prefix_us <<
" us" << endl;
595 cout <<
" Total matches: " << total_matches << endl;
598int main(
int argc,
char *argv[])
602 TCLAP::CmdLine
cmd(
"Trie (Prefix Tree) Example",
' ',
"1.0");
604 TCLAP::ValueArg<int> nArg(
"n",
"count",
"Number of words for performance test",
false, 1000,
606 TCLAP::SwitchArg basicArg(
"b",
"basic",
"Show basic operations",
false);
607 TCLAP::SwitchArg prefixArg(
"p",
"prefix",
"Show prefix search / autocomplete",
false);
608 TCLAP::SwitchArg spellArg(
"s",
"spell",
"Show spell checker example",
false);
609 TCLAP::SwitchArg cmdArg(
"c",
"commands",
"Show command autocomplete example",
false);
610 TCLAP::SwitchArg structArg(
"t",
"structure",
"Show trie structure visualization",
false);
611 TCLAP::SwitchArg perfArg(
"f",
"performance",
"Run performance analysis",
false);
612 TCLAP::SwitchArg allArg(
"a",
"all",
"Run all demos",
false);
623 cmd.parse(argc, argv);
625 int n = nArg.getValue();
626 bool runBasic = basicArg.getValue();
627 bool runPrefix = prefixArg.getValue();
628 bool runSpell = spellArg.getValue();
629 bool runCmd = cmdArg.getValue();
630 bool runStruct = structArg.getValue();
631 bool runPerf = perfArg.getValue();
632 bool runAll = allArg.getValue();
634 if (not runBasic
and not runPrefix
and not runSpell
and not runCmd
and not runStruct
and
638 cout <<
"=== Trie (Prefix Tree): Efficient String Storage ===" << endl;
640 if (runAll or runBasic)
643 if (runAll or runStruct)
646 if (runAll or runPrefix)
649 if (runAll or runSpell)
652 if (runAll or runCmd)
655 if (runAll or runPerf)
658 cout <<
"\n=== Summary ===" << endl;
659 cout <<
"Tries excel at:" << endl;
660 cout <<
" - Fast prefix searches (autocomplete)" << endl;
661 cout <<
" - Memory-efficient storage of strings with shared prefixes" << endl;
662 cout <<
" - O(k) operations where k = word length" << endl;
663 cout <<
"Use cases: autocomplete, spell checkers, IP routing, dictionaries" << endl;
667 catch (TCLAP::ArgException &e)
669 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() << endl;
std::string to_str() const
Convert the subtree to a string representation.
size_t size() const noexcept
Return the current dimension of array.
Owning prefix tree wrapper.
bool insert_word(const std::string &word)
Insert a word into the tree.
DynArray< std::string > words(const size_t max_word_length=2048) const
Get all words stored in the tree.
DynArray< std::string > words_with_prefix(const std::string &prefix, const size_t max_word_length=2048) const
Get all words with a given prefix.
Cnode * root() noexcept
Return the mutable root node.
size_t count() const noexcept
Count the words stored in the tree.
bool contains(const std::string &word) const noexcept
Check whether a word exists in the tree.
size_t size() const noexcept
Return the number of words stored in the tree.
Main namespace for Aleph-w library functions.
static void prefix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
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.