trie data structure
DESCRIPTION
It Is About Trie Data StructureTRANSCRIPT
![Page 1: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/1.jpg)
DATA STRUCTURE
TRIE
Name: Mahadi Hassan
ID: 1320133042
&
Name: Mahmud Rahman Parag
ID: 1320900042
![Page 2: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/2.jpg)
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.
![Page 3: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/3.jpg)
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”.
![Page 4: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/4.jpg)
TYPES OF TRIE
1.Standard Tries
2.Compressed Tries
3.Suffix Tries
![Page 5: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/5.jpg)
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
![Page 6: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/6.jpg)
EXAMPLE:
Standard Trie For A Set Of Strings S
S = { bear, bell, bid, bull, buy, sell, stock, stop }
![Page 7: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/7.jpg)
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
![Page 8: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/8.jpg)
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
![Page 9: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/9.jpg)
TRIE SPECIFICATION
Operations:
Function Print the word in the trie
Postcondition Trie either maybe full or not
![Page 10: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/10.jpg)
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
![Page 11: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/11.jpg)
NODE STRUCTURE
char value
bool end
0 1 2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Character Data
Boolean Data
A Node Type Pointer Array
\0
![Page 12: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/12.jpg)
INSERTION
![Page 13: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/13.jpg)
INSERTION ALGORITHMInsert: 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 … 10 … 2
0 … 26
NULL
![Page 14: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/14.jpg)
INSERTION ALGORITHMInsert: Apple
Root
![Page 15: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/15.jpg)
INSERTION ALGORITHMInsert: Apple
Second Convert All Of The String’s Character To Uppercase Or To Lowercase.
char currentChar = tolower(word.at(i));
![Page 16: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/16.jpg)
INSERTION ALGORITHMInsert: 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……….
![Page 17: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/17.jpg)
INSERTION ALGORITHMInsert: 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
![Page 18: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/18.jpg)
INSERTION ALGORITHMInsert: 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
![Page 19: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/19.jpg)
INSERTION ALGORITHMInsert: Apple
A p p l e
0 1 2 3 4
![Page 20: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/20.jpg)
INSERTION ALGORITHMInsert: Apple
A p p l e
0 1 2 3 4
char currentChar = tolower(word.at(0));
currentChar a
![Page 21: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/21.jpg)
INSERTION ALGORITHMInsert: 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;
![Page 22: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/22.jpg)
INSERTION ALGORITHMInsert: Apple
Declare A Pointer Node Type Pointer Variable That Point To The Root
Node *currentNode = root;
RootcurrentNodePointing To
![Page 23: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/23.jpg)
INSERTION ALGORITHMInsert: Apple
Root
currentNode
![Page 24: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/24.jpg)
INSERTION ALGORITHMInsert: Apple
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 … 10 … 2
0 … 26
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 25: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/25.jpg)
INSERTION ALGORITHMInsert: 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 … 10 … 2
0 … 26
NULL
![Page 26: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/26.jpg)
INSERTION ALGORITHMInsert: Apple
else
{
Node *newNode = new Node(currentChar);
currentNode->children[0] = newNode;
currentNode = newNode;
}
So………. Node Constructor
![Page 27: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/27.jpg)
INSERTION ALGORITHMInsert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar a
Node(currentChar)
![Page 28: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/28.jpg)
INSERTION ALGORITHMInsert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar a
aNode(currentChar)
![Page 29: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/29.jpg)
INSERTION ALGORITHMInsert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar a
aNode(currentChar)
anewNodeAll 26 Children Of newNode Will Point To The NULL
![Page 30: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/30.jpg)
INSERTION ALGORITHMInsert: Apple
currentNode->children[0] = newNode;
So……….
anewNode RootcurrentNode
0
![Page 31: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/31.jpg)
INSERTION ALGORITHMInsert: Apple
currentNode->children[0] = newNode;
So……….
a
newNode Root0
currentNode
![Page 32: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/32.jpg)
INSERTION ALGORITHMInsert: Apple
currentNode = newNode;
So……….
a
newNode Root0
currentNode
![Page 33: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/33.jpg)
INSERTION ALGORITHMInsert: 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
![Page 34: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/34.jpg)
INSERTION ALGORITHMInsert: Apple
So……….
a
newNode Root0
currentNode
bool end will be false
![Page 35: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/35.jpg)
INSERTION ALGORITHMInsert: Apple
Root0
acurrentNode
![Page 36: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/36.jpg)
INSERTION ALGORITHMInsert: Apple
A p p l e
0 1 2 3 4
char currentChar = tolower(word.at(1));
currentChar p
![Page 37: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/37.jpg)
INSERTION ALGORITHMInsert: 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;
![Page 38: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/38.jpg)
INSERTION ALGORITHMInsert: Apple
if (currentNode->children[15] != NULL)
{
currentNode = currentNode->children[15];
}
afalse
15 … 1
0 … 20 … 2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 39: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/39.jpg)
INSERTION ALGORITHMInsert: Apple
if (currentNode->children[15] != NULL)
{
currentNode = currentNode->children[15];
}
Check If The Current Node Has The Current Character As One Of Its Descendants
afalse
15 … 1
0 … 20 … 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
![Page 40: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/40.jpg)
INSERTION ALGORITHMInsert: Apple
else
{
Node *newNode = new Node(currentChar);
currentNode->children[15] = newNode;
currentNode = newNode;
}
So………. Node Constructor
![Page 41: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/41.jpg)
INSERTION ALGORITHMInsert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar p
Node(currentChar)
![Page 42: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/42.jpg)
INSERTION ALGORITHMInsert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar p
pNode(currentChar)
![Page 43: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/43.jpg)
INSERTION ALGORITHMInsert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar p
pNode(currentChar)
pnewNodeAll 26 Children Of newNode Will Point To The NULL
![Page 44: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/44.jpg)
INSERTION ALGORITHMInsert: Apple
currentNode->children[15] = newNode;
So……….
pnewNode acurrentNode
15
![Page 45: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/45.jpg)
INSERTION ALGORITHMInsert: Apple
currentNode->children[15] = newNode;
So……….
p
newNode a15
currentNode
![Page 46: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/46.jpg)
INSERTION ALGORITHMInsert: Apple
currentNode = newNode;
So……….
p
newNode a15
currentNode
![Page 47: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/47.jpg)
INSERTION ALGORITHMInsert: 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
![Page 48: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/48.jpg)
INSERTION ALGORITHMInsert: Apple
So……….
p
newNode a15
currentNode
bool end will be false
![Page 49: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/49.jpg)
INSERTION ALGORITHMInsert: Apple
Root0
a15
p
currentNode
![Page 50: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/50.jpg)
INSERTION ALGORITHMInsert: Apple
SIMILARLY
![Page 51: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/51.jpg)
INSERTION ALGORITHMInsert: Apple
Root0
a15
p15
p
![Page 52: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/52.jpg)
INSERTION ALGORITHMInsert: Apple
Root0
a15
p15
p11
l
![Page 53: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/53.jpg)
INSERTION ALGORITHMInsert: Apple
Root0
a15
p15
p11
l4
e
![Page 54: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/54.jpg)
INSERTION ALGORITHMInsert: 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
![Page 55: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/55.jpg)
INSERTION ALGORITHMInsert: Apple
Root0
a15
p15
p11
l4
e
bool end will be True
![Page 56: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/56.jpg)
INSERTION ALGORITHM
Now Insert: Army
![Page 57: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/57.jpg)
INSERTION ALGORITHMInsert: Army
A r m y
0 1 2 3
![Page 58: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/58.jpg)
INSERTION ALGORITHMInsert: Army
A r m y
0 1 2 3
char currentChar = tolower(word.at(0));
currentChar a
![Page 59: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/59.jpg)
INSERTION ALGORITHMInsert: 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;
![Page 60: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/60.jpg)
INSERTION ALGORITHMInsert: Army
Declare A Pointer Node Type Pointer Variable That Point To The Root
Node *currentNode = root;
RootcurrentNodePointing To
![Page 61: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/61.jpg)
DELETE ALGORITHMDelete: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
currentNode
![Page 62: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/62.jpg)
INSERTION ALGORITHMInsert: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 … 10 … 2
0 … 26
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 63: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/63.jpg)
INSERTION ALGORITHMInsert: 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 … 10 … 2
0 … 26
NULL
![Page 64: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/64.jpg)
INSERTION ALGORITHMInsert: Army
currentNode = currentNode->children[0];
So……….
![Page 65: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/65.jpg)
INSERTION ALGORITHMInsert: Army
Root0
a15
p15
p11
l4
e
currentNode
bool end will be True
![Page 66: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/66.jpg)
INSERTION ALGORITHMInsert: 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
![Page 67: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/67.jpg)
INSERTION ALGORITHMInsert: Army
A r m y
0 1 2 3
char currentChar = tolower(word.at(1));
currentChar r
![Page 68: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/68.jpg)
INSERTION ALGORITHMInsert: 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;
![Page 69: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/69.jpg)
INSERTION ALGORITHMInsert: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
afalse
17 … 1
0 … 20 … 2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 70: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/70.jpg)
INSERTION ALGORITHMInsert: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its Descendants
afalse
17 … 1
0 … 20 … 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
![Page 71: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/71.jpg)
INSERTION ALGORITHMInsert: Army
else
{
Node *newNode = new Node(currentChar);
currentNode->children[17] = newNode;
currentNode = newNode;
}
So………. Node Constructor
![Page 72: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/72.jpg)
INSERTION ALGORITHMInsert: Army
Node *newNode = new Node(currentChar);
So……….
currentChar r
Node(currentChar)
![Page 73: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/73.jpg)
INSERTION ALGORITHMInsert: Army
Node *newNode = new Node(currentChar);
So……….
currentChar r
rNode(currentChar)
![Page 74: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/74.jpg)
INSERTION ALGORITHMInsert: Army
Node *newNode = new Node(currentChar);
So……….
currentChar r
rNode(currentChar)
rnewNodeAll 26 Children Of newNode Will Point To The NULL
![Page 75: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/75.jpg)
INSERTION ALGORITHMInsert: Army
currentNode->children[17] = newNode;
So……….
![Page 76: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/76.jpg)
INSERTION ALGORITHMInsert: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r
currentNode
![Page 77: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/77.jpg)
INSERTION ALGORITHMInsert: Army
currentNode = newNode;
So……….
![Page 78: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/78.jpg)
INSERTION ALGORITHMInsert: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r
currentNode
![Page 79: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/79.jpg)
INSERTION ALGORITHMInsert: 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
![Page 80: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/80.jpg)
INSERTION ALGORITHMInsert: Army
SIMILARLY
![Page 81: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/81.jpg)
INSERTION ALGORITHMInsert: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
![Page 82: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/82.jpg)
INSERTION ALGORITHMInsert: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
![Page 83: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/83.jpg)
INSERTION ALGORITHMInsert: 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
![Page 84: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/84.jpg)
INSERTION ALGORITHMInsert: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
![Page 85: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/85.jpg)
DELETION
![Page 86: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/86.jpg)
DELETION ALGORITHMDelete: Army
A r m y
0 1 2 3
char currentChar = tolower(word.at(0));
currentChar a
![Page 87: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/87.jpg)
DELETION ALGORITHMDelete: 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;
![Page 88: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/88.jpg)
DELETION ALGORITHMDelete: Army
Declare A Pointer Node Type Pointer Variable That Point To The Root
Node *currentNode = root;
RootcurrentNodePointing To
![Page 89: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/89.jpg)
DELETION ALGORITHMDelete: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 90: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/90.jpg)
DELETION ALGORITHMDelete: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 … 10 … 2
0 … 26
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 91: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/91.jpg)
DELETION ALGORITHMDelete: 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 … 10 … 2
0 … 26
NULL
![Page 92: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/92.jpg)
DELETION ALGORITHMDelete: Army
currentNode = currentNode->children[0];
So……….
![Page 93: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/93.jpg)
DELETION ALGORITHMDelete: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 94: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/94.jpg)
DELETION ALGORITHMDelete: Army
A r m y
0 1 2 3
char currentChar = tolower(word.at(1));
currentChar r
![Page 95: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/95.jpg)
DELETION ALGORITHMDelete: 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;
![Page 96: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/96.jpg)
DELETION ALGORITHMDelete: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
afalse
17 … 1
0 … 20 … 2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 97: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/97.jpg)
DELETION ALGORITHMDelete: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its Descendants
afalse
17 … 1
0 … 20 … 2
6
Is 17 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 17 Is The Index Value ( r – ‘a’ )
NULL
![Page 98: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/98.jpg)
DELETION ALGORITHMDelete: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 99: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/99.jpg)
DELETION ALGORITHMDelete: Army
SIMILARLY
![Page 100: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/100.jpg)
DELETION ALGORITHMDelete: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 101: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/101.jpg)
DELETION ALGORITHMDelete: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 102: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/102.jpg)
DELETION ALGORITHMDelete: 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
![Page 103: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/103.jpg)
DELETION ALGORITHMDelete: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
ybool end will be False So Word Couldn’t Be Found By Search So The Word Is Deleted.
![Page 104: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/104.jpg)
SEARCH
![Page 105: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/105.jpg)
SEARCHING ALGORITHMSearch: Army
A r m y
0 1 2 3
char currentChar = tolower(word.at(0));
currentChar a
![Page 106: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/106.jpg)
SEARCHING ALGORITHMSEARCH: 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;
![Page 107: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/107.jpg)
SEARCHING ALGORITHMSearch: Army
Declare A Pointer Node Type Pointer Variable That Point To The Root
Node *currentNode = root;
RootcurrentNodePointing To
![Page 108: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/108.jpg)
SEARCHING ALGORITHMSearch: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 109: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/109.jpg)
SEARCHING ALGORITHMSearch: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 … 10 … 2
0 … 26
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 110: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/110.jpg)
SEARCHING ALGORITHMSearch: 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 … 10 … 2
0 … 26
NULL
![Page 111: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/111.jpg)
SEARCHING ALGORITHMSearch: Army
currentNode = currentNode->children[0];
So……….
![Page 112: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/112.jpg)
SEARCHING ALGORITHMSearch: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 113: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/113.jpg)
SEARCHING ALGORITHMSearch: Army
A r m y
0 1 2 3
char currentChar = tolower(word.at(1));
currentChar r
![Page 114: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/114.jpg)
SEARCHING ALGORITHMSearch: 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;
![Page 115: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/115.jpg)
SEARCHING ALGORITHMSearch: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
afalse
17 … 1
0 … 20 … 2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its Descendants
![Page 116: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/116.jpg)
SEARCHING ALGORITHMSearch: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its Descendants
afalse
17 … 1
0 … 20 … 2
6
Is 17 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 17 Is The Index Value ( r – ‘a’ )
NULL
![Page 117: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/117.jpg)
SEARCHING ALGORITHMSearch: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 118: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/118.jpg)
SEARCHING ALGORITHMSearch: Army
SIMILARLY
![Page 119: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/119.jpg)
SEARCHING ALGORITHMSearch: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 120: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/120.jpg)
SEARCHING ALGORITHMSearch: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
currentNode
![Page 121: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/121.jpg)
SEARCHING ALGORITHMSearch: 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
![Page 122: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/122.jpg)
SEARCHING ALGORITHMSearch: Army
Root0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
yReturn True. Item Found !
![Page 123: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/123.jpg)
PRINTING WORD
![Page 124: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/124.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
![Page 125: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/125.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 126: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/126.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
if (node->end) (1)
![Page 127: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/127.jpg)
PRINTING ALGORITHM
Printing Result
CASE RESULT
CASE 1 FALSE
![Page 128: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/128.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 129: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/129.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
if (node->children[0] != NULL) (2)
![Page 130: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/130.jpg)
PRINTING ALGORITHM
Printing Result
CASE RESULT
CASE 1 FALSE
CASE 2 TRUE
string currentString = prefix + node->children[0]->value;
string currentString = “” +a;
string currentString = a;
alphabetize(node->children[0], currentString);
![Page 131: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/131.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 132: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/132.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
if (node->children[1] != NULL) (3)
![Page 133: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/133.jpg)
PRINTING ALGORITHM
Printing Result
CASE RESULT
CASE 3 FALSE
![Page 134: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/134.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 135: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/135.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
if (node->children[2] != NULL) (4)
![Page 136: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/136.jpg)
PRINTING ALGORITHM
Printing Result
CASE RESULT
CASE 4 FALSE
![Page 137: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/137.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 138: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/138.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
if (node->children[15] != NULL) (17)
![Page 139: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/139.jpg)
PRINTING ALGORITHM
Printing Result
CASE RESULT
CASE 17 TRUE
string currentString = prefix + node->children[15]->value;
string currentString = a + p;
string currentString = ap;
alphabetize(node->children[15], currentString);
![Page 140: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/140.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 141: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/141.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
if (node->children[15] != NULL) (18)
![Page 142: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/142.jpg)
PRINTING ALGORITHM
Printing Result
CASE RESULT
CASE 18 TRUE
string currentString = prefix + node->children[15]->value;
string currentString = ap + p;
string currentString = app;
alphabetize(node->children[15], currentString);
![Page 143: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/143.jpg)
PRINTING ALGORITHM
SIMILARLY
![Page 144: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/144.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 145: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/145.jpg)
PRINTING ALGORITHMRoot
0
a15
p15
p11
l4
e
bool end will be True
17
r12
m15
y
if (node->children[4] != NULL) (19)
![Page 146: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/146.jpg)
PRINTING ALGORITHMTrie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
![Page 147: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/147.jpg)
PRINTING ALGORITHM
Printing Result
CASE RESULT
CASE 19 TRUE
string currentString = prefix + node->children[15]->value;
string currentString = appl + e;
string currentString = apple;
alphabetize(node->children[4], currentString);
![Page 148: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/148.jpg)
PRINTING ALGORITHM
Printing Result
Print apple
![Page 149: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/149.jpg)
PRINTING ALGORITHM
SIMILARLY
![Page 150: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/150.jpg)
PRINTING ALGORITHM
Printing Result
Print apple
Print army
![Page 151: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/151.jpg)
SOURCE CODETrie.h
#ifndef TRIE_H
#define TRIE_H
#include <iostream>
#include <vector>
#include <string>
#include <assert.h>
#include <new>
using namespace std;
class Node
{
public:
char value;
bool end;
Node *children[26];
Node(char value);
};
class Trie
{
public:
Trie();
void addWord(string word);
bool searchForWord(string word);
void deleteWord(string word);
Node *getRoot();
void alphabetize(Node *, string);
private:
Node *root;
};
#endif // TRIE_H
![Page 152: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/152.jpg)
SOURCE CODETrie.c
#ifndef TRIE_CPP
#define TRIE_CPP
#include <iostream>
#include "trie.h"
using namespace std;
Node::Node(char value)
{
this->value = value;
end = false;
for(int i = 0; i < 26; ++i)
{
children[i] = NULL;
}
}
Trie::Trie()
{
root = new Node(' ');
root->end = true;
}
Node *Trie::getRoot()
{
return root;
}
![Page 153: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/153.jpg)
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;
}
}
}
![Page 154: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/154.jpg)
SOURCE CODETrie.c
bool Trie::searchForWord(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);
if(currentNode->children[index] != NULL)
{
currentNode = currentNode->children[index];
}
else
{
return false;
}
if(i == (int)word.size() - 1 && !currentNode->end)
{
return false;
}
}
return true;
}
![Page 155: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/155.jpg)
SOURCE CODETrie.c
void Trie::deleteWord(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);
if(currentNode->children[index] != NULL)
{
currentNode = currentNode->children[index];
}
else
{
return;
}
if(i == (int)word.size() - 1 && currentNode->end)
{
currentNode->end = false;
}
}
}
![Page 156: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/156.jpg)
SOURCE CODETrie.c
void Trie::alphabetize(Node *node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
for (int i = 0; i < 26; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value;
alphabetize(node->children[i], currentString);
}
}
}
#endif // TRIE_CPP
![Page 157: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/157.jpg)
SOURCE CODEMain.cpp
#include <iostream>
#include “trie.h”
using namespace std;
int main()
{
Trie * t = new Trie();
t->addWord("Carlos");
t->addWord("Perea");
t->addWord("Hello");
t->addWord("Ball");
t->addWord("Balloon");
t->addWord("Show");
t->addWord("Shower");
t->alphabetize(t->getRoot(), "");
t-> alphabetize(t->getRoot(), "");
return 0;
}
![Page 158: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/158.jpg)
OUTPUT
![Page 159: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/159.jpg)
APPLICATIONS OF TRIE DATA STRUCTURES
![Page 160: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/160.jpg)
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 target word.
![Page 161: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/161.jpg)
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.
![Page 162: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/162.jpg)
TRIES IN AUTO COMPLETE
![Page 163: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/163.jpg)
AUTO COMPLETE IN GOOGLE SEARCH
![Page 164: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/164.jpg)
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.
![Page 165: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/165.jpg)
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.
![Page 166: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/166.jpg)
SPELL CHECKERS
![Page 167: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/167.jpg)
PHONE BOOK SEARCH
![Page 168: Trie Data Structure](https://reader035.vdocuments.us/reader035/viewer/2022081420/5562c11fd8b42a09618b50ad/html5/thumbnails/168.jpg)
THANKS TO EVRYONE