219#include <tclap/CmdLine.h>
222using namespace Aleph;
229 cout <<
"\n" << string(60,
'=') <<
endl;
230 cout <<
"Trie: Basic Operations" <<
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)
247 cout <<
"\nTrying to insert duplicate:" <<
endl;
248 cout <<
" cat -> " << (
root.insert_word(
"cat") ?
"inserted" :
"already exists") <<
endl;
253 cout <<
"Searching for words:" <<
endl;
260 cout <<
"\n--- Statistics ---" <<
endl;
263 cout <<
"\n--- All Words ---" <<
endl;
264 cout <<
"Words in lexicographic order:" <<
endl;
280 cout <<
"\n" << string(60,
'=') <<
endl;
281 cout <<
"Prefix Search: Autocomplete Feature" <<
endl;
288 "apple",
"application",
"apply",
"approach",
290 "banana",
"band",
"bandana",
"bank",
"banner",
291 "car",
"card",
"care",
"careful",
"careless",
"career",
292 "cart",
"cartoon",
"carton"
299 cout <<
"\n--- Prefix Search Demo ---" <<
endl;
321 cout <<
"\n--- Simulating Autocomplete ---" <<
endl;
324 cout <<
"\nTyping simulation (showing suggestions):" <<
endl;
335 if (i > 0)
cout <<
", ";
358 cout <<
"\n" << string(60,
'=') <<
endl;
359 cout <<
"Practical Example: Simple Spell Checker" <<
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"
379 cout <<
"\n--- Spell Check Demo ---" <<
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;
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"
450 cout <<
"\n--- Tab Completion Simulation ---" <<
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;
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;
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;
542 vector<string> roots = {
"act",
"form",
"port",
"ject",
"duct",
"spect",
"scrib",
"struct",
"mit",
"vers"};
545 for (
int i = 0; i < n; ++i)
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();
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;
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.
size_t size() const noexcept
Count the number of elements of the list.
size_t length() const noexcept
Count the number of elements of a container.
__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.
static void prefix(Node *root, DynList< Node * > &acc)
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.