From 077b76336ec8e9dc7a6a7e4ea0905f300a7512cb Mon Sep 17 00:00:00 2001 From: Dheeraj Kumar Barnwal Date: Tue, 3 Oct 2017 23:55:02 +0530 Subject: [PATCH] Trie Data structure Trie Data structure implementation without any libraries --- data_structures/Trees/TrieImp.java | 135 +++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 data_structures/Trees/TrieImp.java 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