8 questions. Covers Debugging lab: trie bugs.
Questions 1 to 5 of 8
cpp void insert(const std::string& word) { TrieNode* node = root_.get(); for (char c : word) { int idx = c - 'A'; auto& next = node->children.at(idx); if (!next) next = std::make_unique<TrieNode>(); node = next.get(); } node->is_end = true; }
c - 'A' + 1
std::array
at()
'A'
t
out_of_range
cpp void insert(const std::string& word) { TrieNode* node = root_.get(); for (char c : word) { auto& next = node->children[c - 'a']; if (!next) { next = std::make_unique<TrieNode>(); if (c == word.back()) next->is_end = true; } node = next.get(); } }
cpp bool search(const std::string& word) const { const TrieNode* node = root_.get(); for (char c : word) { node = node->children.at(c - 'a').get(); } return node->is_end; }
c - 'A'
cpp bool search(const std::string& word) const { const TrieNode* node = root_.get(); for (char c : word) { const TrieNode* next = node->children[c - 'a'].get(); if (next == nullptr) return false; node = next; } return node != nullptr; }
cpp #include <iostream> #include <string> using namespace std; int main() { string word = "tea"; for (char c : word) { cout << (c - 'a') << (c == word.back() ? '\n' : ','); } }
5 question(s) left on this page.