Last active
March 29, 2018 02:02
-
-
Save Tetsuya3850/c7078f9ec3e77e5dc1a26ddfb97e8df4 to your computer and use it in GitHub Desktop.
Trie with Python
This file contains 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
class TrieNode: | |
def __init__(self): | |
self.children = [None] * 26 | |
self.end = False | |
self.prefix = 0 | |
def has_no_children(self): | |
for child in self.children: | |
if child: | |
return False | |
return True | |
class Trie: | |
def __init__(self): | |
self.root = TrieNode() | |
def char_to_index(self, ch): | |
return ord(ch) - ord('a') | |
def insert(self, key): | |
if len(key) == 0: | |
return | |
curr = self.root | |
curr.prefix += 1 | |
for level in range(len(key)): | |
index = self.char_to_index(key[level]) | |
if not curr.children[index]: | |
curr.children[index] = TrieNode() | |
curr = curr.children[index] | |
curr.prefix += 1 | |
curr.end = True | |
def search(self, key): | |
curr = self.root | |
for level in range(len(key)): | |
index = self.char_to_index(key[level]) | |
if not curr.children[index]: | |
return False | |
curr = curr.children[index] | |
return curr != None and curr.end | |
def shallow_search(self, key): | |
curr = self.root | |
for level in range(len(key)): | |
index = self.char_to_index(key[level]) | |
if not curr.children[index]: | |
return 0 | |
curr = curr.children[index] | |
return curr.prefix | |
def delete(self, key): | |
def delete_helper(curr_node, key, level, length): | |
if curr_node: | |
curr_node.prefix -= 1 | |
if level == length: | |
if curr_node.end: | |
curr_node.end = False | |
return curr_node.has_no_children() | |
else: | |
index = self.char_to_index(key[level]) | |
if delete_helper(curr_node.children[index], key, level+1, length): | |
curr_node.children[index] = None | |
return not curr_node.end and curr_node.has_no_children() | |
return False | |
if len(key) == 0: | |
return | |
delete_helper(self.root, key, 0, len(key)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment