TrieNode: /** Key character of this trie node */ char key_char_; /** whether this node marks the end of a key */ bool is_end_{false}; /** A map of all child nodes of this trie node, which can be accessed by each * child node's key char. */ std::unordered_map<char, std::unique_ptr<TrieNode>> children_;
TrieNodeWithValue: 表示终结节点,继承自TrieNode,表示终结节点,带有一个 T 类型的 value
Trie: 表示整个 Trie 树
1 2 3 4 5
Trie: /* Root node of the trie */ std::unique_ptr<TrieNode> root_; /* Read-write lock for the trie */ ReaderWriterLatch latch_;
/** * TODO(P0): Add implementation * * @brief Construct a new Trie Node object with the given key char. * is_end_ flag should be initialized to false in this constructor. * * @param key_char Key character of this trie node */ explicit TrieNode(char key_char) { this->key_char_ = key_char; this->is_end_ = false; }
/** * TODO(P0): Add implementation * * @brief Move constructor for trie node object. The unique pointers stored * in children_ should be moved from other_trie_node to new trie node. * * @param other_trie_node Old trie node. */ TrieNode(TrieNode &&other_trie_node) noexcept { this->key_char_ = other_trie_node.key_char_; this->is_end_ = other_trie_node.is_end_; this->children_ = std::move(other_trie_node.children_); // this? }
// Construct a new TrieNodeWithValue TrieNodeWithValue(char key_char, T value) : TrieNode(key_char), value_(value) { is_end_ = true; } // Construct a new TrieNodeWithValue object from a TrieNode object and specify its value. TrieNodeWithValue(TrieNode &&trieNode, T value) : TrieNode(std::move(trieNode)), value_(value) { is_end_ = true; }