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;
257 cout <<
"Searching for words:" <<
endl;
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;
270 for (
size_t i = 0; i <
all_words.size(); ++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;
290 "aptitude",
"banana",
"band",
"bandana",
"bank",
291 "banner",
"car",
"card",
"care",
"careful",
292 "careless",
"career",
"cart",
"cartoon",
"carton"};
298 cout <<
"\n--- Prefix Search Demo ---" <<
endl;
304 cout <<
"\nPrefix '" <<
prefix <<
"' matches:" <<
endl;
309 cout <<
" (no matches)" <<
endl;
312 for (
size_t i = 0; i <
matches.size(); ++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): ";
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;
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"};
377 cout <<
"\n--- Spell Check Demo ---" <<
endl;
383 cout <<
"\nChecking: '" <<
word <<
"'" <<
endl;
387 cout <<
" Status: Correct!" <<
endl;
391 cout <<
" Status: Not found - might be misspelled" <<
endl;
401 cout <<
" Did you mean: ";
423 cout <<
"\n" << string(60,
'=') <<
endl;
424 cout <<
"Practical Example: Shell Command Autocomplete" <<
endl;
425 cout << string(60,
'=') <<
endl;
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"};
441 cout <<
"\n--- Tab Completion Simulation ---" <<
endl;
447 cout <<
"\n$ " <<
input <<
"<TAB>" <<
endl;
452 cout <<
" (no completions)" <<
endl;
456 cout <<
" -> " << c <<
" (unique match)" <<
endl;
460 cout <<
" Possible completions: ";
478 cout <<
"\n" << string(60,
'=') <<
endl;
479 cout <<
"Trie Structure Visualization" <<
endl;
480 cout << string(60,
'=') <<
endl;
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;
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;
534 "spect",
"scrib",
"struct",
"mit",
"vers"};
536 "ful",
"less",
"ive",
"ous",
"al"};
538 for (
int i = 0; i < n; ++i)
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;
577 cout <<
" Found: " << found <<
"/" << words.size() <<
endl;
580 start = chrono::high_resolution_clock::now();
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;
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);
625 int n =
nArg.getValue();
638 cout <<
"=== Trie (Prefix Tree): Efficient String Storage ===" <<
endl;
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;
Owning prefix tree wrapper.
bool insert_word(const std::string &word)
Insert a word into the tree.
size_t size() const noexcept
Return the number of words stored in the tree.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
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.