Professional Documents
Culture Documents
TRIE
1.Standard Tries
2.Compressed Tries
3.Suffix Tries
STANDARD TRIE
The Standard Trie For A Set Of Strings S Is An Ordered Tree Such That:
The Paths From The External Nodes To The Root Yield The Strings Of S
EXAMPLE:
Standard Trie For A Set Of Strings S
A Standard Trie Uses O(n) Space. Operations (find, insert, remove) Take Time
d = Alphabet Size
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
TRIE SPECIFICATION
Operations:
print
Function Print the word in the trie
Postcondition Trie either maybe full or not
NODE STRUCTURE
Class Node
{
public:
char value; The Character Value (A – Z) / (a – z).
bool end; Indicates Whether This Node Completes A Word
Node *children[26]; Represents The 26 Letters In The Alphabet
}
NODE STRUCTURE
A Node Type
Character Boolean Pointer
Data Data Array
char value
bool end
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 \
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 0
INSERTION
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
1 2 2
0 … … …
0 0 6
NULL
INSERTION ALGORITHM
Insert: Apple
Root
INSERTION ALGORITHM
Insert: Apple
A p p l e
0 1 2 3 4
INSERTION ALGORITHM
Insert: Apple
A p p l e
0 1 2 3 4
INSERTION ALGORITHM
Insert: Apple
A p p l e
0 1 2 3 4
currentChar a
INSERTION ALGORITHM
Insert: Apple
int index = 0;
INSERTION ALGORITHM
Insert: Apple
Declare A Pointer Node Type Pointer Variable That Point To The Root
currentNode Root
Pointing To
INSERTION ALGORITHM
Insert: Apple
Root
currentNode
INSERTION ALGORITHM
Insert: Apple
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
1 2 2
0 … … … If 0 Pointing To The NULL?
0 0 6
NULL
INSERTION ALGORITHM
Insert: Apple
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
} Here 0 Is The Index Value ( a – ‘a’ )
Is 0 Pointing To The NULL?
false
1 2 2 YES!
0 … … …
0 0 6
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
NULL
character as one of its descendants
INSERTION ALGORITHM
Insert: Apple
So………. Node Constructor
else
{
Node *newNode = new Node(currentChar);
currentNode->children[0] = newNode;
currentNode = newNode;
}
INSERTION ALGORITHM
Insert: Apple
So……….
Node *newNode = new Node(currentChar);
currentChar a
Node(curre
ntChar)
INSERTION ALGORITHM
Insert: Apple
So……….
Node *newNode = new Node(currentChar);
currentChar a
Node(curre a
ntChar)
INSERTION ALGORITHM
Insert: Apple
So……….
Node *newNode = new Node(currentChar);
currentChar a
Node(curre a
ntChar)
newNode a
All 26 Children Of newNode Will Point To The NULL
INSERTION ALGORITHM
Insert: Apple
So……….
currentNode->children[0] = newNode;
a
INSERTION ALGORITHM
Insert: Apple
So……….
currentNode = newNode;
a
INSERTION ALGORITHM
Insert: Apple
Now Check If It Is The Last Character Of The Word Has Been Reached
if (i == word.size() - 1)
{
currentNode->end = true;
}
currentNode a
INSERTION ALGORITHM
Insert: Apple
A p p l e
0 1 2 3 4
currentChar p
INSERTION ALGORITHM
Insert: Apple
a
false
1 1 2 2
… … … If 15 Pointing To The NULL?
5 0 0 6
NULL
INSERTION ALGORITHM
Insert: Apple
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[15] != NULL)
{
currentNode = currentNode->children[15];
} Here 15 Is The Index Value ( p – ‘a’ )
a Is 15 Pointing To The NULL?
false
1 1 2 2 YES!
… … …
5 0 0 6
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
NULL
character as one of its descendants
INSERTION ALGORITHM
Insert: Apple
So………. Node Constructor
else
{
Node *newNode = new Node(currentChar);
currentNode->children[15] = newNode;
currentNode = newNode;
}
INSERTION ALGORITHM
Insert: Apple
So……….
Node *newNode = new Node(currentChar);
currentChar p
Node(curre
ntChar)
INSERTION ALGORITHM
Insert: Apple
So……….
Node *newNode = new Node(currentChar);
currentChar p
Node(curre p
ntChar)
INSERTION ALGORITHM
Insert: Apple
So……….
Node *newNode = new Node(currentChar);
currentChar p
Node(curre p
ntChar)
newNode p
All 26 Children Of newNode Will Point To The NULL
INSERTION ALGORITHM
Insert: Apple
So……….
currentNode->children[15] = newNode;
newNode p currentNode a
1
5
INSERTION ALGORITHM
Insert: Apple
So……….
currentNode->children[15] = newNode;
newNode currentNode a
1
5
p
INSERTION ALGORITHM
Insert: Apple
So……….
currentNode = newNode;
newNode currentNode a
1
5
p
INSERTION ALGORITHM
Insert: Apple
Now Check If It Is The Last Character Of The Word Has Been Reached
if (i == word.size() - 1)
{
currentNode->end = true;
}
newNode currentNode a
1
5
currentNode a
1
5
p
INSERTION ALGORITHM
Insert: Apple
SIMILARLY
INSERTION ALGORITHM
Insert: Apple
Root
0
a
1
5
p
1
5
p
INSERTION ALGORITHM
Insert: Apple
Root
0
a
1
5
p
1
5
p
1
1
l
INSERTION ALGORITHM
Insert: Apple
Root
0
a
1
5
p
1
5
p
1
1
l
4
e
INSERTION ALGORITHM
Insert: Apple
Now Check If It Is The Last Character Of The Word Has Been Reached
if (i == word.size() - 1)
{
currentNode->end = true;
}
a
1
5
p
1
5
p
1
1
l
4
A r m y
0 1 2 3
INSERTION ALGORITHM
Insert: Army
A r m y
0 1 2 3
currentChar a
INSERTION ALGORITHM
Insert: Army
int index = 0;
INSERTION ALGORITHM
Insert: Army
Declare A Pointer Node Type Pointer Variable That Point To The Root
currentNode Root
Pointing To
DELETE ALGORITHM
Delete: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
p
1
1
l
4
false
1 2 2
0 … … … If 0 Pointing To The NULL?
0 0 6
NULL
INSERTION ALGORITHM
Insert: Army
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
} Here 0 Is The Index Value ( a – ‘a’ )
Is 0 Pointing To The NULL?
false
1 2 2 NO!
0 … … …
0 0 6
So IF Statement Will Execute…………
NULL
INSERTION ALGORITHM
Insert: Army
So……….
currentNode = currentNode->children[0];
INSERTION ALGORITHM
Insert: Army
Root
0
currentNode
a
1
5
p
1
5
p
1
1
l
4
A r m y
0 1 2 3
currentChar r
INSERTION ALGORITHM
Insert: Army
a
false
1 1 2 2
… … … If 15 Pointing To The NULL?
7 0 0 6
NULL
INSERTION ALGORITHM
Insert: Army
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
} Here 17 Is The Index Value ( r – ‘a’ )
a Is 17 Pointing To The NULL?
false
1 1 2 2 YES!
… … …
7 0 0 6
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
NULL
character as one of its descendants
INSERTION ALGORITHM
Insert: Army
So………. Node Constructor
else
{
Node *newNode = new Node(currentChar);
currentNode->children[17] = newNode;
currentNode = newNode;
}
INSERTION ALGORITHM
Insert: Army
So……….
Node *newNode = new Node(currentChar);
currentChar r
Node(curre
ntChar)
INSERTION ALGORITHM
Insert: Army
So……….
Node *newNode = new Node(currentChar);
currentChar r
Node(curre r
ntChar)
INSERTION ALGORITHM
Insert: Army
So……….
Node *newNode = new Node(currentChar);
currentChar r
Node(curre r
ntChar)
newNode r
All 26 Children Of newNode Will Point To The NULL
INSERTION ALGORITHM
Insert: Army
So……….
currentNode->children[17] = newNode;
INSERTION ALGORITHM
Insert: Army
Root
0
currentNode a
1 1
5 7
p
1
5
r
p
1
1
l
4
currentNode a
1 1
5 7
p
1
5
r
p
1
1
l
4
SIMILARLY
INSERTION ALGORITHM
Insert: Army
Root
0
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
A r m y
0 1 2 3
currentChar a
DELETION ALGORITHM
Delete: Army
int index = 0;
DELETION ALGORITHM
Delete: Army
Declare A Pointer Node Type Pointer Variable That Point To The Root
currentNode Root
Pointing To
DELETION ALGORITHM
Delete: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
false
1 2 2
0 … … … If 0 Pointing To The NULL?
0 0 6
NULL
DELETION ALGORITHM
Delete: Army
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
} Here 0 Is The Index Value ( a – ‘a’ )
Is 0 Pointing To The NULL?
false
1 2 2 NO!
0 … … …
0 0 6
So IF Statement Will Execute…………
NULL
DELETION ALGORITHM
Delete: Army
So……….
currentNode = currentNode->children[0];
DELETION ALGORITHM
Delete: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
A r m y
0 1 2 3
currentChar r
DELETION ALGORITHM
Delete: Army
a
false
1 1 2 2
… … … If 15 Pointing To The NULL?
7 0 0 6
NULL
DELETION ALGORITHM
Delete: Army
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
} Here 17 Is The Index Value ( r – ‘a’ )
a Is 17 Pointing To The NULL?
false
1 1 2 2 NO!
… … …
7 0 0 6
So IF Statement Will Execute…………
NULL
DELETION ALGORITHM
Delete: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
SIMILARLY
DELETION ALGORITHM
Delete: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
bool end will be False So Word Couldn’t
y Be Found By Search So The Word Is
Deleted.
e
A r m y
0 1 2 3
currentChar a
SEARCHING ALGORITHM
SEARCH: Army
int index = 0;
SEARCHING ALGORITHM
Search: Army
Declare A Pointer Node Type Pointer Variable That Point To The Root
currentNode Root
Pointing To
SEARCHING ALGORITHM
Search: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
false
1 2 2
0 … … … If 0 Pointing To The NULL?
0 0 6
NULL
SEARCHING ALGORITHM
Search: Army
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
} Here 0 Is The Index Value ( a – ‘a’ )
Is 0 Pointing To The NULL?
false
1 2 2 NO!
0 … … …
0 0 6
So IF Statement Will Execute…………
NULL
SEARCHING ALGORITHM
Search: Army
So……….
currentNode = currentNode->children[0];
SEARCHING ALGORITHM
Search: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
A r m y
0 1 2 3
currentChar r
SEARCHING ALGORITHM
Search: Army
a
false
1 1 2 2
… … … If 15 Pointing To The NULL?
7 0 0 6
NULL
SEARCHING ALGORITHM
Search: Army
Check If The Current Node Has The Current Character As One Of Its
Descendants
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
} Here 17 Is The Index Value ( r – ‘a’ )
a Is 17 Pointing To The NULL?
false
1 1 2 2 NO!
… … …
7 0 0 6
So IF Statement Will Execute…………
NULL
SEARCHING ALGORITHM
Search: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
SIMILARLY
SEARCHING ALGORITHM
Search: Army
Root
0
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
currentNode
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
Return True. Item Found !
y
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
SIMILARLY
PRINTING ALGORITHM
Trie::alphabetize(Node * node, string prefix = "")
{
if (node->end)
cout << prefix << endl;
a
1 1
5 7
p
1
5
r
1
2
p
1
1 m
1
5
l
4
Print apple
PRINTING ALGORITHM
SIMILARLY
PRINTING ALGORITHM
Printing Result
Print apple
Print army
Trie.h
SOURCE CODE
#ifndef TRIE_H class Trie
#define TRIE_H {
public:
#include <iostream>
#include <vector> Trie();
#include <string> void addWord(string word);
#include <assert.h> bool searchForWord(string word);
#include <new> void deleteWord(string word);
Node *getRoot();
using namespace std; void alphabetize(Node *, string);
end = false;
#endif // TRIE_CPP
Main.cpp
SOURCE CODE
#include <iostream>
#include “trie.h” return 0;
}
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(), "");
• 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
TRIES IN AUTO COMPLETE