|
| 1 | +<h1> Trie</h1> |
| 2 | + |
| 3 | +<p align="justify"> |
| 4 | +This is a C++ implementation of the Trie data structure. The header file <code>trie.hpp</code> contains the class for trie and <code>trie_node.hpp</code> contains the class for the node (using which Trie is built). <br> |
| 5 | +In this implementation, a single trie object can store multiple copies of the same string efficiently. A string (which is to be stored in the Trie) is not limited to containing alphabetical characters only. It should be non-empty but can contain any valid ASCII character. Two strings are considered identical if they are of the same length and at each valid index, the ASCII value of the characters in both strings are equal. |
| 6 | +</p> |
| 7 | + |
| 8 | +<h2>Public Methods</h2> |
| 9 | +<ul> |
| 10 | + <li> |
| 11 | + <h5><code>void insert (string &s)</code></h5> |
| 12 | + <p align="justify"> |
| 13 | + Inserts string "s" into the Trie |
| 14 | + <br> |
| 15 | + If "s" is already present in the Trie, its occurrence count is increased by one. |
| 16 | + <br> |
| 17 | + Time Complexity: Constant, if "s" is already present in the Trie, otherwise Linear in the length of "s" |
| 18 | + </p> |
| 19 | + </li> |
| 20 | + <li> |
| 21 | + <h5><code>void remove (string &s)</code></h5> |
| 22 | + <p align="justify"> |
| 23 | + Removes string "s" from the Trie |
| 24 | + <br> |
| 25 | + Returns immediately if Trie does not contain "s". |
| 26 | + <br> |
| 27 | + If Trie contains multiple occurrences of "s", this method removes one of them |
| 28 | + <br> |
| 29 | + Time Complexity: Constant, if Trie contains more than one occurrence of "s", otherwise Linear in the length of "s" |
| 30 | + </p> |
| 31 | + </li> |
| 32 | + <li> |
| 33 | + <h5><code>void removeAll (string &s)</code></h5> |
| 34 | + <p align="justify"> |
| 35 | + Removes all occurrences of string "s" from the Trie |
| 36 | + <br> |
| 37 | + Returns immediately if Trie does not contain "s". |
| 38 | + <br> |
| 39 | + Time Complexity: Linear in the length of "s" |
| 40 | + </p> |
| 41 | + </li> |
| 42 | + <li> |
| 43 | + <h5><code>bool contains (string &s)</code></h5> |
| 44 | + <p align="justify"> |
| 45 | + Returns true if the Trie contains "s" (length of "s" > 0), false otherwise |
| 46 | + <br> |
| 47 | + Time Complexity: Constant |
| 48 | + </p> |
| 49 | + </li> |
| 50 | + <li> |
| 51 | + <h5><code>void clear</code></h5> |
| 52 | + <p align="justify"> |
| 53 | + Clears the Trie and removes all the strings stored in it. Also frees the memory occupied by the the Trie nodes |
| 54 | + <br> |
| 55 | + Time Complexity: Linear in the number of nodes in the Trie |
| 56 | + </p> |
| 57 | + </li> |
| 58 | + <li> |
| 59 | + <h5><code>int count (string &s)</code></h5> |
| 60 | + <p align="justify"> |
| 61 | + Returns the number of occurrences of string "s" in the Trie |
| 62 | + <br> |
| 63 | + Time Complexity: Constant |
| 64 | + </p> |
| 65 | + </li> |
| 66 | + <li> |
| 67 | + <h5><code>int totalWords ()</code></h5> |
| 68 | + <p align="justify"> |
| 69 | + Returns the total number of strings stored in the Trie. Duplicate copies of the same string are counted separately |
| 70 | + <br> |
| 71 | + Time Complexity: Constant |
| 72 | + </p> |
| 73 | + </li> |
| 74 | + <li> |
| 75 | + <h5><code>int uniqueWords ()</code></h5> |
| 76 | + <p align="justify"> |
| 77 | + Returns the number of unique strings stored in the Trie |
| 78 | + <br> |
| 79 | + Time Complexity: Constant |
| 80 | + </p> |
| 81 | + </li> |
| 82 | +</ul> |
0 commit comments