diff --git a/data_structures/Trees/TrieImp.java b/data_structures/Trees/TrieImp.java new file mode 100644 index 00000000..c55586ff --- /dev/null +++ b/data_structures/Trees/TrieImp.java @@ -0,0 +1,135 @@ +//Trie Data structure implementation without any libraries */ + +/** + * + * @author Dheeraj Kumar Barnwal (https://github.com/dheeraj92) + * + */ +import java.util.Scanner; + +public class TrieImp { + + public class TrieNode { + TrieNode[] child; + boolean end; + + public TrieNode(){ + child = new TrieNode[26]; + end = false; + } + } + private final TrieNode root; + public TrieImp(){ + root = new TrieNode(); + } + + public void insert(String word){ + TrieNode currentNode = root; + for(int i=0; i < word.length();i++){ + TrieNode node = currentNode.child[word.charAt(i)-'a']; + if(node == null){ + node = new TrieNode(); + currentNode.child[word.charAt(i)-'a']=node; + } + currentNode = node; + } + currentNode.end = true; + } + public boolean search(String word){ + TrieNode currentNode = root; + for(int i=0;i