DATA STRUCTURE TRIE What Is TRIE Trie Is

  • Slides: 168
Download presentation
DATA STRUCTURE TRIE

DATA STRUCTURE TRIE

What Is TRIE? Trie Is An Efficient Information Retrieval Data Structure Also Called Digital

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.

Why Trie Data Structure? • Searching trees in general favor keys which are of

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”.

TYPES OF TRIE 1. Standard Tries 2. Compressed Tries 3. Suffix Tries

TYPES OF TRIE 1. Standard Tries 2. Compressed Tries 3. Suffix Tries

STANDARD TRIE The Standard Trie For A Set Of Strings S Is An Ordered

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

EXAMPLE: Standard Trie For A Set Of Strings S S = { bear, bell,

EXAMPLE: Standard Trie For A Set Of Strings S S = { bear, bell, bid, bull, buy, sell, stock, stop }

TIME COMPLEXITY A Standard Trie Uses O(n) Space. Operations (find, insert, remove) Take Time

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

TRIE SPECIFICATION Operations: add. Word Function Adds word to an. Postcondition Trie is not

TRIE SPECIFICATION Operations: add. Word Function Adds word to an. Postcondition Trie is not full search. Word Function Search a word in the trie Postcondition Returns true if the world is found and false otherwise. delete. Word 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

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)

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 Character Data A Node Type Pointer Array Boolean Data char value bool

NODE STRUCTURE Character Data A Node Type Pointer Array Boolean Data char value bool end 1 1 1 1 1 2 2 2 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 0

INSERTION

INSERTION

INSERTION ALGORITHM Insert: Apple First Create A Root That Has Empty String And Every

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 Root

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or To Lowercase. char current. Char = tolower(word. at(i));

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or To Lowercase. char current. Char = tolower(word. at(i)); Here, Suppose string s = “Apple” And Length Of String Is 5 So……….

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or To Lowercase. char current. Char = 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

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or

INSERTION ALGORITHM Insert: Apple Second Convert All Of The String’s Character To Uppercase Or To Lowercase. char current. Char = 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 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

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 char current. Char = tolower(word. at(0)); current. Char a

INSERTION ALGORITHM Insert: Apple Then Get The Correct Index For The Appropriate Character int

INSERTION ALGORITHM Insert: Apple Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char a int index = current. Char - 'a'; int index = a - 'a'; int index = 0;

INSERTION ALGORITHM Insert: Apple Declare A Pointer Node Type Pointer Variable That Point To

INSERTION ALGORITHM Insert: Apple Declare A Pointer Node Type Pointer Variable That Point To The Root Node *current. Node = root; current. Node Root Pointing To

INSERTION ALGORITHM Insert: Apple current. Node Root

INSERTION ALGORITHM Insert: Apple current. Node Root

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } false 1 2 2 0 … … … 0 0 6 NULL If 0 Pointing To The NULL?

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } Here 0 Is The Index Value ( a – ‘a’ ) false 1 2 2 0 … … … 0 0 6 NULL 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

INSERTION ALGORITHM Insert: Apple So………. Node Constructor else { Node *new. Node = new

INSERTION ALGORITHM Insert: Apple So………. Node Constructor else { Node *new. Node = new Node(current. Char); current. Node->children[0] = new. Node; current. Node = new. Node; }

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char Node(curre nt. Char) a

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char a Node(curre nt. Char) a

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char a Node(curre nt. Char) a new. Node a All 26 Children Of new. Node Will Point To The NULL

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[0] = new. Node; new. Node a current.

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[0] = new. Node; new. Node a current. Node Root 0

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[0] = new. Node; new. Node current. Node

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[0] = new. Node; new. Node current. Node Root 0 a

INSERTION ALGORITHM Insert: Apple So………. current. Node = new. Node; new. Node current. Node

INSERTION ALGORITHM Insert: Apple So………. current. Node = new. Node; new. Node current. Node Root 0 a

INSERTION ALGORITHM Insert: Apple Now Check If It Is The Last Character Of The

INSERTION ALGORITHM Insert: Apple Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() - 1) { current. Node->end = true; } 0 == 4? NO Won’t Execute

INSERTION ALGORITHM Insert: Apple So………. new. Node current. Node Root 0 a bool end

INSERTION ALGORITHM Insert: Apple So………. new. Node current. Node Root 0 a bool end will be false

INSERTION ALGORITHM Insert: Apple Root 0 current. Node a

INSERTION ALGORITHM Insert: Apple Root 0 current. Node a

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 char current. Char = tolower(word. at(1)); current. Char p

INSERTION ALGORITHM Insert: Apple Then Get The Correct Index For The Appropriate Character int

INSERTION ALGORITHM Insert: Apple Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char p int index = current. Char - 'a'; int index = p - 'a'; int index = 15;

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[15] != NULL) { current. Node = current. Node->children[15]; } a false 1 2 2 1 … … … 0 0 6 5 NULL If 15 Pointing To The NULL?

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Apple Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[15] != NULL) { current. Node = current. Node->children[15]; } Here 15 Is The Index Value ( p – ‘a’ ) a false 1 2 2 1 … … … 0 0 6 5 NULL 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

INSERTION ALGORITHM Insert: Apple So………. Node Constructor else { Node *new. Node = new

INSERTION ALGORITHM Insert: Apple So………. Node Constructor else { Node *new. Node = new Node(current. Char); current. Node->children[15] = new. Node; current. Node = new. Node; }

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char Node(curre nt. Char) p

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char p Node(curre nt. Char) p

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Apple So………. Node *new. Node = new Node(current. Char); current. Char p Node(curre nt. Char) p new. Node p All 26 Children Of new. Node Will Point To The NULL

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[15] = new. Node; new. Node p current.

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[15] = new. Node; new. Node p current. Node a 1 5

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[15] = new. Node; new. Node current. Node

INSERTION ALGORITHM Insert: Apple So………. current. Node->children[15] = new. Node; new. Node current. Node p a 1 5

INSERTION ALGORITHM Insert: Apple So………. current. Node = new. Node; new. Node current. Node

INSERTION ALGORITHM Insert: Apple So………. current. Node = new. Node; new. Node current. Node p a 1 5

INSERTION ALGORITHM Insert: Apple Now Check If It Is The Last Character Of The

INSERTION ALGORITHM Insert: Apple Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() - 1) { current. Node->end = true; } 1 == 4? NO Won’t Execute

INSERTION ALGORITHM Insert: Apple So………. new. Node current. Node p bool end will be

INSERTION ALGORITHM Insert: Apple So………. new. Node current. Node p bool end will be false a 1 5

INSERTION ALGORITHM Insert: Apple Root 0 current. Node a 1 5 p

INSERTION ALGORITHM Insert: Apple Root 0 current. Node a 1 5 p

INSERTION ALGORITHM Insert: Apple SIMILARLY

INSERTION ALGORITHM Insert: Apple SIMILARLY

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p 1 1 l

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p 1 1 l

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p 1 1 l 4

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p 1 1 l 4 e

INSERTION ALGORITHM Insert: Apple Now Check If It Is The Last Character Of The

INSERTION ALGORITHM Insert: Apple Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() - 1) { current. Node->end = true; } 4 == 4? YES Will Execute

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p 1 1 l 4

INSERTION ALGORITHM Insert: Apple Root 0 a 1 5 p 1 1 l 4 e bool end will be True

INSERTION ALGORITHM Now Insert: Army

INSERTION ALGORITHM Now Insert: Army

INSERTION ALGORITHM Insert: Army A r m y 0 1 2 3

INSERTION ALGORITHM Insert: Army A r m y 0 1 2 3

INSERTION ALGORITHM Insert: Army A r m y 0 1 2 3 char current.

INSERTION ALGORITHM Insert: Army A r m y 0 1 2 3 char current. Char = tolower(word. at(0)); current. Char a

INSERTION ALGORITHM Insert: Army Then Get The Correct Index For The Appropriate Character int

INSERTION ALGORITHM Insert: Army Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char a int index = current. Char - 'a'; int index = a - 'a'; int index = 0;

INSERTION ALGORITHM Insert: Army Declare A Pointer Node Type Pointer Variable That Point To

INSERTION ALGORITHM Insert: Army Declare A Pointer Node Type Pointer Variable That Point To The Root Node *current. Node = root; current. Node Root Pointing To

DELETE ALGORITHM Delete: Army Root 0 current. Node a 1 5 1 7 p

DELETE ALGORITHM Delete: Army Root 0 current. Node a 1 5 1 7 p 1 5 p 1 1 l 4 e bool end will be True

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } false 1 2 2 0 … … … 0 0 6 NULL If 0 Pointing To The NULL?

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } Here 0 Is The Index Value ( a – ‘a’ ) false 1 2 2 0 … … … 0 0 6 NULL Is 0 Pointing To The NULL? NO! So IF Statement Will Execute…………

INSERTION ALGORITHM Insert: Army So………. current. Node = current. Node->children[0];

INSERTION ALGORITHM Insert: Army So………. current. Node = current. Node->children[0];

INSERTION ALGORITHM Insert: Army Root 0 current. Node a 1 5 p 1 1

INSERTION ALGORITHM Insert: Army Root 0 current. Node a 1 5 p 1 1 l 4 e bool end will be True

INSERTION ALGORITHM Insert: Army Now Check If It Is The Last Character Of The

INSERTION ALGORITHM Insert: Army Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() - 1) { current. Node->end = true; } 0 == 3? NO Won’t Execute

INSERTION ALGORITHM Insert: Army A r m y 0 1 2 3 char current.

INSERTION ALGORITHM Insert: Army A r m y 0 1 2 3 char current. Char = tolower(word. at(1)); current. Char r

INSERTION ALGORITHM Insert: Army Then Get The Correct Index For The Appropriate Character int

INSERTION ALGORITHM Insert: Army Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char r int index = current. Char - 'a'; int index = 17;

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[17] != NULL) { current. Node = current. Node->children[17]; } a false 1 2 2 1 … … … 0 0 6 7 NULL If 15 Pointing To The NULL?

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As

INSERTION ALGORITHM Insert: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[17] != NULL) { current. Node = current. Node->children[17]; } Here 17 Is The Index Value ( r – ‘a’ ) a false 1 2 2 1 … … … 0 0 6 7 NULL 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

INSERTION ALGORITHM Insert: Army So………. Node Constructor else { Node *new. Node = new

INSERTION ALGORITHM Insert: Army So………. Node Constructor else { Node *new. Node = new Node(current. Char); current. Node->children[17] = new. Node; current. Node = new. Node; }

INSERTION ALGORITHM Insert: Army So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Army So………. Node *new. Node = new Node(current. Char); current. Char Node(curre nt. Char) r

INSERTION ALGORITHM Insert: Army So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Army So………. Node *new. Node = new Node(current. Char); current. Char r Node(curre nt. Char) r

INSERTION ALGORITHM Insert: Army So………. Node *new. Node = new Node(current. Char); current. Char

INSERTION ALGORITHM Insert: Army So………. Node *new. Node = new Node(current. Char); current. Char r Node(curre nt. Char) r new. Node r All 26 Children Of new. Node Will Point To The NULL

INSERTION ALGORITHM Insert: Army So………. current. Node->children[17] = new. Node;

INSERTION ALGORITHM Insert: Army So………. current. Node->children[17] = new. Node;

INSERTION ALGORITHM Insert: Army Root 0 a current. Node 1 5 p 1 5

INSERTION ALGORITHM Insert: Army Root 0 a current. Node 1 5 p 1 5 1 7 r p 1 1 l 4 e bool end will be True

INSERTION ALGORITHM Insert: Army So………. current. Node = new. Node;

INSERTION ALGORITHM Insert: Army So………. current. Node = new. Node;

INSERTION ALGORITHM Insert: Army Root 0 a current. Node 1 5 p 1 5

INSERTION ALGORITHM Insert: Army Root 0 a current. Node 1 5 p 1 5 1 7 r p 1 1 l 4 e bool end will be True

INSERTION ALGORITHM Insert: Army Now Check If It Is The Last Character Of The

INSERTION ALGORITHM Insert: Army Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() - 1) { current. Node->end = true; } 1 == 3? NO Won’t Execute

INSERTION ALGORITHM Insert: Army SIMILARLY

INSERTION ALGORITHM Insert: Army SIMILARLY

INSERTION ALGORITHM Insert: Army Root 0 a 1 5 p r 1 5 p

INSERTION ALGORITHM Insert: Army Root 0 a 1 5 p r 1 5 p 1 1 l 1 7 1 2 m 1 5 4 e bool end will be True

INSERTION ALGORITHM Insert: Army Root 0 a 1 5 p r 1 5 p

INSERTION ALGORITHM Insert: Army Root 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

INSERTION ALGORITHM Insert: Army Now Check If It Is The Last Character Of The

INSERTION ALGORITHM Insert: Army Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() - 1) { current. Node->end = true; } 3 == 3? YES Will Execute

INSERTION ALGORITHM Insert: Army Root 0 a 1 5 p r 1 5 p

INSERTION ALGORITHM Insert: Army Root 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

DELETION

DELETION

DELETION ALGORITHM Delete: Army A r m y 0 1 2 3 char current.

DELETION ALGORITHM Delete: Army A r m y 0 1 2 3 char current. Char = tolower(word. at(0)); current. Char a

DELETION ALGORITHM Delete: Army Then Get The Correct Index For The Appropriate Character int

DELETION ALGORITHM Delete: Army Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char a int index = current. Char - 'a'; int index = a - 'a'; int index = 0;

DELETION ALGORITHM Delete: Army Declare A Pointer Node Type Pointer Variable That Point To

DELETION ALGORITHM Delete: Army Declare A Pointer Node Type Pointer Variable That Point To The Root Node *current. Node = root; current. Node Root Pointing To

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } false 1 2 2 0 … … … 0 0 6 NULL If 0 Pointing To The NULL?

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } Here 0 Is The Index Value ( a – ‘a’ ) false 1 2 2 0 … … … 0 0 6 NULL Is 0 Pointing To The NULL? NO! So IF Statement Will Execute…………

DELETION ALGORITHM Delete: Army So………. current. Node = current. Node->children[0];

DELETION ALGORITHM Delete: Army So………. current. Node = current. Node->children[0];

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

DELETION ALGORITHM Delete: Army A r m y 0 1 2 3 char current.

DELETION ALGORITHM Delete: Army A r m y 0 1 2 3 char current. Char = tolower(word. at(1)); current. Char r

DELETION ALGORITHM Delete: Army Then Get The Correct Index For The Appropriate Character int

DELETION ALGORITHM Delete: Army Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char r int index = current. Char - 'a'; int index = 17;

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[17] != NULL) { current. Node = current. Node->children[17]; } a false 1 2 2 1 … … … 0 0 6 7 NULL If 15 Pointing To The NULL?

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As

DELETION ALGORITHM Delete: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[17] != NULL) { current. Node = current. Node->children[17]; } Here 17 Is The Index Value ( r – ‘a’ ) a false 1 2 2 1 … … … 0 0 6 7 NULL Is 17 Pointing To The NULL? NO! So IF Statement Will Execute…………

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

DELETION ALGORITHM Delete: Army SIMILARLY

DELETION ALGORITHM Delete: Army SIMILARLY

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1

DELETION ALGORITHM Delete: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

DELETION ALGORITHM Delete: Army Now Check If It Is The Last Character Of The

DELETION ALGORITHM Delete: Army Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() – 1 && current. Node->end) { current. Node->end = false; } 3 == 3? && current. Node end? YES Will Execute

DELETION ALGORITHM Delete: Army Root 0 a 1 5 p r 1 5 p

DELETION ALGORITHM Delete: Army Root 0 a 1 5 p r 1 5 p 1 1 l 4 e 1 7 1 2 m 1 5 y bool end will be False So Word Couldn’t Be Found By Search So The Word Is Deleted. bool end will be True

SEARCH

SEARCH

SEARCHING ALGORITHM Search: Army A r m y 0 1 2 3 char current.

SEARCHING ALGORITHM Search: Army A r m y 0 1 2 3 char current. Char = tolower(word. at(0)); current. Char a

SEARCHING ALGORITHM SEARCH: Army Then Get The Correct Index For The Appropriate Character int

SEARCHING ALGORITHM SEARCH: Army Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char a int index = current. Char - 'a'; int index = a - 'a'; int index = 0;

SEARCHING ALGORITHM Search: Army Declare A Pointer Node Type Pointer Variable That Point To

SEARCHING ALGORITHM Search: Army Declare A Pointer Node Type Pointer Variable That Point To The Root Node *current. Node = root; current. Node Root Pointing To

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } false 1 2 2 0 … … … 0 0 6 NULL If 0 Pointing To The NULL?

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[0] != NULL) { current. Node = current. Node->children[0]; } Here 0 Is The Index Value ( a – ‘a’ ) false 1 2 2 0 … … … 0 0 6 NULL Is 0 Pointing To The NULL? NO! So IF Statement Will Execute…………

SEARCHING ALGORITHM Search: Army So………. current. Node = current. Node->children[0];

SEARCHING ALGORITHM Search: Army So………. current. Node = current. Node->children[0];

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

SEARCHING ALGORITHM Search: Army A r m y 0 1 2 3 char current.

SEARCHING ALGORITHM Search: Army A r m y 0 1 2 3 char current. Char = tolower(word. at(1)); current. Char r

SEARCHING ALGORITHM Search: Army Then Get The Correct Index For The Appropriate Character int

SEARCHING ALGORITHM Search: Army Then Get The Correct Index For The Appropriate Character int index = current. Char - 'a'; So………. current. Char r int index = current. Char - 'a'; int index = 17;

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[17] != NULL) { current. Node = current. Node->children[17]; } a false 1 2 2 1 … … … 0 0 6 7 NULL If 15 Pointing To The NULL?

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As

SEARCHING ALGORITHM Search: Army Check If The Current Node Has The Current Character As One Of Its Descendants if (current. Node->children[17] != NULL) { current. Node = current. Node->children[17]; } Here 17 Is The Index Value ( r – ‘a’ ) a false 1 2 2 1 … … … 0 0 6 7 NULL Is 17 Pointing To The NULL? NO! So IF Statement Will Execute…………

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

SEARCHING ALGORITHM Search: Army SIMILARLY

SEARCHING ALGORITHM Search: Army SIMILARLY

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1

SEARCHING ALGORITHM Search: Army Root 0 current. Node a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

SEARCHING ALGORITHM Search: Army Now Check If It Is The Last Character Of The

SEARCHING ALGORITHM Search: Army Now Check If It Is The Last Character Of The Word Has Been Reached if (i == word. size() – 1 && current. Node->end) { return true; } 3 == 3? && current. Node end? YES Will Execute

SEARCHING ALGORITHM Search: Army Root 0 a 1 5 p r 1 5 p

SEARCHING ALGORITHM Search: Army Root 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 Return True. Item Found ! y e bool end will be True

PRINTING WORD

PRINTING WORD

PRINTING ALGORITHM Root 0 a 1 5 p r 1 5 p 1 1

PRINTING ALGORITHM Root 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Root if (node->end) (1) 0 a 1 5 p r 1 5

PRINTING ALGORITHM Root if (node->end) (1) 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Printing Result CASE RESULT CASE 1 FALSE

PRINTING ALGORITHM Printing Result CASE RESULT CASE 1 FALSE

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Root if (node->children[0] != NULL) (2) 0 a 1 5 p r

PRINTING ALGORITHM Root if (node->children[0] != NULL) (2) 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Printing Result CASE RESULT CASE 1 FALSE CASE 2 TRUE string current.

PRINTING ALGORITHM Printing Result CASE RESULT CASE 1 FALSE CASE 2 TRUE string current. String = prefix + node->children[0]->value; string current. String = “” +a; string current. String = a; alphabetize(node->children[0], current. String);

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Root if (node->children[1] != NULL) (3) 0 a 1 5 p r

PRINTING ALGORITHM Root if (node->children[1] != NULL) (3) 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Printing Result CASE RESULT CASE 3 FALSE

PRINTING ALGORITHM Printing Result CASE RESULT CASE 3 FALSE

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Root if (node->children[2] != NULL) (4) 0 a 1 5 p r

PRINTING ALGORITHM Root if (node->children[2] != NULL) (4) 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Printing Result CASE RESULT CASE 4 FALSE

PRINTING ALGORITHM Printing Result CASE RESULT CASE 4 FALSE

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Root if (node->children[15] != NULL) (17) 0 a 1 5 p r

PRINTING ALGORITHM Root if (node->children[15] != NULL) (17) 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Printing Result CASE RESULT CASE 17 TRUE string current. String = prefix

PRINTING ALGORITHM Printing Result CASE RESULT CASE 17 TRUE string current. String = prefix + node->children[15]->value; string current. String = a + p; string current. String = ap; alphabetize(node->children[15], current. String);

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Root if (node->children[15] != NULL) (18) 0 a 1 5 p r

PRINTING ALGORITHM Root if (node->children[15] != NULL) (18) 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Printing Result CASE RESULT CASE 18 TRUE string current. String = prefix

PRINTING ALGORITHM Printing Result CASE RESULT CASE 18 TRUE string current. String = prefix + node->children[15]->value; string current. String = ap + p; string current. String = app; alphabetize(node->children[15], current. String);

PRINTING ALGORITHM SIMILARLY

PRINTING ALGORITHM SIMILARLY

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Root if (node->children[4] != NULL) (19) 0 a 1 5 p r

PRINTING ALGORITHM Root if (node->children[4] != NULL) (19) 0 a 1 5 p r 1 5 p 1 1 l 4 1 7 1 2 m 1 5 y e bool end will be True

PRINTING ALGORITHM Trie: : alphabetize(Node * node, string prefix = "") { if (node->end)

PRINTING ALGORITHM 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } }

PRINTING ALGORITHM Printing Result CASE RESULT CASE 19 TRUE string current. String = prefix

PRINTING ALGORITHM Printing Result CASE RESULT CASE 19 TRUE string current. String = prefix + node->children[15]->value; string current. String = appl + e; string current. String = apple; alphabetize(node->children[4], current. String);

PRINTING ALGORITHM Printing Result Print apple

PRINTING ALGORITHM Printing Result Print apple

PRINTING ALGORITHM SIMILARLY

PRINTING ALGORITHM SIMILARLY

PRINTING ALGORITHM Printing Result Print apple Print army

PRINTING ALGORITHM Printing Result Print apple Print army

Trie. h SOURCE CODE #ifndef TRIE_H class Trie #define TRIE_H { public: #include <iostream>

Trie. h SOURCE CODE #ifndef TRIE_H class Trie #define TRIE_H { public: #include <iostream> #include <vector> Trie(); #include <string> void add. Word(string word); #include <assert. h> bool search. For. Word(string word); #include <new> void delete. Word(string word); Node *get. Root(); using namespace std; class Node void alphabetize(Node *, string); private: { public: Node *root; }; char value; bool end; Node *children[26]; Node(char value); }; #endif // TRIE_H

Trie. c SOURCE CODE #ifndef TRIE_CPP Trie: : Trie() #define TRIE_CPP { root =

Trie. c SOURCE CODE #ifndef TRIE_CPP Trie: : Trie() #define TRIE_CPP { root = new Node(' '); #include <iostream> root->end = true; #include "trie. h" } using namespace std; Node *Trie: : get. Root() { Node: : Node(char value) { } this->value = value; end = false; for(int i = 0; i < 26; ++i) { children[i] = NULL; } } return root;

SOURCE CODE Trie. c void Trie: : add. Word(string word) else { { Node

SOURCE CODE Trie. c void Trie: : add. Word(string word) else { { Node * current. Node = root; // the current node doesn't have the current character as one of its decendants Node * new. Node = new Node(current. Char); for (int i = 0; i < (int)word. size(); ++i) current. Node->children[index] = new. Node; { current. Node = new. Node; char current. Char = tolower(word. at(i)); } int index = current. Char - 'a'; assert(index >= 0); if (i == (int)word. size() - 1) // Makes sure the character is between a-z { if (current. Node->children[index] != NULL) // the last character of the word has been reached { current. Node->end = true; // check if the current node has the current character as one of its decendants } current. Node = current. Node->children[index]; } } }

SOURCE CODE Trie. c bool Trie: : search. For. Word(string word) else { {

SOURCE CODE Trie. c bool Trie: : search. For. Word(string word) else { { return false; Node *current. Node = root; } for(int i = 0; i < (int)word. size(); ++i) if(i == (int)word. size() - 1 && !current. Node->end) { { char current. Char = tolower(word. at(i)); return false; int index = current. Char - 'a'; } assert(index >= 0); } if(current. Node->children[index] != NULL) return true; } { current. Node = current. Node->children[index]; }

SOURCE CODE Trie. c void Trie: : delete. Word(string word) else { { return;

SOURCE CODE Trie. c void Trie: : delete. Word(string word) else { { return; Node *current. Node = root; } for(int i = 0; i < (int)word. size(); ++i) if(i == (int)word. size() - 1 && current. Node->end) { { char current. Char = tolower(word. at(i)); current. Node->end = false; int index = current. Char - 'a'; } } assert(index >= 0); } if(current. Node->children[index] != NULL) { current. Node = current. Node->children[index]; }

SOURCE CODE Trie. c void Trie: : alphabetize(Node *node, string prefix = "") {

SOURCE CODE Trie. 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 current. String = prefix + node->children[i]->value; alphabetize(node->children[i], current. String); } } } #endif // TRIE_CPP

Main. cpp SOURCE CODE #include <iostream> #include “trie. h” return 0; using namespace std;

Main. cpp SOURCE CODE #include <iostream> #include “trie. h” return 0; using namespace std; int main() { Trie * t = new Trie(); t->add. Word("Carlos"); t->add. Word("Perea"); t->add. Word("Hello"); t->add. Word("Balloon"); t->add. Word("Shower"); t->alphabetize(t->get. Root(), ""); t-> alphabetize(t->get. Root(), ""); }

OUTPUT

OUTPUT

APPLICATIONS OF TRIE DATA STRUCTURES

APPLICATIONS OF TRIE DATA STRUCTURES

TRIES IN AUTO COMPLETE • Since a trie is a tree-like data structure in

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

TRIES IN AUTO COMPLETE • Auto-complete functionality is used widely over the internet and

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.

TRIES IN AUTO COMPLETE

TRIES IN AUTO COMPLETE

AUTO COMPLETE IN GOOGLE SEARCH

AUTO COMPLETE IN GOOGLE SEARCH

WHY TRIES IN AUTO COMPLETE • Implementing auto complete using a trie is easy.

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.

AUTOMATIC COMMAND COMPLETION • When using an operating system such as Unix or DOS,

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.

SPELL CHECKERS

SPELL CHECKERS

PHONE BOOK SEARCH

PHONE BOOK SEARCH

THANKS

THANKS