-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.py
71 lines (59 loc) · 1.91 KB
/
Trie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
@version: python3.6
@author: Fieldhunter
@contact: 1677532160yuan@gmail.com
@time: 2020-05-03
"""
import functools
class Trie_Node():
"""
The string types stored in the Trie contain
only 26 English lowercase letters.
"""
def __init__(self, string):
self.data = string
self.next = [None] * 26
self.end = False
class Trie():
def __init__(self):
self.__head = Trie_Node(None)
def add_data(self, string):
pointer = self.__head
for character in string:
character_ascll = ord(character) - 97
if pointer.next[character_ascll] == None:
new_node = Trie_Node(character)
pointer.next[character_ascll] = new_node
pointer = pointer.next[character_ascll]
pointer.end = True
def find_data(self, string):
pointer = self.__head
find = False
for character in string:
character_ascll = ord(character) - 97
if pointer.next[character_ascll] == None:
break
pointer = pointer.next[character_ascll]
if character == string[-1]:
if pointer.end == True:
find = True
if find:
print("Find target data")
else:
print("No target data")
"""
Check if the code used to access the self.__head, Decorator function.
The purpose of simply adding code is to prevent Trie from
being tampered with maliciously and to provide the API for developers.
"""
def __check_code(func):
@functools.wraps(func)
def check(self, code):
if code != 'adsf;{h3096j34ka`fd>&/edgb^45:6':
raise Exception('code is wrong!')
result = func(self, code)
return result
return check
@__check_code
def return_head(self, code):
return self.__head