2. What Is TRIE?
Trie Is An Efficient Information Retrieval Data Structure Also Called
Digital Tree And Sometimes Radix Tree Or Prefix Tree (As They Can
Be Searched By Prefixes), Is An Ordered Tree Data Structure That Is
Used To Store A Dynamic Set Or Associative Array Where The Keys
Are Usually Strings.
3. Why Trie Data Structure?
• Searching trees in general favor keys which are of fixed size
since this leads to efficient storage management.
• However in case of applications which are retrieval based and
which call for keys varying length, tries provide better options.
• Tries are also called as Lexicographic Search trees.
• The name trie (pronounced as “try”)originated from the word
“retrieval”.
5. STANDARD TRIE
The Standard Trie For A Set Of Strings S Is An Ordered Tree Such That:
Each Node Labeled With A Character (Without Root).
The Children Of A Node Are Alphabetically Ordered.
The Paths From The External Nodes To The Root Yield The Strings Of S
7. TIME COMPLEXITY
A Standard Trie Uses O(n) Space. Operations (find, insert, remove) Take Time
O(dm) Each, Where:
n = Total Size Of The Strings In S,
m = Size Of The String Parameter Of The Operation
d = Alphabet Size
8. TRIE SPECIFICATION
Operations:
addWord
Function Adds word to an .
Postcondition Trie is not full
searchWord
Function Search a word in the trie
Postcondition Returns true if the world is found and false otherwise.
deleteWord
Function Delete a word in the trie
Postcondition Trie is not empty
10. NODE STRUCTURE
Class Node
{
public:
char value;
bool end;
Node *children[26];
}
The Character Value (A – Z) / (a – z).
Indicates Whether This Node Completes A Word
Represents The 26 Letters In The Alphabet
11. NODE STRUCTURE
char value
bool end
0 1 2 3 4 5 6 7 8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
Character
Data
Boolean
Data
A Node Type
Pointer
Array
0
13. INSERTION ALGORITHM
Insert: Apple
First Create A Root That Has Empty String And Every Single Pointer Array
Must Point To The NULL (Default). And Boolean Value Of Every Node Must Be
false By Default.
false
0 …
1
0
…
2
0
…
2
6
NULL
16. INSERTION ALGORITHM
Insert: Apple
Second Convert All Of The String’s Character To Uppercase Or To Lowercase.
char currentChar = tolower(word.at(i));
Here, Suppose string s = “Apple” And Length Of String Is 5 So……….
17. INSERTION ALGORITHM
Insert: Apple
Second Convert All Of The String’s Character To Uppercase Or To Lowercase.
char currentChar = tolower(word.at(i));
Here, Suppose string s = “Apple” And Length Of String Is 5 So……….
s[0] = A, s[1] = p, s[2] = p, s[3] = l, s[4] = e
18. INSERTION ALGORITHM
Insert: Apple
Second Convert All Of The String’s Character To Uppercase Or To Lowercase.
char currentChar = tolower(word.at(i));
A p p l e
0 1 2 3 4
Here, Suppose string s = “Apple” And Length Of String Is 5 So……….
s[0] = A, s[1] = p, s[2] = p, s[3] = l, s[4] = e
21. INSERTION ALGORITHM
Insert: Apple
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
24. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
25. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
YES!
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
character as one of its descendants
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
29. INSERTION ALGORITHM
Insert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar a
aNode(curre
ntChar)
anewNode
All 26 Children Of newNode Will Point To The NULL
33. INSERTION ALGORITHM
Insert: Apple
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
0 == 4? NO Won’t Execute
37. INSERTION ALGORITHM
Insert: Apple
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar p
int index = currentChar - 'a'; int index = p - 'a';
int index = 15;
38. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[15] != NULL)
{
currentNode = currentNode->children[15];
}
a
false
1
5
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
39. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[15] != NULL)
{
currentNode = currentNode->children[15];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
5
…
1
0
…
2
0
…
2
6
Is 15 Pointing To The NULL?
YES!
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
character as one of its descendants
Here 15 Is The Index Value ( p – ‘a’ )
NULL
43. INSERTION ALGORITHM
Insert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar p
pNode(curre
ntChar)
pnewNode
All 26 Children Of newNode Will Point To The NULL
47. INSERTION ALGORITHM
Insert: Apple
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
1 == 4? NO Won’t Execute
54. INSERTION ALGORITHM
Insert: Apple
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
4 == 4? YES Will Execute
59. INSERTION ALGORITHM
Insert: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
62. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
63. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
66. INSERTION ALGORITHM
Insert: Army
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
0 == 3? NO Won’t Execute
68. INSERTION ALGORITHM
Insert: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar r
int index = currentChar - 'a'; int index = r - 'a';
int index = 17;
69. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
a
false
1
7
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
70. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
7
…
1
0
…
2
0
…
2
6
Is 17 Pointing To The NULL?
YES!
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
character as one of its descendants
Here 17 Is The Index Value ( r – ‘a’ )
NULL
74. INSERTION ALGORITHM
Insert: Army
Node *newNode = new Node(currentChar);
So……….
currentChar r
rNode(curre
ntChar)
rnewNode
All 26 Children Of newNode Will Point To The NULL
79. INSERTION ALGORITHM
Insert: Army
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
1 == 3? NO Won’t Execute
83. INSERTION ALGORITHM
Insert: Army
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
3 == 3? YES Will Execute
87. DELETION ALGORITHM
Delete: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
90. DELETION ALGORITHM
Delete: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
91. DELETION ALGORITHM
Delete: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
95. DELETION ALGORITHM
Delete: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar r
int index = currentChar - 'a'; int index = r - 'a';
int index = 17;
96. DELETION ALGORITHM
Delete: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
a
false
1
7
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
97. DELETION ALGORITHM
Delete: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
7
…
1
0
…
2
0
…
2
6
Is 17 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 17 Is The Index Value ( r – ‘a’ )
NULL
102. DELETION ALGORITHM
Delete: Army
if (i == word.size() – 1 && currentNode->end)
{
currentNode->end = false;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
3 == 3? && currentNode end? YES Will Execute
106. SEARCHING ALGORITHM
SEARCH: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
109. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
110. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
114. SEARCHING ALGORITHM
Search: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar r
int index = currentChar - 'a'; int index = r - 'a';
int index = 17;
115. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
a
false
1
7
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
116. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
7
…
1
0
…
2
0
…
2
6
Is 17 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 17 Is The Index Value ( r – ‘a’ )
NULL
121. SEARCHING ALGORITHM
Search: Army
if (i == word.size() – 1 && currentNode->end)
{
return true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
3 == 3? && currentNode end? YES Will Execute
153. SOURCE CODETrie.c
void Trie::addWord(string word)
{
Node * currentNode = root;
for (int i = 0; i < (int)word.size(); ++i)
{
char currentChar = tolower(word.at(i));
int index = currentChar - 'a';
assert(index >= 0); // Makes sure the character is between a-z
if (currentNode->children[index] != NULL)
{
// check if the current node has the current character as one of its
decendants
currentNode = currentNode->children[index];
}
else
{
// the current node doesn't have the current character as one of its
decendants
Node * newNode = new Node(currentChar);
currentNode->children[index] = newNode;
currentNode = newNode;
}
if (i == (int)word.size() - 1)
{
// the last character of the word has been reached
currentNode->end = true;
}
}
}
160. TRIES IN AUTO COMPLETE
• Since a trie is a tree-like data structure in which each node
contains an array of pointers, one pointer for each character in the
alphabet.
• Starting at the root node, we can trace a word by following
pointers corresponding to the letters in the target word.
• Starting from the root node, you can check if a word exists in the
trie easily by following pointers corresponding to the letters in the
161. TRIES IN AUTO COMPLETE
• Auto-complete functionality is used widely over the internet and
mobile apps. A lot of websites and apps try to complete your input
as soon as you start typing.
• All the descendants of a node have a common prefix of the string
associated with that node.
164. WHY TRIES IN AUTO COMPLETE
• Implementing auto complete using a trie is easy.
• We simply trace pointers to get to a node that represents the string
the user entered. By exploring the trie from that node down, we
can enumerate all strings that complete user’s input.
165. AUTOMATIC COMMAND COMPLETION
• When using an operating system such as Unix or DOS, we type in
system commands to accomplish certain tasks. For example, the
Unix and DOS command cd may be used to change the current
directory.