| 
 | 1 | +import sys  | 
 | 2 | + | 
 | 3 | +# index of given char  | 
 | 4 | +def index(inputCh):  | 
 | 5 | + return ord(inputCh) - ord('a')  | 
 | 6 | + | 
 | 7 | + | 
 | 8 | +# empty Node  | 
 | 9 | +class Node(object):  | 
 | 10 | + children = None  | 
 | 11 | + | 
 | 12 | + def __init__(self):  | 
 | 13 | + self.children = [None] * 26  | 
 | 14 | + self.ch = ' '  | 
 | 15 | + self.word = ""  | 
 | 16 | + self.isLeaf = False  | 
 | 17 | + self.parent = None  | 
 | 18 | + | 
 | 19 | + | 
 | 20 | +class Trie(object):  | 
 | 21 | + root = None  | 
 | 22 | + | 
 | 23 | + def __init__(self):  | 
 | 24 | + self.root = Node()  | 
 | 25 | + | 
 | 26 | + def insertString(self, in_string):  | 
 | 27 | + | 
 | 28 | + current = self.root  | 
 | 29 | + for c in in_string[:-1]:  | 
 | 30 | + idx = index(c)  | 
 | 31 | + try:  | 
 | 32 | + if current.children[idx] is None:  | 
 | 33 | + current.children[idx] = Node()  | 
 | 34 | + current.children[idx].ch = c  | 
 | 35 | + current.children[idx].word = current.word + c  | 
 | 36 | + current.children[idx].parent = current  | 
 | 37 | + except IndexError as ie:  | 
 | 38 | + print("idx = {}\nchar = {}\nstring = {}".format(idx, c, in_string))  | 
 | 39 | + | 
 | 40 | + current = current.children[idx]  | 
 | 41 | + current.isLeaf = True  | 
 | 42 | + | 
 | 43 | + def searchString(self, in_string):  | 
 | 44 | + | 
 | 45 | + current = self.root  | 
 | 46 | + | 
 | 47 | + for c in in_string:  | 
 | 48 | + idx = index(c)  | 
 | 49 | + | 
 | 50 | + # if character is in the trie  | 
 | 51 | + if current.children[idx] is not None:  | 
 | 52 | + current = current.children[idx]  | 
 | 53 | + | 
 | 54 | + # character is not in the trie, word is not exist  | 
 | 55 | + else:  | 
 | 56 | + return False  | 
 | 57 | + | 
 | 58 | + # if word is in the trie, returns true  | 
 | 59 | + return current.isLeaf  | 
 | 60 | + | 
 | 61 | + def deleteString(self, in_string):  | 
 | 62 | + | 
 | 63 | + current = self.root  | 
 | 64 | + if self.searchString(in_string):  | 
 | 65 | + for c in in_string:  | 
 | 66 | + idx = index(c)  | 
 | 67 | + current = current.children[idx]  | 
 | 68 | + | 
 | 69 | + current.isLeaf = False  | 
 | 70 | + | 
 | 71 | + | 
 | 72 | + | 
 | 73 | + | 
 | 74 | +# Examples  | 
 | 75 | + | 
 | 76 | +if __name__ == '__main__':  | 
 | 77 | + | 
 | 78 | +new_trie = Trie()  | 
 | 79 | + | 
 | 80 | + | 
 | 81 | +# insertion  | 
 | 82 | +new_trie.insertString("apple\n")  | 
 | 83 | +new_trie.insertString('app\n')  | 
 | 84 | +new_trie.insertString("application\n")  | 
 | 85 | +new_trie.insertString("homework\n")  | 
 | 86 | + | 
 | 87 | +# search  | 
 | 88 | +print(new_trie.searchString("app")) # expected output: true  | 
 | 89 | +print(new_trie.searchString("apply")) # expected output: false  | 
 | 90 | + | 
 | 91 | +# delete  | 
 | 92 | +new_trie.deleteString('app')  | 
 | 93 | + | 
 | 94 | +# search after delete  | 
 | 95 | +print(">>>Search after delete<<<")  | 
 | 96 | +print(new_trie.searchString('app')) # expected output: false  | 
 | 97 | +print(new_trie.searchString('application')) # expected output: true  | 
0 commit comments