168
DATA STRUCTURE TRIE Name: Mahadi Hassan ID: 1320133042 & Name: Mahmud Rahman Parag ID: 1320900042

Trie Data Structure

  • Upload
    -

  • View
    429

  • Download
    3

Embed Size (px)

DESCRIPTION

It Is About Trie Data Structure

Citation preview

Page 1: Trie Data Structure

DATA STRUCTURE

TRIE

Name: Mahadi Hassan

ID: 1320133042

&

Name: Mahmud Rahman Parag

ID: 1320900042

Page 2: Trie Data Structure

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

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

TYPES OF TRIE

1.Standard Tries

2.Compressed Tries

3.Suffix Tries

Page 5: Trie Data Structure

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

EXAMPLE:

Standard Trie For A Set Of Strings S

S = { bear, bell, bid, bull, buy, sell, stock, stop }

Page 7: Trie Data Structure

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

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

TRIE SPECIFICATION

Operations:

print

Function Print the word in the trie

Postcondition Trie either maybe full or not

Page 10: Trie Data Structure

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

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

INSERTION

Page 13: Trie Data Structure

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

INSERTION ALGORITHMInsert: Apple

Root

Page 15: Trie Data Structure

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

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

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

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

INSERTION ALGORITHMInsert: Apple

A p p l e

0 1 2 3 4

Page 20: Trie Data Structure

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

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

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

INSERTION ALGORITHMInsert: Apple

Root

currentNode

Page 24: Trie Data Structure

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

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

INSERTION ALGORITHMInsert: Apple

else

{

Node *newNode = new Node(currentChar);

currentNode->children[0] = newNode;

currentNode = newNode;

}

So………. Node Constructor

Page 27: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Node *newNode = new Node(currentChar);

So……….

currentChar a

Node(currentChar)

Page 28: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Node *newNode = new Node(currentChar);

So……….

currentChar a

aNode(currentChar)

Page 29: Trie Data Structure

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

INSERTION ALGORITHMInsert: Apple

currentNode->children[0] = newNode;

So……….

anewNode RootcurrentNode

0

Page 31: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

currentNode->children[0] = newNode;

So……….

a

newNode Root0

currentNode

Page 32: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

currentNode = newNode;

So……….

a

newNode Root0

currentNode

Page 33: Trie Data Structure

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

INSERTION ALGORITHMInsert: Apple

So……….

a

newNode Root0

currentNode

bool end will be false

Page 35: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Root0

acurrentNode

Page 36: Trie Data Structure

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

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

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

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

INSERTION ALGORITHMInsert: Apple

else

{

Node *newNode = new Node(currentChar);

currentNode->children[15] = newNode;

currentNode = newNode;

}

So………. Node Constructor

Page 41: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Node *newNode = new Node(currentChar);

So……….

currentChar p

Node(currentChar)

Page 42: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Node *newNode = new Node(currentChar);

So……….

currentChar p

pNode(currentChar)

Page 43: Trie Data Structure

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

INSERTION ALGORITHMInsert: Apple

currentNode->children[15] = newNode;

So……….

pnewNode acurrentNode

15

Page 45: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

currentNode->children[15] = newNode;

So……….

p

newNode a15

currentNode

Page 46: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

currentNode = newNode;

So……….

p

newNode a15

currentNode

Page 47: Trie Data Structure

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

INSERTION ALGORITHMInsert: Apple

So……….

p

newNode a15

currentNode

bool end will be false

Page 49: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Root0

a15

p

currentNode

Page 50: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

SIMILARLY

Page 51: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Root0

a15

p15

p

Page 52: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Root0

a15

p15

p11

l

Page 53: Trie Data Structure

INSERTION ALGORITHMInsert: Apple

Root0

a15

p15

p11

l4

e

Page 54: Trie Data Structure

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

INSERTION ALGORITHMInsert: Apple

Root0

a15

p15

p11

l4

e

bool end will be True

Page 56: Trie Data Structure

INSERTION ALGORITHM

Now Insert: Army

Page 57: Trie Data Structure

INSERTION ALGORITHMInsert: Army

A r m y

0 1 2 3

Page 58: Trie Data Structure

INSERTION ALGORITHMInsert: Army

A r m y

0 1 2 3

char currentChar = tolower(word.at(0));

currentChar a

Page 59: Trie Data Structure

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

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

DELETE ALGORITHMDelete: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

currentNode

Page 62: Trie Data Structure

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

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

INSERTION ALGORITHMInsert: Army

currentNode = currentNode->children[0];

So……….

Page 65: Trie Data Structure

INSERTION ALGORITHMInsert: Army

Root0

a15

p15

p11

l4

e

currentNode

bool end will be True

Page 66: Trie Data Structure

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

INSERTION ALGORITHMInsert: Army

A r m y

0 1 2 3

char currentChar = tolower(word.at(1));

currentChar r

Page 68: Trie Data Structure

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

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

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

INSERTION ALGORITHMInsert: Army

else

{

Node *newNode = new Node(currentChar);

currentNode->children[17] = newNode;

currentNode = newNode;

}

So………. Node Constructor

Page 72: Trie Data Structure

INSERTION ALGORITHMInsert: Army

Node *newNode = new Node(currentChar);

So……….

currentChar r

Node(currentChar)

Page 73: Trie Data Structure

INSERTION ALGORITHMInsert: Army

Node *newNode = new Node(currentChar);

So……….

currentChar r

rNode(currentChar)

Page 74: Trie Data Structure

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

INSERTION ALGORITHMInsert: Army

currentNode->children[17] = newNode;

So……….

Page 76: Trie Data Structure

INSERTION ALGORITHMInsert: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r

currentNode

Page 77: Trie Data Structure

INSERTION ALGORITHMInsert: Army

currentNode = newNode;

So……….

Page 78: Trie Data Structure

INSERTION ALGORITHMInsert: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r

currentNode

Page 79: Trie Data Structure

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

INSERTION ALGORITHMInsert: Army

SIMILARLY

Page 81: Trie Data Structure

INSERTION ALGORITHMInsert: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

Page 82: Trie Data Structure

INSERTION ALGORITHMInsert: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

Page 83: Trie Data Structure

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

INSERTION ALGORITHMInsert: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

Page 85: Trie Data Structure

DELETION

Page 86: Trie Data Structure

DELETION ALGORITHMDelete: Army

A r m y

0 1 2 3

char currentChar = tolower(word.at(0));

currentChar a

Page 87: Trie Data Structure

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

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

DELETION ALGORITHMDelete: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 90: Trie Data Structure

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

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

DELETION ALGORITHMDelete: Army

currentNode = currentNode->children[0];

So……….

Page 93: Trie Data Structure

DELETION ALGORITHMDelete: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 94: Trie Data Structure

DELETION ALGORITHMDelete: Army

A r m y

0 1 2 3

char currentChar = tolower(word.at(1));

currentChar r

Page 95: Trie Data Structure

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

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

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

DELETION ALGORITHMDelete: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 99: Trie Data Structure

DELETION ALGORITHMDelete: Army

SIMILARLY

Page 100: Trie Data Structure

DELETION ALGORITHMDelete: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 101: Trie Data Structure

DELETION ALGORITHMDelete: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 102: Trie Data Structure

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

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

SEARCH

Page 105: Trie Data Structure

SEARCHING ALGORITHMSearch: Army

A r m y

0 1 2 3

char currentChar = tolower(word.at(0));

currentChar a

Page 106: Trie Data Structure

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

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

SEARCHING ALGORITHMSearch: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 109: Trie Data Structure

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

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

SEARCHING ALGORITHMSearch: Army

currentNode = currentNode->children[0];

So……….

Page 112: Trie Data Structure

SEARCHING ALGORITHMSearch: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 113: Trie Data Structure

SEARCHING ALGORITHMSearch: Army

A r m y

0 1 2 3

char currentChar = tolower(word.at(1));

currentChar r

Page 114: Trie Data Structure

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

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

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

SEARCHING ALGORITHMSearch: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 118: Trie Data Structure

SEARCHING ALGORITHMSearch: Army

SIMILARLY

Page 119: Trie Data Structure

SEARCHING ALGORITHMSearch: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 120: Trie Data Structure

SEARCHING ALGORITHMSearch: Army

Root0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

currentNode

Page 121: Trie Data Structure

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

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

PRINTING WORD

Page 124: Trie Data Structure

PRINTING ALGORITHMRoot

0

a15

p15

p11

l4

e

bool end will be True

17

r12

m15

y

Page 125: Trie Data Structure

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

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

PRINTING ALGORITHM

Printing Result

CASE RESULT

CASE 1 FALSE

Page 128: Trie Data Structure

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

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

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

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

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

PRINTING ALGORITHM

Printing Result

CASE RESULT

CASE 3 FALSE

Page 134: Trie Data Structure

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

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

PRINTING ALGORITHM

Printing Result

CASE RESULT

CASE 4 FALSE

Page 137: Trie Data Structure

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

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

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

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

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

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

PRINTING ALGORITHM

SIMILARLY

Page 144: Trie Data Structure

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

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

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

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

PRINTING ALGORITHM

Printing Result

Print apple

Page 149: Trie Data Structure

PRINTING ALGORITHM

SIMILARLY

Page 150: Trie Data Structure

PRINTING ALGORITHM

Printing Result

Print apple

Print army

Page 151: Trie Data Structure

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

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

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

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

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

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

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

OUTPUT

Page 159: Trie Data Structure

APPLICATIONS OF TRIE DATA STRUCTURES

Page 160: Trie Data Structure

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

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

TRIES IN AUTO COMPLETE

Page 163: Trie Data Structure

AUTO COMPLETE IN GOOGLE SEARCH

Page 164: Trie Data Structure

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

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

SPELL CHECKERS

Page 167: Trie Data Structure

PHONE BOOK SEARCH

Page 168: Trie Data Structure

THANKS TO EVRYONE