|
| 1 | +from pydatastructs.utils.misc_util import TrieNode |
| 2 | +from collections import deque |
| 3 | +import copy |
| 4 | + |
| 5 | +__all__ = [ |
| 6 | + 'Trie' |
| 7 | +] |
| 8 | + |
| 9 | +Stack = Queue = deque |
| 10 | + |
| 11 | +class Trie(object): |
| 12 | + """ |
| 13 | + Represents the trie data structure for storing strings. |
| 14 | +
|
| 15 | + Examples |
| 16 | + ======== |
| 17 | +
|
| 18 | + >>> from pydatastructs import Trie |
| 19 | + >>> trie = Trie() |
| 20 | + >>> trie.insert("a") |
| 21 | + >>> trie.insert("aa") |
| 22 | + >>> trie.strings_with_prefix("a") |
| 23 | + ['a', 'aa'] |
| 24 | + >>> trie.is_present("aa") |
| 25 | + True |
| 26 | + >>> trie.delete("aa") |
| 27 | + True |
| 28 | + >>> trie.is_present("aa") |
| 29 | + False |
| 30 | +
|
| 31 | + References |
| 32 | + ========== |
| 33 | +
|
| 34 | + .. [1] https://en.wikipedia.org/wiki/Trie |
| 35 | + """ |
| 36 | + |
| 37 | + __slots__ = ['root'] |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def methods(cls): |
| 41 | + return ['__new__', 'insert', 'is_present', 'delete', |
| 42 | + 'strings_with_prefix'] |
| 43 | + |
| 44 | + def __new__(cls): |
| 45 | + obj = object.__new__(cls) |
| 46 | + obj.root = TrieNode() |
| 47 | + return obj |
| 48 | + |
| 49 | + def insert(self, string: str) -> None: |
| 50 | + """ |
| 51 | + Inserts the given string into the trie. |
| 52 | +
|
| 53 | + Parameters |
| 54 | + ========== |
| 55 | +
|
| 56 | + string: str |
| 57 | +
|
| 58 | + Returns |
| 59 | + ======= |
| 60 | +
|
| 61 | + None |
| 62 | + """ |
| 63 | + walk = self.root |
| 64 | + for char in string: |
| 65 | + if walk.get_child(char) is None: |
| 66 | + newNode = TrieNode(char) |
| 67 | + walk.add_child(newNode) |
| 68 | + walk = newNode |
| 69 | + else: |
| 70 | + walk = walk.get_child(char) |
| 71 | + walk.is_terminal = True |
| 72 | + |
| 73 | + def is_present(self, string: str) -> bool: |
| 74 | + """ |
| 75 | + Checks if the given string is present as a prefix in the trie. |
| 76 | +
|
| 77 | + Parameters |
| 78 | + ========== |
| 79 | +
|
| 80 | + string: str |
| 81 | +
|
| 82 | + Returns |
| 83 | + ======= |
| 84 | +
|
| 85 | + True if the given string is present as a prefix; |
| 86 | + False in all other cases. |
| 87 | + """ |
| 88 | + walk = self.root |
| 89 | + for char in string: |
| 90 | + if walk.get_child(char) is None: |
| 91 | + return False |
| 92 | + walk = walk.get_child(char) |
| 93 | + return True |
| 94 | + |
| 95 | + def delete(self, string: str) -> bool: |
| 96 | + """ |
| 97 | + Deletes the given string from the trie. |
| 98 | +
|
| 99 | + Parameters |
| 100 | + ========== |
| 101 | +
|
| 102 | + string: str |
| 103 | +
|
| 104 | + Returns |
| 105 | + ======= |
| 106 | +
|
| 107 | + True if successfully deleted; |
| 108 | + None if the string is not present in the trie. |
| 109 | + """ |
| 110 | + path = [] |
| 111 | + walk = self.root |
| 112 | + size = len(string) |
| 113 | + for i in range(size): |
| 114 | + char = string[i] |
| 115 | + path.append(walk) |
| 116 | + if walk.get_child(char) is None: |
| 117 | + return None |
| 118 | + walk = walk.get_child(char) |
| 119 | + path.append(walk) |
| 120 | + i = len(path) - 1 |
| 121 | + path[i].is_terminal = False |
| 122 | + while not path[i]._children and i >= 1: |
| 123 | + path[i-1].remove_child(path[i].char) |
| 124 | + i -= 1 |
| 125 | + if path[i].is_terminal: |
| 126 | + return True |
| 127 | + return True |
| 128 | + |
| 129 | + def strings_with_prefix(self, string: str) -> list: |
| 130 | + """ |
| 131 | + Generates a list of all strings with the given prefix. |
| 132 | +
|
| 133 | + Parameters |
| 134 | + ========== |
| 135 | +
|
| 136 | + string: str |
| 137 | +
|
| 138 | + Returns |
| 139 | + ======= |
| 140 | +
|
| 141 | + strings: list |
| 142 | + The list of strings with the given prefix. |
| 143 | + """ |
| 144 | + |
| 145 | + def _collect(prefix: str, node: TrieNode, strings: list) -> str: |
| 146 | + TrieNode_stack = Stack() |
| 147 | + TrieNode_stack.append((node, prefix)) |
| 148 | + while TrieNode_stack: |
| 149 | + walk, curr_prefix = TrieNode_stack.pop() |
| 150 | + if walk.is_terminal: |
| 151 | + strings.append(curr_prefix + walk.char) |
| 152 | + for child in walk._children: |
| 153 | + TrieNode_stack.append((walk.get_child(child), curr_prefix + walk.char)) |
| 154 | + |
| 155 | + strings = [] |
| 156 | + prefix = "" |
| 157 | + walk = self.root |
| 158 | + for char in string: |
| 159 | + walk = walk.get_child(char) |
| 160 | + if walk is None: |
| 161 | + return strings |
| 162 | + prefix += char |
| 163 | + if walk.is_terminal: |
| 164 | + strings.append(walk.char) |
| 165 | + for child in walk._children: |
| 166 | + _collect(prefix, walk.get_child(child), strings) |
| 167 | + return strings |
0 commit comments