ObjectivesMore practice with recursion.Practice writing some tem.docxvannagoforth
Objectives
More practice with recursion.
Practice writing some template functions.
Use stack ADT to implement given algorithms.
Look at some common applications of stacks.Description
In this assignment, you will be using the Stack abstract data type we developed this week and discussed in our weeks lectures, to implement 4 functions that use a stack data type to accomplish their algorithms. The functions range from relatively simple, straight forward use of a stack, to a bit more complex. But in all 4 cases, you should only need to use the abstract stack interface functions push(), pop(), top(), and isEmpty() in order to successfully use our Stack type for this assignment and the function you are asked to write.
For this assignment you need to perform the following tasks.
1. In the rst task, we will write a function that will check if a string of parenthesis is matching. Strings will be given to the function of the format "(()((())))", using only opening "(" and closing ")". Your function should be named doParenthesisMatch(). It takes a single string as input, and it returns a boolean result of true if the parenthesis match, and false otherwise.
The algorithm to check for matching parenthesis using a stack is fairly simple. A pseudo-code description migth be
for each charcter c in expression do
if c is an open paren ’(’ push it on stack
if c is a close paren ’)’:
do
if stack is empty
answer is false, because we can’t match the current ’)’
else pop stack, because we just matched an open ’(’ with a close ’)’
done
done
Your function will be given a C++ string class as input. It is relatively simple to parse each character of a C++ string. Here is an example for loop to do this:
s = "(())";
for (size_t index = 0; index < s.length(); index++)
{ char c = s[index];
// handle char c of the string expression s here }
2. In the next task, we will also write a function that decodes a string expression. Given a sequence consisting of ’I’ and ’D’ characters, where ’I’ denotes increasing and ’D’ denotes decreasing, decode the given sequence to construct a "minimum number" without repeated digits.
An example of some inputs and outputs will make it clear what is meant by a "minimal number".
sequence
output
IIII
->
12345
DDDD
->
54321
ID
->
132
IDIDII
->
1325467
IIDDIDID
->
125437698
If you are given 4 characters in the input sequence, the result will be a number with 5 characters, and further only the digits ’12345’ would be in the "minimal number" output. Each ’I’ and ’D’ in the input denotes that the next digit in the output should go up (increase) or go down (decrease) respectively. As you can see for the input sequence "IDI" you have to accommodate the sequence, thus the output goes from 1 to 3 for the initial increase, becase in order to then decrease, and also only have the digits ’123’, we need 3 for the second digit so the third can decrease to 2.
Take a moment to think how you might write an algorithm to solve this problem? It may be di cult to thi ...
ObjectivesMore practice with recursion.Practice writing some tem.docxvannagoforth
Objectives
More practice with recursion.
Practice writing some template functions.
Use stack ADT to implement given algorithms.
Look at some common applications of stacks.Description
In this assignment, you will be using the Stack abstract data type we developed this week and discussed in our weeks lectures, to implement 4 functions that use a stack data type to accomplish their algorithms. The functions range from relatively simple, straight forward use of a stack, to a bit more complex. But in all 4 cases, you should only need to use the abstract stack interface functions push(), pop(), top(), and isEmpty() in order to successfully use our Stack type for this assignment and the function you are asked to write.
For this assignment you need to perform the following tasks.
1. In the rst task, we will write a function that will check if a string of parenthesis is matching. Strings will be given to the function of the format "(()((())))", using only opening "(" and closing ")". Your function should be named doParenthesisMatch(). It takes a single string as input, and it returns a boolean result of true if the parenthesis match, and false otherwise.
The algorithm to check for matching parenthesis using a stack is fairly simple. A pseudo-code description migth be
for each charcter c in expression do
if c is an open paren ’(’ push it on stack
if c is a close paren ’)’:
do
if stack is empty
answer is false, because we can’t match the current ’)’
else pop stack, because we just matched an open ’(’ with a close ’)’
done
done
Your function will be given a C++ string class as input. It is relatively simple to parse each character of a C++ string. Here is an example for loop to do this:
s = "(())";
for (size_t index = 0; index < s.length(); index++)
{ char c = s[index];
// handle char c of the string expression s here }
2. In the next task, we will also write a function that decodes a string expression. Given a sequence consisting of ’I’ and ’D’ characters, where ’I’ denotes increasing and ’D’ denotes decreasing, decode the given sequence to construct a "minimum number" without repeated digits.
An example of some inputs and outputs will make it clear what is meant by a "minimal number".
sequence
output
IIII
->
12345
DDDD
->
54321
ID
->
132
IDIDII
->
1325467
IIDDIDID
->
125437698
If you are given 4 characters in the input sequence, the result will be a number with 5 characters, and further only the digits ’12345’ would be in the "minimal number" output. Each ’I’ and ’D’ in the input denotes that the next digit in the output should go up (increase) or go down (decrease) respectively. As you can see for the input sequence "IDI" you have to accommodate the sequence, thus the output goes from 1 to 3 for the initial increase, becase in order to then decrease, and also only have the digits ’123’, we need 3 for the second digit so the third can decrease to 2.
Take a moment to think how you might write an algorithm to solve this problem? It may be di cult to thi ...
Lazy Java by Mario Fusco
Like all imperative languages Java is, with some minor but notable exceptions, an eagerly evaluated programming language. Nevertheless the introduction of lambdas in Java 8 also allowed the adoption of some lazy patterns and data structures that are more typically employed in functional languages. Streams represent the most evident example of how also native Java API has taken advantage of laziness, but there is a number of other interesting scenarios where laziness can be an effective solution to quite common problems. In fact laziness is the only possible technique to process potentially infinite amount of data, or more in general to delay the expensive evaluation of an expression only when and if it is necessary. But laziness is even more than that: for instance the reader monad delays not only a computation but also the need of external dependencies thus lowering the abuse of dependency injection, while a trampoline uses laziness to delay and then linearize recursive calls preventing the overflow of the stack. The purpose of this talk is illustrating why and how implementing laziness in Java with practical examples delivered with both slides and live coding sessions.
Mario Fusco - Lazy Java - Codemotion Milan 2018Codemotion
Like all imperative languages Java is eagerly evaluated, but the introduction of lambdas allowed the adoption of some lazy patterns and data structures that are typically functional. Streams are the most evident example of a native Java API using laziness, but there are other cases where laziness is an effective solution to common problems. In fact it makes possible to process infinite amount of data, but it is even more than that. This talk shows why and how implementing laziness in Java with practical examples delivered with both slides and live coding sessions.
Data Structure- Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations −
PUSH, POP, PEEP
Lazy Java by Mario Fusco
Like all imperative languages Java is, with some minor but notable exceptions, an eagerly evaluated programming language. Nevertheless the introduction of lambdas in Java 8 also allowed the adoption of some lazy patterns and data structures that are more typically employed in functional languages. Streams represent the most evident example of how also native Java API has taken advantage of laziness, but there is a number of other interesting scenarios where laziness can be an effective solution to quite common problems. In fact laziness is the only possible technique to process potentially infinite amount of data, or more in general to delay the expensive evaluation of an expression only when and if it is necessary. But laziness is even more than that: for instance the reader monad delays not only a computation but also the need of external dependencies thus lowering the abuse of dependency injection, while a trampoline uses laziness to delay and then linearize recursive calls preventing the overflow of the stack. The purpose of this talk is illustrating why and how implementing laziness in Java with practical examples delivered with both slides and live coding sessions.
Mario Fusco - Lazy Java - Codemotion Milan 2018Codemotion
Like all imperative languages Java is eagerly evaluated, but the introduction of lambdas allowed the adoption of some lazy patterns and data structures that are typically functional. Streams are the most evident example of a native Java API using laziness, but there are other cases where laziness is an effective solution to common problems. In fact it makes possible to process infinite amount of data, but it is even more than that. This talk shows why and how implementing laziness in Java with practical examples delivered with both slides and live coding sessions.
Data Structure- Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations −
PUSH, POP, PEEP
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
CFD analysis is incredibly effective at solving mysteries and improving the performance of complex systems!
Here's a great example: At a large natural gas-fired power plant, where they use waste heat to generate steam and energy, they were puzzled that their boiler wasn't producing as much steam as expected.
R&R and Tetra Engineering Group Inc. were asked to solve the issue with reduced steam production.
An inspection had shown that a significant amount of hot flue gas was bypassing the boiler tubes, where the heat was supposed to be transferred.
R&R Consult conducted a CFD analysis, which revealed that 6.3% of the flue gas was bypassing the boiler tubes without transferring heat. The analysis also showed that the flue gas was instead being directed along the sides of the boiler and between the modules that were supposed to capture the heat. This was the cause of the reduced performance.
Based on our results, Tetra Engineering installed covering plates to reduce the bypass flow. This improved the boiler's performance and increased electricity production.
It is always satisfying when we can help solve complex challenges like this. Do your systems also need a check-up or optimization? Give us a call!
Work done in cooperation with James Malloy and David Moelling from Tetra Engineering.
More examples of our work https://www.r-r-consult.dk/en/cases-en/
Water scarcity is the lack of fresh water resources to meet the standard water demand. There are two type of water scarcity. One is physical. The other is economic water scarcity.
Student information management system project report ii.pdfKamal Acharya
Our project explains about the student management. This project mainly explains the various actions related to student details. This project shows some ease in adding, editing and deleting the student details. It also provides a less time consuming process for viewing, adding, editing and deleting the marks of the students.
Welcome to WIPAC Monthly the magazine brought to you by the LinkedIn Group Water Industry Process Automation & Control.
In this month's edition, along with this month's industry news to celebrate the 13 years since the group was created we have articles including
A case study of the used of Advanced Process Control at the Wastewater Treatment works at Lleida in Spain
A look back on an article on smart wastewater networks in order to see how the industry has measured up in the interim around the adoption of Digital Transformation in the Water Industry.
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
2. What Is TRIE?
Trie Is An Efficient Information Retrieval Data Structure Also Called
Digital Tree And Sometimes Radix Tree Or Prefix Tree (As They Can
Be Searched By Prefixes), Is An Ordered Tree Data Structure That Is
Used To Store A Dynamic Set Or Associative Array Where The Keys
Are Usually Strings.
3. Why Trie Data Structure?
• Searching trees in general favor keys which are of fixed size
since this leads to efficient storage management.
• However in case of applications which are retrieval based and
which call for keys varying length, tries provide better options.
• Tries are also called as Lexicographic Search trees.
• The name trie (pronounced as “try”)originated from the word
“retrieval”.
5. STANDARD TRIE
The Standard Trie For A Set Of Strings S Is An Ordered Tree Such That:
Each Node Labeled With A Character (Without Root).
The Children Of A Node Are Alphabetically Ordered.
The Paths From The External Nodes To The Root Yield The Strings Of S
7. TIME COMPLEXITY
A Standard Trie Uses O(n) Space. Operations (find, insert, remove) Take Time
O(dm) Each, Where:
n = Total Size Of The Strings In S,
m = Size Of The String Parameter Of The Operation
d = Alphabet Size
8. TRIE SPECIFICATION
Operations:
addWord
Function Adds word to an .
Postcondition Trie is not full
searchWord
Function Search a word in the trie
Postcondition Returns true if the world is found and false otherwise.
deleteWord
Function Delete a word in the trie
Postcondition Trie is not empty
10. NODE STRUCTURE
Class Node
{
public:
char value;
bool end;
Node *children[26];
}
The Character Value (A – Z) / (a – z).
Indicates Whether This Node Completes A Word
Represents The 26 Letters In The Alphabet
11. NODE STRUCTURE
char value
bool end
0 1 2 3 4 5 6 7 8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
Character
Data
Boolean
Data
A Node Type
Pointer
Array
0
13. INSERTION ALGORITHM
Insert: Apple
First Create A Root That Has Empty String And Every Single Pointer Array
Must Point To The NULL (Default). And Boolean Value Of Every Node Must Be
false By Default.
false
0 …
1
0
…
2
0
…
2
6
NULL
16. INSERTION ALGORITHM
Insert: Apple
Second Convert All Of The String’s Character To Uppercase Or To Lowercase.
char currentChar = tolower(word.at(i));
Here, Suppose string s = “Apple” And Length Of String Is 5 So……….
17. INSERTION ALGORITHM
Insert: Apple
Second Convert All Of The String’s Character To Uppercase Or To Lowercase.
char currentChar = tolower(word.at(i));
Here, Suppose string s = “Apple” And Length Of String Is 5 So……….
s[0] = A, s[1] = p, s[2] = p, s[3] = l, s[4] = e
18. INSERTION ALGORITHM
Insert: Apple
Second Convert All Of The String’s Character To Uppercase Or To Lowercase.
char currentChar = tolower(word.at(i));
A p p l e
0 1 2 3 4
Here, Suppose string s = “Apple” And Length Of String Is 5 So……….
s[0] = A, s[1] = p, s[2] = p, s[3] = l, s[4] = e
21. INSERTION ALGORITHM
Insert: Apple
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
24. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
25. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
YES!
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
character as one of its descendants
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
29. INSERTION ALGORITHM
Insert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar a
a
Node(curre
ntChar)
a
newNode
All 26 Children Of newNode Will Point To The NULL
33. INSERTION ALGORITHM
Insert: Apple
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
0 == 4? NO Won’t Execute
37. INSERTION ALGORITHM
Insert: Apple
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar p
int index = currentChar - 'a'; int index = p - 'a';
int index = 15;
38. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[15] != NULL)
{
currentNode = currentNode->children[15];
}
a
false
1
5
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
39. INSERTION ALGORITHM
Insert: Apple
if (currentNode->children[15] != NULL)
{
currentNode = currentNode->children[15];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
5
…
1
0
…
2
0
…
2
6
Is 15 Pointing To The NULL?
YES!
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
character as one of its descendants
Here 15 Is The Index Value ( p – ‘a’ )
NULL
43. INSERTION ALGORITHM
Insert: Apple
Node *newNode = new Node(currentChar);
So……….
currentChar p
p
Node(curre
ntChar)
p
newNode
All 26 Children Of newNode Will Point To The NULL
47. INSERTION ALGORITHM
Insert: Apple
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
1 == 4? NO Won’t Execute
54. INSERTION ALGORITHM
Insert: Apple
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
4 == 4? YES Will Execute
59. INSERTION ALGORITHM
Insert: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
62. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
63. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
66. INSERTION ALGORITHM
Insert: Army
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
0 == 3? NO Won’t Execute
68. INSERTION ALGORITHM
Insert: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar r
int index = currentChar - 'a'; int index = r - 'a';
int index = 17;
69. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
a
false
1
7
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
70. INSERTION ALGORITHM
Insert: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
7
…
1
0
…
2
0
…
2
6
Is 17 Pointing To The NULL?
YES!
So IF Statement Won’t Execute…………
And The Current Node Doesn't have the current
character as one of its descendants
Here 17 Is The Index Value ( r – ‘a’ )
NULL
74. INSERTION ALGORITHM
Insert: Army
Node *newNode = new Node(currentChar);
So……….
currentChar r
r
Node(curre
ntChar)
r
newNode
All 26 Children Of newNode Will Point To The NULL
79. INSERTION ALGORITHM
Insert: Army
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
1 == 3? NO Won’t Execute
83. INSERTION ALGORITHM
Insert: Army
if (i == word.size() - 1)
{
currentNode->end = true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
3 == 3? YES Will Execute
87. DELETION ALGORITHM
Delete: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
90. DELETION ALGORITHM
Delete: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
91. DELETION ALGORITHM
Delete: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
95. DELETION ALGORITHM
Delete: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar r
int index = currentChar - 'a'; int index = r - 'a';
int index = 17;
96. DELETION ALGORITHM
Delete: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
a
false
1
7
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
97. DELETION ALGORITHM
Delete: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
7
…
1
0
…
2
0
…
2
6
Is 17 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 17 Is The Index Value ( r – ‘a’ )
NULL
102. DELETION ALGORITHM
Delete: Army
if (i == word.size() – 1 && currentNode->end)
{
currentNode->end = false;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
3 == 3? && currentNode end? YES Will Execute
106. SEARCHING ALGORITHM
SEARCH: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar a
int index = currentChar - 'a'; int index = a - 'a';
int index = 0;
109. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
false
0 …
1
0
…
2
0
…
2
6
NULL
If 0 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
110. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[0] != NULL)
{
currentNode = currentNode->children[0];
}
Is 0 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 0 Is The Index Value ( a – ‘a’ )
Check If The Current Node Has The Current Character As One Of Its
Descendants
false
0 …
1
0
…
2
0
…
2
6
NULL
114. SEARCHING ALGORITHM
Search: Army
Then Get The Correct Index For The Appropriate Character
int index = currentChar - 'a';
So……….
currentChar r
int index = currentChar - 'a'; int index = r - 'a';
int index = 17;
115. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
a
false
1
7
…
1
0
…
2
0
…
2
6
NULL
If 15 Pointing To The NULL?
Check If The Current Node Has The Current Character As One Of Its
Descendants
116. SEARCHING ALGORITHM
Search: Army
if (currentNode->children[17] != NULL)
{
currentNode = currentNode->children[17];
}
Check If The Current Node Has The Current Character As One Of Its
Descendants
a
false
1
7
…
1
0
…
2
0
…
2
6
Is 17 Pointing To The NULL?
NO!
So IF Statement Will Execute…………
Here 17 Is The Index Value ( r – ‘a’ )
NULL
121. SEARCHING ALGORITHM
Search: Army
if (i == word.size() – 1 && currentNode->end)
{
return true;
}
Now Check If It Is The Last Character Of The Word Has Been Reached
3 == 3? && currentNode end? YES Will Execute
153. SOURCE CODE
Trie.c
void Trie::addWord(string word)
{
Node * currentNode = root;
for (int i = 0; i < (int)word.size(); ++i)
{
char currentChar = tolower(word.at(i));
int index = currentChar - 'a';
assert(index >= 0); // Makes sure the character is between a-z
if (currentNode->children[index] != NULL)
{
// check if the current node has the current character as one of its
decendants
currentNode = currentNode->children[index];
}
else
{
// the current node doesn't have the current character as one of its
decendants
Node * newNode = new Node(currentChar);
currentNode->children[index] = newNode;
currentNode = newNode;
}
if (i == (int)word.size() - 1)
{
// the last character of the word has been reached
currentNode->end = true;
}
}
}
160. TRIES IN AUTO COMPLETE
• Since a trie is a tree-like data structure in which each node
contains an array of pointers, one pointer for each character in the
alphabet.
• Starting at the root node, we can trace a word by following pointers
corresponding to the letters in the target word.
• Starting from the root node, you can check if a word exists in the
trie easily by following pointers corresponding to the letters in the
161. TRIES IN AUTO COMPLETE
• Auto-complete functionality is used widely over the internet and
mobile apps. A lot of websites and apps try to complete your input
as soon as you start typing.
• All the descendants of a node have a common prefix of the string
associated with that node.
164. WHY TRIES IN AUTO COMPLETE
• Implementing auto complete using a trie is easy.
• We simply trace pointers to get to a node that represents the string
the user entered. By exploring the trie from that node down, we
can enumerate all strings that complete user’s input.
165. AUTOMATIC COMMAND COMPLETION
• When using an operating system such as Unix or DOS, we type in
system commands to accomplish certain tasks. For example, the
Unix and DOS command cd may be used to change the current
directory.