1
+ # trie.py
2
+
3
+ class TrieNode :
4
+ """Represents a node in the Trie data structure."""
5
+ def __init__ (self ):
6
+ """Initializes a TrieNode with empty children and is_end_of_word set to False."""
7
+ self .children = {}
8
+ self .is_end_of_word = False
9
+ self .word = None
10
+
11
+ class Trie :
12
+ """Represents the Trie (prefix tree) data structure."""
13
+ def __init__ (self ):
14
+ """Initializes an empty Trie with a root TrieNode."""
15
+ self .root = TrieNode ()
16
+ self .word_count = 0
17
+
18
+ def insert (self , word ):
19
+ """Inserts a word into the Trie."""
20
+ node = self .root
21
+ for char in word :
22
+ if char not in node .children :
23
+ node .children [char ] = TrieNode ()
24
+ node = node .children [char ]
25
+ if not node .is_end_of_word :
26
+ node .is_end_of_word = True
27
+ node .word = word
28
+ self .word_count += 1
29
+
30
+ def search (self , word ):
31
+ """Searches for a word in the Trie."""
32
+ node = self .root
33
+ for char in word :
34
+ if char not in node .children :
35
+ return False
36
+ node = node .children [char ]
37
+ return node .is_end_of_word
38
+
39
+ def starts_with (self , prefix ):
40
+ """Checks if any word in the Trie starts with the given prefix."""
41
+ node = self .root
42
+ for char in prefix :
43
+ if char not in node .children :
44
+ return False
45
+ node = node .children [char ]
46
+ return True
47
+
48
+ def count_words (self ):
49
+ """Returns the total number of words stored in the Trie."""
50
+ return self .word_count
51
+
52
+ def longest_common_prefix (self ):
53
+ """Finds the longest common prefix among all words in the Trie."""
54
+ node = self .root
55
+ prefix = ""
56
+ while len (node .children ) == 1 and not node .is_end_of_word :
57
+ char = next (iter (node .children ))
58
+ prefix += char
59
+ node = node .children [char ]
60
+ return prefix
61
+
62
+ def autocomplete (self , prefix ):
63
+ """Provides a list of words that match a given prefix."""
64
+ node = self .root
65
+ for char in prefix :
66
+ if char not in node .children :
67
+ return []
68
+ node = node .children [char ]
69
+
70
+ def collect_words (current_node , current_prefix ):
71
+ words = []
72
+ if current_node .is_end_of_word :
73
+ words .append (current_prefix )
74
+ for char , child_node in current_node .children .items ():
75
+ words .extend (collect_words (child_node , current_prefix + char ))
76
+ return words
77
+
78
+ return collect_words (node , prefix )
79
+
80
+ def bulk_insert (self , words ):
81
+ """Inserts multiple words into the Trie in a single operation."""
82
+ for word in words :
83
+ self .insert (word )
84
+
85
+ def clear (self ):
86
+ """Removes all words from the Trie, resetting it."""
87
+ self .root = TrieNode ()
88
+ self .word_count = 0
89
+
90
+ def is_empty (self ):
91
+ """Returns True if the Trie is empty, otherwise False."""
92
+ return self .word_count == 0
93
+
94
+ def find_all_words (self ):
95
+ """Retrieves all words currently stored in the Trie."""
96
+ def collect_words (current_node ):
97
+ words = []
98
+ if current_node .is_end_of_word :
99
+ words .append (current_node .word )
100
+ for child_node in current_node .children .values ():
101
+ words .extend (collect_words (child_node ))
102
+ return words
103
+
104
+ return collect_words (self .root )
105
+
106
+ def shortest_unique_prefix (self , word ):
107
+ """Determines the shortest unique prefix for a given word."""
108
+ node = self .root
109
+ prefix = ""
110
+ for char in word :
111
+ prefix += char
112
+ if len (node .children [char ].children ) <= 1 and node .children [char ].is_end_of_word == False :
113
+ return prefix
114
+ node = node .children [char ]
115
+ return word
116
+
117
+ def longest_word (self ):
118
+ """Finds and returns the longest word in the Trie."""
119
+ all_words = self .find_all_words ()
120
+ if not all_words :
121
+ return None
122
+ return max (all_words , key = len )
0 commit comments