The following article provides an outline for Trie Data Structure in Java. Basically, data structure plays a very important part in computer programming and also, we must know when and why we use different types of data structure in computer programming. Normally trie is a discrete data structure, and this is not familiar, or we can say that this is not a widely used data structure but this used in the typical algorithm, a trie is also known as a digital tree; it also has another name that is radix or prefix.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Using a trie data structure, we search the element by prefixes in a well-structured tree with a key, and it is advantageous to store the strings. Moreover, we can perform the different operation trie data structure such as insertion, deletion and searching.
Syntax of Trie Data Structure in Java
Given below is the syntax mentioned:
public void insert_node(specified string of word){ TrieNode present = rootNode; For (char i: word.toCharArray()){ Present = present.getChildren().computeIfAbsent(I, c->new TrieNode()); } Present.setEndOfWord(true) }
Explanation:
By using the above syntax, we try to insert elements into the trie data structure; for that, we need to follow the following steps as follows:
Similarly, we can write syntax for delete and search operations.
Given below shows how trie data structure works in java:
Normally we can perform 3 different operations in a trie data structure as follows:
We already explain how insertion operation works in java on the above point. The complexity of insertion operation is O (n), where n represents the size of the key.
After insertion operation, we can perform the search or find operation on trie data structure by using the following algorithm as follows.
Code:
public void find_node(specified string of word){ TrieNode present = rootNode; For (char j = 0; j < word.length(); j++){ char c = word.charAt(j); TrieNode node = present.getChildren().get(c); If (node = = null){ return false; } Present = node; } return present.isEndOfWord(); }
Explanation:
Now follow the following steps for the search element in a trie data structure as follows:
Besides insertion operation and find the element; clearly, we likewise should have the option to delete operation, so we need to follow the following steps as follows.
Given below is the example of Trie Data Structure in Java:
Code:
import java.util.ArrayList; import java.util.Collections; import java.util.List; // created class to store node into the trie data structure class trie_data { // Define the size of alphabet size private static final int CHAR_AlPHA_SIZE = 26; private boolean isLeaf; private List<trie_data> child = null; // Created Constructor of class trie_data() { isLeaf = false; child = new ArrayList<>(Collections.nCopies(CHAR_AlPHA_SIZE, null)); } // function for insertion operation public void trie_insert(String id) { System.out.println("We inserted new element into the data structure \"" + id + "\""); // Staritng from the parent node that is root node trie_data present = this; for (char ch: id.toCharArray()) { // if node is not exist then create new node in trie if (present.child.get(ch - 'a') == null) { present.child.set(ch - 'a', new trie_data()); } // visit next node present = present.child.get(ch - 'a'); } // mark present as leaf node present.isLeaf = true; } // search function to search element into trie data structure // if key value is not present then it return the false public boolean trie_search(String id) { System.out.print("We searched element\"" + id + "\" : "); trie_data present = this; for (char ch: id.toCharArray()) { // visit next node present = present.child.get(ch - 'a'); if (present == null) { return false; } } return present.isLeaf; } } class Main { public static void main (String[] args) { // construct a new Trie node trie_data head = new trie_data(); head.trie_insert("the"); head.trie_insert("they"); head.trie_insert("final"); System.out.println(head.trie_search("the")); // true System.out.println(head.trie_search("they")); // true System.out.println(head.trie_search("final")); // true System.out.println(head.trie_search("Sample")); // false head.trie_insert("Sample"); System.out.println(head.trie_search("the")); // true System.out.println(head.trie_search("they")); // true System.out.println(head.trie_search("final")); // true System.out.println(head.trie_search("Sample")); // true } }
Explanation:
Output:
From the above article, we saw the basic syntax of Trie data structure, and we also saw different examples of Trie data structure. From this article, we saw how and when we use the Trie data structure in Java.
The above is the detailed content of Trie Data Structure in Java. For more information, please follow other related articles on the PHP Chinese website!