|
| 1 | + |
| 2 | +public class Trie { |
| 3 | + |
| 4 | + // Alphabet size (# of symbols) |
| 5 | + static final int ALPHABET_SIZE = 26; |
| 6 | + |
| 7 | + // trie node |
| 8 | + static class TrieNode |
| 9 | + { |
| 10 | + TrieNode[] children = new TrieNode[ALPHABET_SIZE]; |
| 11 | + |
| 12 | + // isEndOfWord is true if the node represents |
| 13 | + // end of a word |
| 14 | + boolean isEndOfWord; |
| 15 | + |
| 16 | + TrieNode(){ |
| 17 | + isEndOfWord = false; |
| 18 | + for (int i = 0; i < ALPHABET_SIZE; i++) |
| 19 | + children[i] = null; |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + static TrieNode root; |
| 24 | + |
| 25 | + // If not present, inserts key into trie |
| 26 | + // If the key is prefix of trie node, |
| 27 | + // just marks leaf node |
| 28 | + static void insert(String key) |
| 29 | + { |
| 30 | + int level; |
| 31 | + int length = key.length(); |
| 32 | + int index; |
| 33 | + |
| 34 | + TrieNode pCrawl = root; |
| 35 | + |
| 36 | + for (level = 0; level < length; level++) |
| 37 | + { |
| 38 | + index = key.charAt(level) - 'a'; |
| 39 | + if (pCrawl.children[index] == null) |
| 40 | + pCrawl.children[index] = new TrieNode(); |
| 41 | + |
| 42 | + pCrawl = pCrawl.children[index]; |
| 43 | + } |
| 44 | + |
| 45 | + // mark last node as leaf |
| 46 | + pCrawl.isEndOfWord = true; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + static boolean search(String key) |
| 51 | + { |
| 52 | + int level; |
| 53 | + int length = key.length(); |
| 54 | + int index; |
| 55 | + TrieNode pCrawl = root; |
| 56 | + |
| 57 | + for (level = 0; level < length; level++) |
| 58 | + { |
| 59 | + index = key.charAt(level) - 'a'; |
| 60 | + |
| 61 | + if (pCrawl.children[index] == null) |
| 62 | + return false; |
| 63 | + |
| 64 | + pCrawl = pCrawl.children[index]; |
| 65 | + } |
| 66 | + |
| 67 | + return (pCrawl != null && pCrawl.isEndOfWord); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public static void main(String args[]) |
| 72 | + { |
| 73 | + // Input keys (use only 'a' through 'z' and lower case) |
| 74 | + String keys[] = {"the", "a", "there", "answer", "any", |
| 75 | + "by", "bye", "their"}; |
| 76 | + |
| 77 | + String output[] = {"Not present in trie", "Present in trie"}; |
| 78 | + |
| 79 | + |
| 80 | + root = new TrieNode(); |
| 81 | + |
| 82 | + // Construct trie |
| 83 | + int i; |
| 84 | + for (i = 0; i < keys.length ; i++) |
| 85 | + insert(keys[i]); |
| 86 | + |
| 87 | + // Search for different keys |
| 88 | + if(search("the") == true) |
| 89 | + System.out.println("the --- " + output[1]); |
| 90 | + else System.out.println("the --- " + output[0]); |
| 91 | + |
| 92 | + if(search("these") == true) |
| 93 | + System.out.println("these --- " + output[1]); |
| 94 | + else System.out.println("these --- " + output[0]); |
| 95 | + |
| 96 | + if(search("their") == true) |
| 97 | + System.out.println("their --- " + output[1]); |
| 98 | + else System.out.println("their --- " + output[0]); |
| 99 | + |
| 100 | + if(search("thaw") == true) |
| 101 | + System.out.println("thaw --- " + output[1]); |
| 102 | + else System.out.println("thaw --- " + output[0]); |
| 103 | + |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments