implement-trie-prefix-tree 1.0.0
Implement Trie (Prefix Tree)
Loading...
Searching...
No Matches
Trie Class Reference
Collaboration diagram for Trie:

Public Member Functions

 Trie ()
 
void insert (string word)
 
bool search (string word)
 
bool startsWith (string prefix)
 

Private Attributes

vector< unique_ptr< trie_node > > tree
 
vector< bool > tree_beginnings
 

Detailed Description

Definition at line 35 of file main.cpp.

Constructor & Destructor Documentation

◆ Trie()

Trie::Trie ( )
inline

Definition at line 42 of file main.cpp.

42 : tree(), tree_beginnings(26, false)
43 {
44 tree.reserve(26);
45 for (char c = 'a'; c <= 'z'; ++c)
46 tree.push_back(make_unique<trie_node>(c));
47 }
vector< unique_ptr< trie_node > > tree
Definition main.cpp:38
vector< bool > tree_beginnings
Definition main.cpp:39

References tree.

Member Function Documentation

◆ insert()

void Trie::insert ( string  word)
inline

Definition at line 49 of file main.cpp.

50 {
51 if (word.empty())
52 return;
53 trie_node* node = tree[word[0] - 'a'].get();
54 tree_beginnings[word[0] - 'a'] = true;
55 for (int i = 1; i < (int)word.length(); ++i)
56 {
57 trie_node* child = node->find(word[i]);
58 if (child == nullptr)
59 child = node->add_child(word[i]);
60 node = child;
61 }
62 node->set_last();
63 }
Definition for trie tree node.
Definition main.cpp:9
trie_node * add_child(char c)
Definition main.cpp:17
bool set_last(bool last=true)
Definition main.cpp:32
trie_node * find(char c)
Definition main.cpp:24

References trie_node::add_child(), trie_node::find(), trie_node::set_last(), tree, and tree_beginnings.

◆ search()

bool Trie::search ( string  word)
inline

Definition at line 65 of file main.cpp.

66 {
67 if (word.empty())
68 return false;
69 trie_node* node = tree[word[0] - 'a'].get();
70 if ((int)word.length() == 1)
71 return node->is_last() && tree_beginnings[word[0] - 'a'];
72 for (int i = 1; i < (int)word.length(); ++i)
73 {
74 trie_node* child = node->find(word[i]);
75 if (child == nullptr)
76 return false;
77 node = child;
78 }
79 return node->is_last();
80 }
bool is_last()
Definition main.cpp:31

References trie_node::find(), trie_node::is_last(), tree, and tree_beginnings.

◆ startsWith()

bool Trie::startsWith ( string  prefix)
inline

Definition at line 82 of file main.cpp.

83 {
84 if (prefix.empty())
85 return true;
86 trie_node* node = tree[prefix[0] - 'a'].get();
87 if ((int)prefix.length() == 1)
88 return tree_beginnings[prefix[0] - 'a'];
89 for (int i = 1; i < (int)prefix.length(); ++i)
90 {
91 trie_node* child = node->find(prefix[i]);
92 if (child == nullptr)
93 return false;
94 node = child;
95 }
96 return true;
97 }

References trie_node::find(), tree, and tree_beginnings.

Field Documentation

◆ tree

vector<unique_ptr<trie_node> > Trie::tree
private

Definition at line 38 of file main.cpp.

Referenced by insert(), search(), startsWith(), and Trie().

◆ tree_beginnings

vector<bool> Trie::tree_beginnings
private

Definition at line 39 of file main.cpp.

Referenced by insert(), search(), and startsWith().


The documentation for this class was generated from the following file: