Implementing a Trie (Prefix Tree) in JavaScript
Tries, also known as prefix trees, are a powerful data structure for efficient retrieval of strings based on prefixes. They are particularly useful for implementing features like autocomplete, spell checking, and IP routing. This challenge asks you to implement a Trie data structure in JavaScript, allowing for insertion, searching, and deletion of words.
Problem Description
You are tasked with creating a Trie class in JavaScript. The Trie should support the following operations:
insert(word): Inserts a word into the trie.search(word): Returnstrueif the word is present in the trie,falseotherwise.startsWith(prefix): Returnstrueif there is any word in the trie that starts with the given prefix,falseotherwise.delete(word): Deletes a word from the trie. If the word is not present, nothing should happen.
The Trie should be implemented using nested JavaScript objects to represent the tree structure. Each node in the Trie represents a character, and the path from the root to a node represents a prefix. A node should have a property isWord set to true if the path from the root to that node represents a complete word.
Key Requirements:
- The Trie should be case-sensitive.
- The
deleteoperation should correctly remove the word from the trie without affecting other words that share the same prefix. - The implementation should be efficient in terms of both time and space complexity.
Expected Behavior:
The insert, search, and startsWith methods should return the correct boolean values based on the presence or absence of words and prefixes in the trie. The delete method should remove the specified word if it exists, and leave the trie unchanged if the word is not present.
Edge Cases to Consider:
- Empty string insertion/search/startsWith/deletion.
- Inserting the same word multiple times.
- Deleting a word that doesn't exist.
- Deleting a word that is a prefix of another word.
- Trie with only one word.
- Trie with many words.
Examples
Example 1:
Input:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // returns true
trie.search("app"); // returns false
trie.startsWith("app"); // returns true
trie.insert("app");
trie.search("app"); // returns true
trie.delete("apple");
trie.search("apple"); // returns false
trie.search("app"); // returns true
Explanation: Demonstrates basic insertion, searching, and deletion.
Example 2:
Input:
Trie trie = new Trie();
trie.insert("banana");
trie.insert("band");
trie.startsWith("ban"); // returns true
trie.delete("band");
trie.startsWith("ban"); // returns true
trie.search("band"); // returns false
Explanation: Shows how deletion affects prefixes and demonstrates the handling of shared prefixes.
Example 3:
Input:
Trie trie = new Trie();
trie.insert("");
trie.search(""); // returns true
trie.startsWith(""); // returns true
trie.delete(""); // Deletes the empty string
trie.search(""); // returns false
trie.startsWith(""); // returns false
Explanation: Handles the edge case of inserting and deleting the empty string.
Constraints
- The length of each word will be between 0 and 2000 characters, inclusive.
- The number of words that can be inserted into the Trie will be up to 100,000.
- The time complexity of
insert,search,startsWith, anddeleteshould be O(k), where k is the length of the word/prefix. - The space complexity should be proportional to the total number of characters in all inserted words.
Notes
- Consider using a JavaScript object (or Map) to represent each node in the Trie. The keys of the object will represent the characters, and the values will be the child nodes.
- The
isWordproperty of each node indicates whether the path from the root to that node represents a complete word. - The
deleteoperation requires careful handling to avoid removing nodes that are part of other words. You may need to traverse the Trie and remove nodes that are no longer needed after deleting a word. - Think about how to handle the case where a word is a prefix of another word. For example, if you insert "apple" and then "app", deleting "app" should not affect the "apple" entry.