You are on page 1of 8

INTRODUCTION

The word trie is a reflection of the word “retrieval”,


because the trie can find a single word in a dictionary
with only a prefix of the word.
Trie is an efficient data retrieval data structure. Using
trie, search complexities can be brought to an
optimal limit, i.e., length of the string.
It is a multi-way tree structure useful for storing
strings over an alphabet, when we are storing them.
It has been used to store large dictionaries of
English, say, words in spell-checking programs.
However, the penalty on tries is the storage
requirements.

WHAT IS A TRIE???
A trie is a tree like data structure which stores
strings, and helps you find the data associated
with that string using the prefix of the string.
For example, say you plan on building a dictionary
to store strings along with their meanings. You
must be wondering why can’t I simply use a hash
table, to get the information.
Yes, you can get information using a hash table,
but the hash tables can only find data where the
string exactly matches the one, we’ve added. But
the trie will give us the capability to find strings
with common prefixes, a missing character etc. in
lesser time, in comparison to a hash table.

Structure of Trie:
The trie consists of nodes. Each node has these
fields:
1. Key - Part of the string to be searched, inserted or
deleted.
2. Value - The value associated with a string (e.g. In a
dictionary it could be the meaning of the word
which we are searching)
3. Neighbour node address - It consists of the
address of the neighbouring node at the same
level.
4. Previous neighbour address - It consists of the
address of the previous node at the same level.
5. Children node address - It consists of the address
of the child nodes of the current node.
6. Parent node address - It consists of the address of
the parent node of the current node.
DIAGRAMMATIC REPRESENTATION OF TRIE

Problem Statement
Through this project, we intend to help the
user to store words and their meanings. It
also allows for users to search for a specific
word and fetch its meaning from the
collection of previously stored data. Along
with searching, this program also allows for
the user to modify the meaning of a
previously stored word, by searching for the
word and storing the new meaning and also
to delete a word and its meaning.
Data structure used:
For the implementation of the dictionary, we
used Trie. Trie is a special data structure that
is used to store strings that can be visualized
like a graph. It consists of nodes and edges.
Each node consists at max 26 children and
edges connect each parent node to its
children. Dictionary can be implemented
using other concepts like hash tables, but
using a Trie allows or data to be searched
faster. It also avoids the collisions that can
occur if hash tables are used. Using Trie
allows to skip the hassle of choosing an
appropriate hash function. Trie
has predetermined alphabetical ordering
which makes it easier to use to implement a
dictionary and find and store words with
common prefix.
 Time complexity:
 We can search a key in O(M) time, where M is
length of the string
  Space complexity:
 Memory requirements of Trie is O(ALPHABETSIZE
* Key Length * N),
  where N is the number of keys in Trie.
 Software and hardware requirements: 2.0GHz
CPU, 1GB RAM
Operating system: Windows
Algorithm
The trie is a tree of nodes which supports Find and
Insert operations. Find returns the value for a key
string, and Insert inserts a string (the key) and a value
into the trie. Both Insert and Find run in O(m) time,
where m is the length of the key.
A simple Node class can be used to represent nodes in
the trie:
class Node: def __init__(self) -> None:
self.children: Dict[str,Node] = {}
self.value: Optional[Any] = None
Note that children is a dictionary of characters to a
node's children; and it is said that a "terminal" node is
one which represents a complete string.
A trie's value can be looked up as follows:
def find(node: Node, key:str) -> Optional[Any]:
for char in key:
if char in node.children:
node = node.children[char]
else:
return None
return node.value
Insertion proceeds by walking the trie according to the
string to be inserted, then appending new nodes for the
suffix of the string that is not contained in the trie:
def insert(node: Node, key:str, value: Any)-> >None:
for char in key:
if char not in node.children:
node.children[char] = Node()
node = node.children[char]
node.value = value

FLOW CHART
RESULT:

You might also like