Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 51k
Add delete to trie.py + tests#1177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| """ | ||
| A Trie/Prefix Tree is a kind of search tree used to provide quick lookup | ||
| of words/patterns in a set of words. A basic Trie however has O(n^2) space complexity | ||
| making it impractical in practice. It however provides O(max(search_string, length of longest word)) lookup | ||
| time making it an optimal approach when space is not an issue. | ||
| making it impractical in practice. It however provides O(max(search_string, length of longest word)) | ||
| lookup time making it an optimal approach when space is not an issue. | ||
| """ | ||
| @@ -12,7 +11,7 @@ def __init__(self): | ||
| self.nodes = dict() # Mapping from char to TrieNode | ||
| self.is_leaf = False | ||
| def insert_many(self, words: [str]): # noqa: E999 This syntax is Python 3 only | ||
| def insert_many(self, words: [str]): | ||
| """ | ||
| Inserts a list of words into the Trie | ||
| :param words: list of string words | ||
| @@ -21,7 +20,7 @@ def insert_many(self, words: [str]): # noqa: E999 This syntax is Python 3 only | ||
| for word in words: | ||
| self.insert(word) | ||
| def insert(self, word: str): # noqa: E999 This syntax is Python 3 only | ||
| def insert(self, word: str): | ||
| """ | ||
| Inserts a word into the Trie | ||
| :param word: word to be inserted | ||
| @@ -34,7 +33,7 @@ def insert(self, word: str): # noqa: E999 This syntax is Python 3 only | ||
| curr = curr.nodes[char] | ||
| curr.is_leaf = True | ||
| def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only | ||
| def find(self, word: str) -> bool: | ||
| """ | ||
| Tries to find word in a Trie | ||
| :param word: word to look for | ||
| @@ -47,29 +46,82 @@ def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only | ||
| curr = curr.nodes[char] | ||
| return curr.is_leaf | ||
| def delete(self, word: str): | ||
| """ | ||
| Deletes a word in a Trie | ||
| :param word: word to delete | ||
| :return: None | ||
| """ | ||
| def print_words(node: TrieNode, word: str): # noqa: E999 This syntax is Python 3 only | ||
| def _delete(curr: TrieNode, word: str, index: int): | ||
| if index == len(word): | ||
| # If word does not exist | ||
| if not curr.is_leaf: | ||
| return False | ||
| curr.is_leaf = False | ||
| return len(curr.nodes) == 0 | ||
| char = word[index] | ||
| char_node = curr.nodes.get(char) | ||
| # If char not in current trie node | ||
| if not char_node: | ||
| return False | ||
| # Flag to check if node can be deleted | ||
| delete_curr = _delete(char_node, word, index + 1) | ||
| if delete_curr: | ||
| del curr.nodes[char] | ||
| return len(curr.nodes) == 0 | ||
| return delete_curr | ||
| _delete(self, word, 0) | ||
| def print_words(node: TrieNode, word: str): | ||
| """ | ||
| Prints all the words in a Trie | ||
| :param node: root node of Trie | ||
| :param word: Word variable should be empty at start | ||
| :return: None | ||
| """ | ||
| if node.is_leaf: | ||
| print(word, end=' ') | ||
| print(word, end=" ") | ||
| for key, value in node.nodes.items(): | ||
| print_words(value, word + key) | ||
| def test(): | ||
| words = ['banana', 'bananas', 'bandana', 'band', 'apple', 'all', 'beast'] | ||
| def test_trie(): | ||
| words = "bananabananasbandanabandappleallbeast".split() | ||
| root = TrieNode() | ||
| root.insert_many(words) | ||
| # print_words(root, '') | ||
| assert root.find('banana') | ||
| assert not root.find('bandanas') | ||
| assert not root.find('apps') | ||
| assert root.find('apple') | ||
| # print_words(root, "") | ||
| assert all(root.find(word) for word in words) | ||
| assert root.find("banana") | ||
| assert not root.find("bandanas") | ||
| assert not root.find("apps") | ||
| assert root.find("apple") | ||
| assert root.find("all") | ||
| root.delete("all") | ||
cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| assert not root.find("all") | ||
| root.delete("banana") | ||
| assert not root.find("banana") | ||
cclauss marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| assert root.find("bananas") | ||
| return True | ||
| def print_results(msg: str, passes: bool) -> None: | ||
| print(str(msg), "works!" if passes else "doesn't work :(") | ||
| def pytests(): | ||
| assert test_trie() | ||
| def main(): | ||
| """ | ||
| >>> pytests() | ||
| """ | ||
| print_results("Testing trie functionality", test_trie()) | ||
| test() | ||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we test that len(the whole tree) == len(words)?Could we add a test: assert all(root.find(word) for word in words)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on how to go about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's forget the first request because this implementation does not have a
__len__()method or avisit()method that would facilitate the creation of that test.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert all(root.find(word) for word in words) will ensure that we can find() each word that we just added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.python.org/3/library/functions.html#all