Skip to content

Commit 9789c94

Browse files
committed
Create 83. 构建Trie.md
1 parent 8c82af0 commit 9789c94

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

83. 构建Trie.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```
2+
class TrieNode():
3+
def __init__(self, val=None):
4+
self.val = val
5+
self.isEnd = False
6+
self.children = {}
7+
8+
class Trie:
9+
def __init__(self):
10+
self.root = TrieNode()
11+
12+
def insert(self, word):
13+
cur = self.root
14+
for c in word:
15+
if c not in cur.children:
16+
cur.children[c] = TrieNode(c)
17+
cur = cur.children[c]
18+
cur.isEnd = True
19+
20+
def search(self, word):
21+
cur = self.root
22+
for c in word:
23+
if c not in cur.children:
24+
return False
25+
cur = cur.children[c]
26+
return cur.isEnd
27+
28+
def delete(self, word):
29+
if self.search(word):
30+
cur = self.root
31+
for c in word:
32+
cur = cur.children[c]
33+
cur.isEnd = False
34+
```

0 commit comments

Comments
 (0)