Insertion in a Trie
Explore the method of inserting words into a Trie data structure, including handling cases with no common prefix, shared prefixes, and existing words. Understand the process of setting end-of-word markers and the time complexity of the insertion operation, to build efficient string storage and retrieval systems in Java.
We'll cover the following...
Inserting a word in Trie
Insertion is simple for individual characters in the trie. If the character is already present, we follow the path; if that character is not present, then we insert the corresponding nodes. While inserting the last node, we must also set the value of isEndWord() to true.
Case 1: What if the word has no common subsequence? i.e. No Common Prefix
For example, if we want to insert a new word into the trie below, e.g. “any”, then we need to insert all of the characters for the word “any”; currently, “the” is the only word in the trie, and there are no common character subsequences between “any” and “the”.
Case 2: If the word has a common subsequence? i.e. Common Prefix Found
For example, if we want to insert the word “there” into the trie below-- which already consists of the word “ their”–then the path to “ the” already exists. After that, we need to insert two nodes for “e” and “r” as shown below.
Case 3: If the word is already present? i.e. Common Prefix Found
For example, if we want to insert the word “the” into the trie below-- which consists of a word “ their”–then the path for “ the” already exists. Therefore, we simply need to set the value of isEndWord() to true at “e” in order to represent the end of the word for “the” as shown below.
Implementation
The following is the code snippet, so try to understand the code. If you don’t understand any point, you can read the explanation below.
If this Code had a Face
Explanation
The Insert() function (line # 14) takes an argument of a string key, indicating a word which will be stored for that particular word/key.
Null-keys are not allowed, and keys are stored in lowercase.
We simply iterate the key character by character, and we generate an index for each character using getIndex() (line # 9). Afterward, we check the child of currentNode at that particular index, and if it’s null then we create a new TrieNode at that index indicating the specific character of the given key.
In the end, we mark the currentNode as leaf; i.e we’re setting the last character as the end of the word we just stored as “key”.
Time Complexity
If the length of the word is , the worst-case time complexity is . In the worst case, at every level, i.e., for every character in the word, a TrieNode is created and inserted into the Trie. The running time of the TrieNode constructor is . Since the alphabet size is fixed, this is . Thus, the running time of inserting in the trie is .
In this lesson, we have learned how to insert a word in Trie.