Skip to content

Commit c5e7f80

Browse files
committed
Merge pull request prakhar1989#18 from Xuefeng-Zhu/patch-11
Improve trie
2 parents 48af920 + 2d6548e commit c5e7f80

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

trees/trie.py

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,100 @@
1-
""" Tries in python
1+
""" Tries in python
22
Methods - insert_key(k, v)
33
has_key(k)
44
retrie_val(k)
55
start_with_prefix(prefix)
66
"""
7-
# HELPERS #
8-
def _get_child_branches(tr):
9-
if tr == []:
10-
return []
11-
return tr[1:]
127

13-
def _get_child_branch(tr, c):
14-
for branch in _get_child_branches(tr):
8+
9+
def _get_child_branches(trie):
10+
"""
11+
Helper method for getting branches
12+
"""
13+
return trie[1:]
14+
15+
16+
def _get_child_branch(trie, c):
17+
"""
18+
Get branch matching the character
19+
"""
20+
for branch in _get_child_branches(trie):
1521
if branch[0] == c:
1622
return branch
23+
1724
return None
1825

19-
def _retrive_branch(k, trie_list):
20-
if k == "":
26+
27+
def _retrive_branch(k, trie):
28+
"""
29+
Get branch matching the key word
30+
"""
31+
if not k:
2132
return None
22-
tr = trie_list
33+
2334
for c in k:
24-
child_branch = _get_child_branch(tr, c)
35+
child_branch = _get_child_branch(trie, c)
2536
if not child_branch:
2637
return None
27-
tr = child_branch
28-
return tr
38+
trie = child_branch
39+
40+
return trie
41+
2942

3043
def _is_trie_bucket(bucket):
3144
if len(bucket) != 2:
3245
return False
33-
if type(bucket[1]) is tuple:
34-
return True
46+
47+
return type(bucket[1]) is tuple
48+
3549

3650
def _get_bucket_key(bucket):
3751
if not _is_trie_bucket(bucket):
3852
return None
39-
return bucket[1][0]
4053

41-
# HAS_KEY #
42-
def has_key(k, tr):
43-
if k == "":
44-
return None
45-
key_tuple = _retrive_branch(k, tr)
46-
if not key_tuple:
47-
return False
48-
return True
54+
return bucket[1][0]
4955

50-
# RETRIE_VAL
51-
def retrie_val(k, tr):
52-
if k == "":
53-
return None
54-
key_tuple = _retrive_branch(k, tr)
56+
57+
def has_key(k, trie):
58+
"""
59+
Check if trie contain the key word
60+
"""
61+
return _retrive_branch(k, trie) is not None
62+
63+
64+
def retrie_val(k, trie):
65+
key_tuple = _retrive_branch(k, trie)
5566
if not key_tuple:
5667
return None
68+
5769
return key_tuple[1]
5870

5971

60-
def insert_key(key, v, trie_list):
61-
if key == "":
62-
return None
63-
elif has_key(key, trie_list):
64-
return None
65-
else:
66-
tr = trie_list
67-
for char in key:
68-
branch = _get_child_branch(tr, char)
69-
if branch == None:
70-
new_branch = [char]
71-
tr.append(new_branch)
72-
tr = new_branch
73-
else:
74-
tr = branch
75-
tr.append((key, v))
76-
return None
72+
def insert_key(key, v, trie):
73+
"""
74+
Insert a (key, value) pair into trie
75+
"""
76+
if not key or has_key(key, trie):
77+
return
78+
79+
for char in key:
80+
branch = _get_child_branch(trie, char)
81+
if not branch:
82+
new_branch = [char]
83+
trie.append(new_branch)
84+
trie = new_branch
85+
else:
86+
trie = branch
87+
trie.append((key, v))
7788

7889

7990
def start_with_prefix(prefix, trie):
91+
"""
92+
Find words start with prefix
93+
"""
8094
branch = _retrive_branch(prefix, trie)
8195
if not branch:
8296
return []
97+
8398
prefix_list = []
8499
q = branch[1:]
85100
while q:
@@ -88,6 +103,7 @@ def start_with_prefix(prefix, trie):
88103
prefix_list.append(_get_bucket_key(curr_branch))
89104
else:
90105
q.extend(curr_branch[1:])
106+
91107
return prefix_list
92108

93109
if __name__ == "__main__":
@@ -142,7 +158,7 @@ def start_with_prefix(prefix, trie):
142158
Washington
143159
West Virginia
144160
Wisconsin
145-
Wyoming"""
161+
Wyoming"""
146162
states_list = [w.strip().lower() for w in states.splitlines() if w]
147163
for state in states_list:
148164
insert_key(state, True, trie)

0 commit comments

Comments
 (0)