From 6b60aeeac88bee9fd4fad9d28199fc1b77fa9146 Mon Sep 17 00:00:00 2001 From: Tr-Jono Date: Sat, 5 Oct 2019 17:00:33 +0800 Subject: [PATCH 1/3] Add trie --- Data_Structure/src/Trees/trie.py | 104 +++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Data_Structure/src/Trees/trie.py diff --git a/Data_Structure/src/Trees/trie.py b/Data_Structure/src/Trees/trie.py new file mode 100644 index 00000000..81e3351a --- /dev/null +++ b/Data_Structure/src/Trees/trie.py @@ -0,0 +1,104 @@ +"""Trie implementation in Python""" + + +class _TrieNode: + def __init__(self): + self.words = 0 + self.children = {} + + def __len__(self): + return self.words + sum(len(c) for c in self.children.values()) + + def add(self, s): + if not s: + self.words += 1 + return + if s[0] not in self.children: + self.children[s[0]] = _TrieNode() + self.children[s[0]].add(s[1:]) + + def remove(self, s): + if not s: + if not self.words: + raise ValueError + self.words -= 1 + return + if s[0] not in self.children: + raise ValueError + self.children[s[0]].remove(s[1:]) + + def count(self, s): + if not s: + return self.words + if s[0] not in self.children: + return 0 + return self.children[s[0]].count(s[1:]) + + def get_words(self): + words = [""] * self.words + for c in self.children: + for w in self.children[c].get_words(): + words.append(c + w) + return words + + +class Trie: + def __init__(self, *words): + self.children = {} + for w in words: + self.add(w) + + def __contains__(self, item): + return bool(self.count(item)) + + def __len__(self): + return sum(len(c) for c in self.children.values()) + + def __bool__(self): + return bool(len(self)) + + def __repr__(self): + return "{}({})".format(self.__class__.__name__, ", ".join([repr(w) for w in self.get_words()])) + + def add(self, s): + if not s: + raise ValueError("Empty strings disallowed") + if s[0] not in self.children: + self.children[s[0]] = _TrieNode() + self.children[s[0]].add(s[1:]) + + def remove(self, s): + if not s: + raise ValueError("Empty strings disallowed") + try: + if s[0] not in self.children: + raise ValueError + self.children[s[0]].remove(s[1:]) + except ValueError: + raise ValueError("{} not in trie".format(repr(s))) from None + + def count(self, s): + if s[0] not in self.children: + return 0 + return self.children[s[0]].count(s[1:]) + + def get_words(self): + words = [] + for c in self.children: + for w in self.children[c].get_words(): + words.append(c + w) + return words + + +def main(): + trie = Trie("dog", "dog", "cat", "tiger") + trie.add("bird") + trie.remove("bird") + print("repr(trie):", trie) + print("len(trie):", len(trie)) + print("trie.count('dog'):", trie.count("dog")) + print("'bird' in trie:", "bird" in trie) + + +if __name__ == "__main__": + main() From 65f07c539b9676aef398d928d8a2d1cdce64e62f Mon Sep 17 00:00:00 2001 From: Tr-Jono Date: Sat, 5 Oct 2019 17:20:27 +0800 Subject: [PATCH 2/3] create desc for trie --- Data_Structure/src/Trees/trie.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Data_Structure/src/Trees/trie.py b/Data_Structure/src/Trees/trie.py index 81e3351a..1bdde314 100644 --- a/Data_Structure/src/Trees/trie.py +++ b/Data_Structure/src/Trees/trie.py @@ -1,4 +1,18 @@ -"""Trie implementation in Python""" +""" +Trie implementation in Python by @Tr-Jono + +A trie is a type of tree that is typically used for the storage of strings. +The strings "ab", "ac", "ace", "ba", "boom" in a trie would look like + [root] + / \ + "a" "b" + / \ / \ + "b" "c" "a" "o" + | | + "e" "o" + | + "m" +""" class _TrieNode: From 4a934ac29de93cc4f185a9441384c6dac970402b Mon Sep 17 00:00:00 2001 From: Tr-Jono Date: Sat, 5 Oct 2019 21:14:57 +0800 Subject: [PATCH 3/3] add more desc to trie --- Data_Structure/src/Trees/trie.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Data_Structure/src/Trees/trie.py b/Data_Structure/src/Trees/trie.py index 1bdde314..5f60a2f9 100644 --- a/Data_Structure/src/Trees/trie.py +++ b/Data_Structure/src/Trees/trie.py @@ -1,8 +1,9 @@ """ Trie implementation in Python by @Tr-Jono -A trie is a type of tree that is typically used for the storage of strings. -The strings "ab", "ac", "ace", "ba", "boom" in a trie would look like +A trie is a type of search tree that is used for storing strings. +Visual representation of a trie with strings "ab", "ac", "ace", "ba", "boom": + [root] / \ "a" "b" @@ -12,6 +13,16 @@ "e" "o" | "m" + +The time complexities of addition, removal and querying existence/count of a string are all O(|target string|). + +A trie could be used in place of a string list used for searching. +Time complexity of searching a string in: + - a list: O(|list|) + - a trie: O(|target string|) + +This implementation of the trie relies on recursively stored _TrieNode instances. +The Trie class, acting as the root, implements typical data structure methods. """