重点知识
字典树也称作前缀树或者Trie树,是一种哈希树的变种。
典型应用:用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。
优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
字典树的特点
根节点不包含字符,除根节点外每一个节点都只包含一个字符;
从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串;
每个节点的所有子节点包含的字符都不相同。
字典树数据结构的实现方法
字典树要实现以下两种基本功能:
- 插入单词
- 搜索单词
- 搜索前缀
练习
class MyPrefixTree:
def __init__(self):
self.trees = {}
def search(self, word: str):
my_tree = self.trees
for s in word:
if s not in my_tree:
return False
my_tree = my_tree[s]
if my_tree.get("is_word"):
return True
return False
def insert(self, word: str):
my_tree = self.trees
for s in word:
if s in my_tree:
my_tree = my_tree[s]
my_tree["is_word"] = False
else:
my_tree[s] = {"is_word": False}
my_tree = my_tree[s]
if len(my_tree) == 1:
my_tree["is_word"] = True
def search_prefix(self, prefix: str):
my_tree = self.trees
step = 0
for s in prefix:
if s not in my_tree:
return False, step
my_tree = my_tree[s]
step += 1
if not my_tree.get("is_word"):
step = 0
return True, step
class Solution:
def minimumLengthEncoding(self, words: list[str]) -> int:
my_prefix_tree = MyPrefixTree()
for word in set(words):
reversed_w = word[-1:-len(word)-1:-1]
my_prefix_tree.insert(reversed_w)
ret = 0
for word in set(words):
reversed_w = word[-1:-len(word) - 1:-1]
_, step = my_prefix_tree.search_prefix(reversed_w)
if step:
ret += step
ret += 1
return ret