mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-03-15 21:59:56 +00:00
Update Trie.md
This commit is contained in:
parent
4b045d4325
commit
99b27b0733
@ -71,7 +71,7 @@ And therefore representation of trie containing string "act" will be as below.
|
||||
|
||||
Now, observe the trie below, which contains two strings "act" and "ace".
|
||||
|
||||

|
||||
.jpg)
|
||||
|
||||
Note that the node representing character `c` in the above trie, in magnified sense would look as below:
|
||||
|
||||
@ -85,7 +85,7 @@ Therefore, we are not creating any new node until we need one and **Trie is a ve
|
||||
|
||||
Now, observe the trie below, which contains three strings `"act"`, `"ace"` and `"cat"`.
|
||||
|
||||

|
||||
.jpg)
|
||||
|
||||
Let's see proper algorithm to insert a string in a trie.
|
||||
|
||||
@ -116,13 +116,13 @@ Can you figure out, how can we check whether the given string is present in trie
|
||||
|
||||
For example, if you are searching `"aco"` in the trie below, then how will you do?
|
||||
|
||||

|
||||
.jpg)
|
||||
|
||||
**Can you see, why `isEndofString` is needed?**
|
||||
|
||||
Observe the trie given below and try to search whether `"on"` is present or not.
|
||||
|
||||

|
||||
.jpg)
|
||||
|
||||
If you don't have `isEndofString` variable, then you will not be able to correctly check whether `on` is present or not. Because it is prefix of `once`.
|
||||
|
||||
@ -151,7 +151,7 @@ bool search(trie_node* root, string s)
|
||||
|
||||
How will you delete `"ace"` from the trie below?
|
||||
|
||||

|
||||
.jpg)
|
||||
|
||||
Things to take care about while you are deleting a string from the trie,
|
||||
1. It should not affect any other string present in the trie.
|
||||
@ -280,7 +280,7 @@ It is easy implemention, but with single downside. Therefore, use as per the req
|
||||
|
||||
How will you find the number of words(strings) present in the trie below?
|
||||
|
||||

|
||||
.jpg)
|
||||
|
||||
Ultimately, It means to find the total number of nodes having `true` value of `isEndofString`. Which can be easily done using recursive traversal of all the nodes present in the trie.
|
||||
|
||||
@ -340,7 +340,7 @@ How will you design autocompletion feature using Trie?
|
||||
|
||||
For example, we have stored C++ keywords in a trie. Now, when you type `"n"` it should show all keywords starting from `"n"`. For simplicity only keywords starting from `"n"` are shown in the trie below,
|
||||
|
||||

|
||||
.jpg)
|
||||
|
||||
How will you print all keywords starting from `"n"`? OR how will you print all keywords having `"n"` as prefix?
|
||||
|
||||
@ -426,4 +426,4 @@ struct trie_node
|
||||
Below image shows a typical trie structure for dictionary.
|
||||
|
||||
|
||||

|
||||
.jpg)
|
||||
|
Loading…
x
Reference in New Issue
Block a user