Cover image for Interview Classic 150 Problem P208: Implement Trie (Prefix Tree)

Interview Classic 150 Problem P208: Implement Trie (Prefix Tree)


Timeline

Timeline

2025-11-24

init

Trie

Problem:

Trie implementation, note that you must have is_end to determine whether it is the end of a word.
Actually, you can directly use a vector to store, because there are only 26 letters, just convert letters to numbers.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
#include <string>#include <vector>#include <unordered_map>using std::vector;using std::string;using std::unordered_map;typedef struct TrieNode {	//vector<struct TrieNode *> children;	unordered_map<char, struct TrieNode *> children;	char data;	bool is_end;} TrieNode;class Trie {    private:	TrieNode *root;    public:	Trie()	{		root = new TrieNode;		root->children = unordered_map<char, TrieNode *>();		root->is_end = false;	}	void insert(string word)	{		TrieNode *p = root, *node;		int i = 0, n = word.size();		char ch;		for (i = 0; i < n; i++) {			ch = word[i];			if (p->children.count(ch)) {				p = p->children[ch];				if (i == n - 1) {					p->is_end = true;				}				continue;			}			node = new TrieNode;			node->children = unordered_map<char, TrieNode *>();			node->data = ch;			if (i == n - 1) {				node->is_end = true;			} else {				node->is_end = false;			}			p->children[ch] = node;			p = node;		}	}	bool search(string word)	{		int i, n = word.size();		char ch;		TrieNode *p = root;		for (i = 0; i < n; i++) {			ch = word[i];			if (p->children.count(ch)) {				p = p->children[ch];				continue;			}			return false;		}		return p->is_end;	}	bool startsWith(string prefix)	{		int i, n = prefix.size();		char ch;		TrieNode *p = root;		for (i = 0; i < n; i++) {			ch = prefix[i];			if (p->children.count(ch)) {				p = p->children[ch];				continue;			}			return false;		}		return true;	}};/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */

leetcode hot 100 rewrite

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
#include <string>#include <vector>using std::string;using std::vector;struct TrieNode {        char val;        vector<struct TrieNode *> children;        bool is_end;        TrieNode()        {                this->children = vector<struct TrieNode *>(26, nullptr);        }};class Trie {    private:        TrieNode *root;    public:        Trie()        {                root = new TrieNode;        }        void insert(string word)        {                TrieNode *p = root;                for (char ch : word) {                        if (p->children[ch - 'a'] == nullptr) {                                p->children[ch - 'a'] = new TrieNode;                                p->children[ch - 'a']->val = ch;                                p->children[ch - 'a']->is_end = false;                        }                        p = p->children[ch - 'a'];                }                p->is_end = true;        }        bool search(string word)        {                TrieNode *p = root;                for (char ch : word) {                        if (p->children[ch - 'a'])                                p = p->children[ch - 'a'];                        else                                return false;                }                return p->is_end;        }        bool startsWith(string prefix)        {                TrieNode *p = root;                for (char ch : prefix) {                        if (p->children[ch - 'a'])                                p = p->children[ch - 'a'];                        else                                return false;                }                return true;        }};/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */
Loading comments…