Trie树构造与查找

trie树即字典树,前缀树

 1 #!/usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 '''
 4 Description:
 5 Created on 2015年9月21日
 6 @author: zenwan
 7 @version: 
 8 '''  
 9 li = ['自然','语言','处理']
10 class Node():  
11     def __init__(self,key = None,nextnode = None):  
12         self.key = key
13         self.nextnode = nextnode
14 #构造
15 def cre_node():
16     trie = Node()
17     p=trie
18     #trie1 = Node('a',Node('b',Node('c',None)))
19     for item in li:
20         p.key = item
21         p.nextnode = Node()
22         p=p.nextnode
23     return trie    
24 #查找
25 def findnode(trie):
26     if trie == None:
27         return
28     else:
29         print trie.key
30         findnode(trie.nextnode)
31 trie = cre_node()
32 findnode(trie)

 

posted @ 2015-09-24 14:45  zeNlp  阅读(250)  评论(0编辑  收藏  举报