Skip to content

Commit 36cf54b

Browse files
authored
Add files via upload
1 parent 8eb0c20 commit 36cf54b

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Trie/Trie_add_Search_using_dict.ipynb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 18,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"class Trie:\n",
10+
" root = {}\n",
11+
" \n",
12+
" def add(self, word):\n",
13+
" cur = self.root\n",
14+
" \n",
15+
" for ch in word:\n",
16+
" if ch not in cur:\n",
17+
" cur[ch] = {}\n",
18+
" cur = cur[ch]\n",
19+
" cur['*'] = True\n",
20+
" \n",
21+
" def search(self,word):\n",
22+
" cur = self.root\n",
23+
" \n",
24+
" for ch in word:\n",
25+
" if ch not in cur:\n",
26+
" return False\n",
27+
" cur = cur[ch]\n",
28+
" if '*' in cur:\n",
29+
" return True\n",
30+
" else:\n",
31+
" return False\n",
32+
" \n",
33+
" "
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 21,
39+
"metadata": {},
40+
"outputs": [
41+
{
42+
"name": "stdout",
43+
"output_type": "stream",
44+
"text": [
45+
"True\n",
46+
"False\n",
47+
"True\n",
48+
"True\n",
49+
"True\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"dictionary = Trie()\n",
55+
"dictionary.add(\"pqr\")\n",
56+
"dictionary.add(\"pqrst\")\n",
57+
"print(dictionary.search(\"pqr\"))\n",
58+
"print(dictionary.search(\"pq\"))\n",
59+
"print(dictionary.search(\"p\"))\n",
60+
"print(dictionary.search(\"pqrst\"))\n",
61+
"dictionary.add(\"p\")\n",
62+
"print(dictionary.search(\"p\"))"
63+
]
64+
}
65+
],
66+
"metadata": {
67+
"kernelspec": {
68+
"display_name": "Python 3",
69+
"language": "python",
70+
"name": "python3"
71+
},
72+
"language_info": {
73+
"codemirror_mode": {
74+
"name": "ipython",
75+
"version": 3
76+
},
77+
"file_extension": ".py",
78+
"mimetype": "text/x-python",
79+
"name": "python",
80+
"nbconvert_exporter": "python",
81+
"pygments_lexer": "ipython3",
82+
"version": "3.7.6"
83+
}
84+
},
85+
"nbformat": 4,
86+
"nbformat_minor": 4
87+
}

0 commit comments

Comments
 (0)