Trie树
''' trie树 联想词 查询 '''
inputFile='input.txt'
#TrieTree 节点
class Node:
''' 一层的节点 hashmap={"杨":[count,childNode],"李":[count,childNode]} '''
def __init__(self):
self.hashmap={}
self.blank=True
def get(self,chars):
return self.hashmap.get(chars,None)
''' 遍历 '''
def dfs(self,char):
#print(char)
child=self.get(char)
if not child:
return None
#result=[]
#temp=[]
return self._dfs(child[1])
def _dfs(self,cls):
result=[]
temp=[]
for k,v in cls.hashmap.items():
#print(k)
temp=[]
#temp.append(k)
if not v[1].blank:
rr=self._dfs(v[1])
for r in rr:
temp.append(k+r)
else:
temp.append(k)
result+=temp
#print(result)
return result
''' 一个已存在的键值 插入 '''
def update(self,char):
self.hashmap[char][0]+=1
''' 尚未存在该键值 '''
def append(self,char):
self.hashmap[char]=[1,None]
self.blank=False
''' 添加新的键值 '''
def insert(self,char):
if self.get(char):
self.update(char)
else:
self.append(char)
''' 返回子结点 '''
def child(self,char):
n=self.hashmap[char][1]
if n:
return n
self.hashmap[char][1]=Node()
return self.hashmap[char][1]
class TrieTree:
def __init__(self):
print("加载数据")
f=open(inputFile,'r',encoding='utf-8')
self.data=f.readlines()
f.close()
print("数据加载完毕...")
print("共 %s 条数据"%len(self.data))
self.setup()
self.construct()
#数据结构
''' 存储方式: 结点->结点 每个节点一个hash '''
def setup(self):
self.treeNode=Node()
''' 输入的数据构建trie树 '''
def construct(self):
for s in self.data:
#print(s)
self.insert(s)
''' 插入单个字符串 '''
def insert(self,name):
node=self.treeNode
for n in name:
node.insert(n)
node=node.child(n)
''' 查找 '''
def find(self,char):
node=self.treeNode
return self.treeNode.dfs(char)
if __name__=='__main__':
trie=TrieTree()
while True:
s=input("首字母\n")
r=trie.find(s)
if not r:
print("数据库尚未包含")
else:
for rr in r:
print(s+rr)