I am implementing a Trie in python. Till now I have come across two different methods to implement it:
1) use a class Node (similar to struct Node in C++) with data members:
char
- to store character
is_end
- to store end of word (true or false)
prefix_count
- store number of words with current prefix
child
- Node type dict (to store other nodes i.e. for 26 alphabets)
class Node(object):
def __init__(self):
self.char = ''
self.word = ''
self.is_end = False
self.prefix_count = 0
self.child = {}
2) use a dictionary to store all the data:
e.g. for the input words = {'foo', 'bar', 'baz', 'barz'}
we derive this dictionary:
{'b': {'a': {'r': {'_end_': '_end_', 'z': {'_end_': '_end_'}},
'z': {'_end_': '_end_'}}},
'f': {'o': {'o': {'_end_': '_end_'}}}}
Which is the efficient and standard data structure, which is both memory efficient and fast for traversal and other trie operations on big dataset of words?
Node
inself.child
, considering this is a dictionary? If indeed you keep it as adict
, and somehow referNode
objects, I see both the methods as having same time complexity, but 1st one having more space complexity. And if you referself.child
as a list, then the 1st one might be a bit slower – Ibnrushddict
to implement trie, you're [almost] cheating. – Nanceynanchang