Leetcode 208. 实现 Trie (前缀树)

地址 https://leetcode-cn.com/problems/implement-trie-prefix-tree/

Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。

请你实现 Trie 类:
Trie() 初始化前缀树对象。
void insert(String word) 向前缀树中插入字符串 word 。
boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

示例:

输入
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输出
[null, null, true, false, true, null, true]

解释
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple");   // 返回 True
trie.search("app");     // 返回 False
trie.startsWith("app"); // 返回 True
trie.insert("app");
trie.search("app");     // 返回 True
 

提示:
1 <= word.length, prefix.length <= 2000
word 和 prefix 仅由小写英文字母组成
insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次

解答
题目已经解释了 使用前缀树进行解答
前缀树使用一个node节点记录一个出现的字符

struct NODE {
		bool wordFinish;
		struct NODE* next[26];
	};

每个node节点 有26个next的node指针对应当前字母在字符串中的后继字母。 使用一个标记记录当前node代表的字母是否是某一个字符串的结尾。
那么一个字符串的组成就是前缀树从根节点root走到某个节点的路径
如图
image

如果多个字符串的前几个字母是相同的,那么他们在前缀树中的记录节点和路径时一致的不必重复记录。

插入字符串的处理如下
假设字符串有n个字母,第一个字母就查看根节点的next的指针中是否已经指向该字母的node,已经指向则直接使用,没有指向则创建该节点并且让根节点的对应字母的next指针指向它。
后面的字母也如上处理。

查询则是从根节点按照字符串的字母一层层的检测该字母的node节点是否存在,不存在则说明该字符串未出现,如果存在,还需要查看字符串最后一个字母在前缀树中是否有结束标记。
有,则说明该字符串出现过。 无,则说明该字符串未出现,但是将该字符串作为前缀的字母串有出现

那么题目的处理代码如下

class Trie {
public:
	/** Initialize your data structure here. */
	struct NODE {
		char l;
		bool wordFinish;
		struct NODE* next[26];
	};

	struct NODE* root;

	Trie() {
		root = new struct NODE();
	}

	/** Inserts a word into the trie. */
	void insert(string word) {
		struct NODE* curr = root;
		for (int i = 0; i < word.size(); i++) {
			char c = word[i];
			if (curr->next[c - 'a'] == NULL) {
				curr->next[c - 'a'] = new struct NODE();
			}
			curr = curr->next[c - 'a'];
			curr->l = c;
			if (i == word.size() - 1) {
				curr->wordFinish = true;
			}
		}
	}

	/** Returns if the word is in the trie. */
	bool search(string word) {
		struct NODE* curr = root;
		for (int i = 0; i < word.size(); i++) {
			char c = word[i];
			if (curr->next[c - 'a'] == NULL) return false;
			else {
				curr = curr->next[c - 'a'];
				if (i == word.size() - 1 && curr->wordFinish == true) {
					return true;
				}
				else if(i == word.size() - 1){
					return false;
				}
			}
		}

		return false;
	}

	/** Returns if there is any word in the trie that starts with the given prefix. */
	bool startsWith(string prefix) {
		struct NODE* curr = root;
		for (int i = 0; i < prefix.size(); i++) {
			char c = prefix[i];
			if (curr->next[c - 'a'] == NULL) return false;
			else {
				curr = curr->next[c - 'a'];
				if (i == prefix.size() - 1 ) {
					return true;
				}
			}
		}

		return false;
	}
};

我的视频题解空间

作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
阿里打赏 微信打赏
原文地址:https://www.cnblogs.com/itdef/p/15203330.html