Insertion in a Trie
This lesson defines all the cases needed for inserting a word in Trie, along with showing how to implement this functionality 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”.
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy