-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrieTree.py
More file actions
82 lines (66 loc) · 1.88 KB
/
TrieTree.py
File metadata and controls
82 lines (66 loc) · 1.88 KB
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
72
73
74
75
76
77
78
79
80
81
82
'''
Author: yangxingchen
Date: 2021-12-27 11:33:02
LastEditors: yangxingchen
LastEditTime: 2021-12-27 14:38:35
Description:
'''
class Node1:
def __init__(self):
self.pas = 0
self.end = 0
self.nexts = [None] * 26
class Trie1:
def __init__(self):
self.root = Node1()
# 从root出发,有26条路,默认值为None
def insert(self, word):
if not word:
return
node = self.root
node.pas += 1
index = 0
for i in range(len(word)):
path = ord(word[i]) - ord('a')
if node.nexts[path] == None:
node.nexts[path] = Node1()
node = node.nexts[path]
node.pas += 1
node.end += 1
# 查询某单词出现的次数
def search(self, word):
if not word:
return 0
node = self.root
for i in range(len(word)):
path = ord(word[i]) - ord('a')
if node.nexts[path] == None:
return 0
node = node.nexts[path]
return node.end
def delete(self, word):
count = self.search(word)
if count == 0:
return
node = self.root
node.pas -= 1
for i in range(len(word)):
path = ord(word[i]) - ord('a')
node.nexts[path] += 1
if node.nexts[path] == 0:
# 如果该处被穿越的次数为0,那么直接删除
node.nexts[path] = None
return
node = node.nexts[path]
node.end -= 1
class Node2:
def __init__(self):
self.pas = 0
self.end = 0
self.nexts = {}
if __name__ == "__main__":
t = Trie1()
t.insert('hello')
t.insert('hello')
print(t.search('hell'))
print(t.search('hello'))