|
| 1 | +""" |
| 2 | +Purpose: |
| 3 | +
|
| 4 | +Implementation of Trie has been asked in my interviews like in Google |
| 5 | +Store word in Dictionary |
| 6 | +Search word in the dictionary returns True if exist |
| 7 | +Stores words in a compressed manner |
| 8 | +""" |
| 9 | +""" |
| 10 | +Inputs: |
| 11 | +1nd line no of word you want to store in Trie (int) |
| 12 | +2rd line to (2+1st line input)th line (string) |
| 13 | +Next line enter number of words want to search |
| 14 | +then enter strings in each line |
| 15 | +
|
| 16 | +""" |
| 17 | +class Trie: |
| 18 | + root = {} #main dictionary of Trie |
| 19 | + |
| 20 | + def add(self, word): # This funtion adds new word to Trie |
| 21 | + cur = self.root # cur is iterator to check whether new key/paths are required to add new word and root is main dictionary |
| 22 | + |
| 23 | + for ch in word: # ch is all the charactors in the word |
| 24 | + if ch not in cur: # if charactor is not present add new key/path initiate it empty |
| 25 | + cur[ch] = {} |
| 26 | + cur = cur[ch] # now go to the key that was created or was present |
| 27 | + cur['*'] = True # set * key to true at the end of each word |
| 28 | + #so larger the word the dictionary nesting will take place and * key represant the end state of the word |
| 29 | + |
| 30 | + def search(self,word): # This function searches whether the word is presant or not presant |
| 31 | + cur = self.root #cur is iterator to check whether new key/paths are presant or not |
| 32 | + |
| 33 | + for ch in word: # ch is all the charactors in the word |
| 34 | + if ch not in cur: # if ch is not found in cur that means word not presant so result is false |
| 35 | + return False |
| 36 | + cur = cur[ch] #if ch present then go till the end unless it becomes false |
| 37 | + if '*' in cur: # if * key not present that means words is prefix of a word that is present but word is not Present |
| 38 | + return True |
| 39 | + else: |
| 40 | + return False |
| 41 | +# dynamic input |
| 42 | +n2 = int(input("No of words to be Added:\n")) |
| 43 | +print("enter words:") |
| 44 | +words = [] |
| 45 | +checks = [] |
| 46 | +for j in range(n2): |
| 47 | + words.append(input()) |
| 48 | +n3 = int(input("No of words to be searched:\n")) |
| 49 | +print("enter words:") |
| 50 | +for j in range(n3): |
| 51 | + checks.append(input()) |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +""" |
| 56 | +Time complexity : |
| 57 | +Insert: O(word Length) |
| 58 | +Search: O(word Length) |
| 59 | +
|
| 60 | +Space complexity : |
| 61 | +Insert: O(1) |
| 62 | +Search: O(1) |
| 63 | +
|
| 64 | +Storages required for storing words: |
| 65 | +if no word has same prefix the required (total word length+1)*(char size) |
| 66 | +
|
| 67 | +""" |
| 68 | +dictionary = Trie() # Trie object created |
| 69 | + |
| 70 | + |
| 71 | +for k in words: |
| 72 | + dictionary.add(k) # adding word in Trie from words list |
| 73 | +for k in checks: |
| 74 | + print(dictionary.search(k)) # printing Searched word results if present then prints True if not present then returns False |
0 commit comments