|
| 1 | +# Trie |
| 2 | + |
| 3 | +A Trie is a tree-like data structure used for storing a dynamic set of strings where the keys are usually strings. It is also known as prefix tree or digital tree. |
| 4 | + |
| 5 | +>Trie is a type of search tree, where each node represents a single character of a string. |
| 6 | +
|
| 7 | +>Nodes are linked in such a way that they form a tree, where each path from the root to a leaf node represents a unique string stored in the Trie. |
| 8 | +
|
| 9 | +## Characteristics of Trie |
| 10 | +- **Prefix Matching**: Tries are particularly useful for prefix matching operations. Any node in the Trie represents a common prefix of all strings below it. |
| 11 | +- **Space Efficiency**: Tries can be more space-efficient than other data structures like hash tables for storing large sets of strings with common prefixes. |
| 12 | +- **Time Complexity**: Insertion, deletion, and search operations in a Trie have a time complexity of |
| 13 | +𝑂(𝑚), where m is the length of the string. This makes Tries very efficient for these operations. |
| 14 | + |
| 15 | +## Structure of Trie |
| 16 | + |
| 17 | +Trie mainly consists of three parts: |
| 18 | +- **Root**: The root of a Trie is an empty node that does not contain any character. |
| 19 | +- **Edges**: Each edge in the Trie represents a character in the alphabet of the stored strings. |
| 20 | +- **Nodes**: Each node contains a character and possibly additional information, such as a boolean flag indicating if the node represents the end of a valid string. |
| 21 | + |
| 22 | +To implement the nodes of trie. We use Classes in Python. Each node is an object of the Node Class. |
| 23 | + |
| 24 | +Node Class have mainly two components |
| 25 | +- *Array of size 26*: It is used to represent the 26 alphabets. Initially all are None. While inserting the words, then array will be filled with object of child nodes. |
| 26 | +- *End of word*: It is used to represent the end of word while inserting. |
| 27 | + |
| 28 | +Code Block of Node Class : |
| 29 | + |
| 30 | +```python |
| 31 | +class Node: |
| 32 | + def __init__(self): |
| 33 | + self.alphabets = [None] * 26 |
| 34 | + self.end_of_word = 0 |
| 35 | +``` |
| 36 | + |
| 37 | +Now we need to implement Trie. We create another class named Trie with some methods like Insertion, Searching and Deletion. |
| 38 | + |
| 39 | +**Initialization:** In this, we initializes the Trie with a `root` node. |
| 40 | + |
| 41 | +Code Implementation of Initialization: |
| 42 | + |
| 43 | +```python |
| 44 | +class Trie: |
| 45 | + def __init__(self): |
| 46 | + self.root = Node() |
| 47 | +``` |
| 48 | + |
| 49 | +## Operations on Trie |
| 50 | + |
| 51 | +1. **Insertion**: Inserts the word into the Trie. This method takes `word` as parameter. For each character in the word, it checks if there is a corresponding child node. If not, it creates a new `Node`. After processing all the characters in word, it increments the `end_of_word` value of the last node. |
| 52 | + |
| 53 | +Code Implementation of Insertion: |
| 54 | +```python |
| 55 | +def insert(self, word): |
| 56 | + node = self.root |
| 57 | + for char in word: |
| 58 | + index = ord(char) - ord('a') |
| 59 | + if not node.alphabets[index]: |
| 60 | + node.alphabets[index] = Node() |
| 61 | + node = node.alphabets[index] |
| 62 | + node.end_of_word += 1 |
| 63 | +``` |
| 64 | + |
| 65 | +2. **Searching**: Search the `word` in trie. Searching process starts from the `root` node. Each character of the `word` is processed. After traversing the whole word in trie, it return the count of words. |
| 66 | + |
| 67 | +There are two cases in Searching: |
| 68 | +- *Word Not found*: It happens when the word we search not present in the trie. This case will occur, if the value of `alphabets` array at that character is `None` or if the value of `end_of_word` of the node, reached after traversing the whole word is `0`. |
| 69 | +- *Word found*: It happens when the search word is present in the Trie. This case will occur, when the `end_of_word` value is greater than `0` of the node after traversing the whole word. |
| 70 | + |
| 71 | +Code Implementation of Searching: |
| 72 | +```python |
| 73 | + def Search(self, word): |
| 74 | + node = self.root |
| 75 | + for char in word: |
| 76 | + index = ord(char) - ord('a') |
| 77 | + if not node.alphabets[index]: |
| 78 | + return 0 |
| 79 | + node = node.alphabets[index] |
| 80 | + return node.end_of_word |
| 81 | +``` |
| 82 | + |
| 83 | +3. **Deletion**: To delete a string, follow the path of the string. If the end node is reached and `end_of_word` is greater than `0` then decrement the value. |
| 84 | + |
| 85 | +Code Implementation of Deletion: |
| 86 | + |
| 87 | +```python |
| 88 | +def delete(self, word): |
| 89 | + node = self.root |
| 90 | + for char in word: |
| 91 | + index = ord(char) - ord('a') |
| 92 | + node = node.alphabets[index] |
| 93 | + if node.end_of_word: |
| 94 | + node.end_of_word-=1 |
| 95 | +``` |
| 96 | + |
| 97 | +Python Code to implement Trie: |
| 98 | + |
| 99 | +```python |
| 100 | +class Node: |
| 101 | + def __init__(self): |
| 102 | + self.alphabets = [None] * 26 |
| 103 | + self.end_of_word = 0 |
| 104 | + |
| 105 | +class Trie: |
| 106 | + def __init__(self): |
| 107 | + self.root = Node() |
| 108 | + |
| 109 | + def insert(self, word): |
| 110 | + node = self.root |
| 111 | + for char in word: |
| 112 | + index = ord(char) - ord('a') |
| 113 | + if not node.alphabets[index]: |
| 114 | + node.alphabets[index] = Node() |
| 115 | + node = node.alphabets[index] |
| 116 | + node.end_of_word += 1 |
| 117 | + |
| 118 | + def Search(self, word): |
| 119 | + node = self.root |
| 120 | + for char in word: |
| 121 | + index = ord(char) - ord('a') |
| 122 | + if not node.alphabets[index]: |
| 123 | + return 0 |
| 124 | + node = node.alphabets[index] |
| 125 | + return node.end_of_word |
| 126 | + |
| 127 | + def delete(self, word): |
| 128 | + node = self.root |
| 129 | + for char in word: |
| 130 | + index = ord(char) - ord('a') |
| 131 | + node = node.alphabets[index] |
| 132 | + if node.end_of_word: |
| 133 | + node.end_of_word-=1 |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + trie = Trie() |
| 137 | + |
| 138 | + word1 = "apple" |
| 139 | + word2 = "app" |
| 140 | + word3 = "bat" |
| 141 | + |
| 142 | + trie.insert(word1) |
| 143 | + trie.insert(word2) |
| 144 | + trie.insert(word3) |
| 145 | + |
| 146 | + print(trie.Search(word1)) |
| 147 | + print(trie.Search(word2)) |
| 148 | + print(trie.Search(word3)) |
| 149 | + |
| 150 | + trie.delete(word2) |
| 151 | + print(trie.Search(word2)) |
| 152 | +``` |
0 commit comments