1
+
2
+ class TrieNode :
3
+ def __init__ (self , key , parent = None , children : dict = {}):
4
+ self .key = key
5
+ self .parent = parent
6
+ self .children :dict = {}
7
+ self .endchar : bool = False
8
+
9
+ class Trie :
10
+ def __init__ (self ):
11
+ self .root : TrieNode = TrieNode (None )
12
+
13
+ def insert (self , string : str ):
14
+ current : TrieNode = self .root
15
+ for character in string :
16
+ if character not in current .children :
17
+ current .children [character ] = TrieNode (character , current )
18
+ current = current .children [character ]
19
+ current .endchar = True
20
+
21
+ def contains (self , string : str )-> bool :
22
+ current : TrieNode = self .root
23
+ for character in string :
24
+ if character not in current .children :
25
+ current = None
26
+ break
27
+ current = current .children [character ]
28
+ if current is None :
29
+ return False
30
+ return current .endchar
31
+
32
+ def delete (self , string : str ):
33
+ current : TrieNode = self .root
34
+ for character in string :
35
+ if character not in current .children :
36
+ current = None
37
+ break
38
+ current = current .children [character ]
39
+ if current is None :
40
+ return
41
+ current .endchar = False
42
+ parent : TrieNode = current .parent
43
+ while parent is not None and not current .endchar and len (current .children ) == 0 :
44
+ del (parent .children [current .key ])
45
+ current = parent
46
+ parent = current .parent
47
+
48
+ def prefix (self , prefix : str )-> list :
49
+ current : TrieNode = self .root
50
+ for character in prefix :
51
+ if character not in current .children :
52
+ current = None
53
+ break
54
+ current = current .children [character ]
55
+ if current is None :
56
+ return
57
+ words : list = []
58
+ self .helper (current , words , prefix )
59
+ return words
60
+
61
+ def helper (self , node : TrieNode , words : list , currentWord : str ):
62
+ if node is None :
63
+ return
64
+ if node .endchar :
65
+ words .append (currentWord )
66
+ for key in node .children :
67
+ self .helper (node .children [key ], words , currentWord + key )
68
+
69
+ def allWords (self )-> list :
70
+ words : list = []
71
+ self .helper (self .root , words , "" )
72
+ return words
73
+
74
+ def count (self )-> int :
75
+ return self .countHelper (self .root )
76
+
77
+ def countHelper (self , node : TrieNode )-> int :
78
+ if node is None :
79
+ return 0
80
+ sum : int = 0
81
+ if node .endchar :
82
+ sum += 1
83
+ for character in node .children :
84
+ sum += self .countHelper (node .children [character ])
85
+ return sum
86
+
87
+ trie = Trie ()
88
+ trie .insert ("javascript" )
89
+ trie .insert ("java" )
90
+ trie .insert ("scala" )
91
+ trie .insert ("scale" )
92
+ trie .insert ("scalable" )
93
+ trie .insert ("perl" )
94
+
95
+ print ("Contains 'javascript' : " , trie .contains ("javascript" ))
96
+ print ("Contains 'java' : " , trie .contains ("java" ))
97
+ print ("Contains 'ruby' : " , trie .contains ("ruby" ))
98
+
99
+ #trie.delete("java")
100
+ trie .delete ("javascript" )
101
+
102
+
103
+ print ("Contains 'javascript' : " , trie .contains ("javascript" ))
104
+ print ("Contains 'java' : " , trie .contains ("java" ))
105
+ print ("Contains 'ruby' : " , trie .contains ("ruby" ))
106
+
107
+ print (trie .prefix ("scal" )) # ['scala', 'scalable', 'scale']
108
+ print (trie .prefix ("java" )) # ['java']
109
+
110
+ print ("All words" , trie .allWords ()) # All words ['java', 'scala', 'scalable', 'scale', 'perl']
111
+ print ("Count : " , trie .count ())
0 commit comments