|
1 |
| -"""Trie DS implementation in Python""" |
| 1 | +""" |
| 2 | +Inputs: |
| 3 | +1nd line no of word you want to store in Trie (int) |
| 4 | +2rd line to (2+1st line input)th line (string) |
| 5 | +Next line enter number of words want to search |
| 6 | +then enter strings in each line |
| 7 | +
|
| 8 | +""" |
2 | 9 | class Trie:
|
3 |
| - root = {} |
| 10 | + root = {} #main dictionary of Trie |
4 | 11 |
|
5 |
| - def add(self, word): |
6 |
| - cur = self.root |
| 12 | + def add(self, word): # This funtion adds new word to Trie |
| 13 | + cur = self.root # cur is iterator to check whether new key/paths are required to add new word and root is main dictionary |
7 | 14 |
|
8 |
| - for ch in word: |
9 |
| - if ch not in cur: |
10 |
| - cur[ch] = {} |
11 |
| - cur = cur[ch] |
12 |
| - cur['*'] = True |
| 15 | + for ch in word: # ch is all the charactors in the word |
| 16 | + if ch not in cur: # if charactor is not present add new key/path initiate it empty |
| 17 | + cur[ch] = {} |
| 18 | + cur = cur[ch] # now go to the key that was created or was present |
| 19 | + cur['*'] = True # set * key to true at the end of each word |
| 20 | + #so larger the word the dictionary nesting will take place and * key represant the end state of the word |
13 | 21 |
|
14 |
| - def search(self,word): |
15 |
| - cur = self.root |
| 22 | + def search(self,word): # This function searches whether the word is presant or not presant |
| 23 | + cur = self.root #cur is iterator to check whether new key/paths are presant or not |
16 | 24 |
|
17 |
| - for ch in word: |
18 |
| - if ch not in cur: |
| 25 | + for ch in word: # ch is all the charactors in the word |
| 26 | + if ch not in cur: # if ch is not found in cur that means word not presant so result is false |
19 | 27 | return False
|
20 |
| - cur = cur[ch] |
21 |
| - if '*' in cur: |
| 28 | + cur = cur[ch] #if ch present then go till the end unless it becomes false |
| 29 | + if '*' in cur: # if * key not present that means words is prefix of a word that is present but word is not Present |
22 | 30 | return True
|
23 | 31 | else:
|
24 | 32 | return False
|
| 33 | +# dynamic input |
| 34 | +n2 = int(input("No of words to be Added:\n")) |
| 35 | +print("enter words:") |
| 36 | +words = [] |
| 37 | +checks = [] |
| 38 | +for j in range(n2): |
| 39 | + words.append(input()) |
| 40 | +n3 = int(input("No of words to be searched:\n")) |
| 41 | +print("enter words:") |
| 42 | +for j in range(n3): |
| 43 | + checks.append(input()) |
| 44 | + |
| 45 | + |
25 | 46 | dictionary = Trie()
|
26 |
| -dictionary.add("pqr") |
27 |
| -dictionary.add("pqrst") |
28 |
| -print(dictionary.search("pqr")) |
29 |
| -print(dictionary.search("pq")) |
30 |
| -print(dictionary.search("p")) |
31 |
| -print(dictionary.search("pqrst")) |
32 |
| -dictionary.add("p") |
33 |
| -print(dictionary.search("p")) |
| 47 | + |
| 48 | +for k in words: |
| 49 | + dictionary.add(k) |
| 50 | +for k in checks: |
| 51 | + print(dictionary.search(k)) |
0 commit comments