Skip to content

Commit 0369179

Browse files
committed
Added tries
1 parent 455230d commit 0369179

File tree

3 files changed

+152
-153
lines changed

3 files changed

+152
-153
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Completed
3232
- Binary Search Tree
3333
- Kandane's Algorithm
3434
- Knapsack Problem (0/1 and unbounded)
35+
- Prefix Tries
36+
3537

3638
Tests
3739
---

trees/bst.py

Lines changed: 0 additions & 153 deletions
This file was deleted.

trees/trie.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
""" Tries in python
2+
Methods - insert_key(k, v)
3+
has_key(k)
4+
retrie_val(k)
5+
start_with_prefix(prefix)
6+
"""
7+
# HELPERS #
8+
def _get_child_branches(tr):
9+
if tr == []:
10+
return []
11+
return tr[1:]
12+
13+
def _get_child_branch(tr, c):
14+
for branch in _get_child_branches(tr):
15+
if branch[0] == c:
16+
return branch
17+
return None
18+
19+
def _retrive_branch(k, trie_list):
20+
if k == "":
21+
return None
22+
tr = trie_list
23+
for c in k:
24+
child_branch = _get_child_branch(tr, c)
25+
if not child_branch:
26+
return None
27+
tr = child_branch
28+
return tr
29+
30+
def _is_trie_bucket(bucket):
31+
if len(bucket) != 2:
32+
return False
33+
if type(bucket[1]) is tuple:
34+
return True
35+
36+
def _get_bucket_key(bucket):
37+
if not _is_trie_bucket(bucket):
38+
return None
39+
return bucket[1][0]
40+
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
49+
50+
# RETRIE_VAL
51+
def retrie_val(k, tr):
52+
if k == "":
53+
return None
54+
key_tuple = _retrive_branch(k, tr)
55+
if not key_tuple:
56+
return None
57+
return key_tuple[1]
58+
59+
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
77+
78+
79+
def start_with_prefix(prefix, trie):
80+
branch = _retrive_branch(prefix, trie)
81+
if not branch:
82+
return []
83+
prefix_list = []
84+
q = branch[1:]
85+
while q:
86+
curr_branch = q.pop(0)
87+
if _is_trie_bucket(curr_branch):
88+
prefix_list.append(_get_bucket_key(curr_branch))
89+
else:
90+
q.extend(curr_branch[1:])
91+
return prefix_list
92+
93+
94+
if __name__ == "__main__":
95+
trie = [[]]
96+
states = """
97+
Alabama
98+
Alaska
99+
Arizona
100+
Arkansas
101+
California
102+
Colorado
103+
Connecticut
104+
Delaware
105+
Florida
106+
Georgia
107+
Hawaii
108+
Idaho
109+
Illinois
110+
Indiana
111+
Iowa
112+
Kansas
113+
Kentucky
114+
Louisiana
115+
Maine
116+
Maryland
117+
Massachusetts
118+
Michigan
119+
Minnesota
120+
Mississippi
121+
Missouri
122+
Montana
123+
Nebraska
124+
Nevada
125+
New Hampshire
126+
New Jersey
127+
New Mexico
128+
New York
129+
North Carolina
130+
North Dakota
131+
Ohio
132+
Oklahoma
133+
Oregon
134+
Pennsylvania
135+
Rhode Island
136+
South Carolina
137+
South Dakota
138+
Tennessee
139+
Texas
140+
Utah
141+
Vermont
142+
Virginia
143+
Washington
144+
West Virginia
145+
Wisconsin
146+
Wyoming"""
147+
states_list = [w.strip().lower() for w in states.splitlines() if w]
148+
for state in states_list:
149+
insert_key(state, True, trie)
150+
print start_with_prefix("new", trie)

0 commit comments

Comments
 (0)