nathanwailes

Tries

Jun 9th, 2024
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. """
  2. A trie (pronounced "try") is a tree-like data structure that is used to efficiently store and search a dynamic set of strings. Tries are particularly useful for tasks involving prefix matching and fast retrieval of strings.
  3. """
  4.  
  5. class TrieNode:
  6.     def __init__(self):
  7.         self.children = {}
  8.         self.is_end_of_word = False
  9.  
  10. class Trie:
  11.     def __init__(self):
  12.         self.root = TrieNode()
  13.  
  14.     # Insert a word into the trie
  15.     def insert(self, word):
  16.         node = self.root
  17.         for char in word:
  18.             if char not in node.children:
  19.                 node.children[char] = TrieNode()
  20.             node = node.children[char]
  21.         node.is_end_of_word = True
  22.  
  23.     # Search for a word in the trie
  24.     def search(self, word):
  25.         node = self.root
  26.         for char in word:
  27.             if char not in node.children:
  28.                 return False
  29.             node = node.children[char]
  30.         return node.is_end_of_word
  31.  
  32.     # Check if there is any word in the trie that starts with the given prefix
  33.     def starts_with(self, prefix):
  34.         node = self.root
  35.         for char in prefix:
  36.             if char not in node.children:
  37.                 return False
  38.             node = node.children[char]
  39.         return True
  40.  
  41.     # Delete a word from the trie
  42.     def delete(self, word):
  43.         def _delete(node, word, depth):
  44.             if not node:
  45.                 return False
  46.  
  47.             if depth == len(word):
  48.                 if not node.is_end_of_word:
  49.                     return False
  50.                 node.is_end_of_word = False
  51.                 return len(node.children) == 0
  52.  
  53.             char = word[depth]
  54.             if char not in node.children:
  55.                 return False
  56.  
  57.             should_delete_current_node = _delete(node.children[char], word, depth + 1)
  58.  
  59.             if should_delete_current_node:
  60.                 del node.children[char]
  61.                 return len(node.children) == 0
  62.  
  63.             return False
  64.  
  65.         _delete(self.root, word, 0)
  66.  
  67.     # Get all words stored in the trie
  68.     def get_all_words(self):
  69.         def _get_all_words(node, prefix, words):
  70.             if node.is_end_of_word:
  71.                 words.append(prefix)
  72.  
  73.             for char, next_node in node.children.items():
  74.                 _get_all_words(next_node, prefix + char, words)
  75.  
  76.         words = []
  77.         _get_all_words(self.root, "", words)
  78.         return words
  79.  
  80. # Example usage
  81. trie = Trie()
  82. trie.insert("apple")
  83. trie.insert("app")
  84. trie.insert("apricot")
  85. trie.insert("banana")
  86.  
Advertisement
Add Comment
Please, Sign In to add comment