Skip to content

Commit ec38fea

Browse files
committed
Checking in Trie implementation
1 parent 0d41140 commit ec38fea

File tree

5 files changed

+159
-2
lines changed

5 files changed

+159
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ Below topics/problems are covered as of now.
9999
- [X] [Dequeue](../master/src/com/deepak/data/structures/Queue/Dequeue.java)
100100

101101
**11. Trie**
102-
- [ ] Introduction
102+
- [X] [Introduction](../master/src/com/deepak/data/structures/Trie/TrieIntroduction.md)
103+
- [X] [Implement Trie](../master/src/com/deepak/data/structures/Trie/TrieImplementation.java)
103104

104105
**12. Treap**
105106
- [ ] Introduction

src/com/deepak/data/structures/Stack/ArrayBasedStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-And-Algorithms-in-Java
2+
* Data-Structures-in-Java
33
* ArrayBasedStack.java
44
*/
55
package com.deepak.data.structures.Stack;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* TrieImplementation.java
4+
*/
5+
package com.deepak.data.structures.Trie;
6+
7+
import java.util.HashMap;
8+
9+
/**
10+
* Class to hold Trie implementation
11+
* Not making it generic since keys are usually strings
12+
*
13+
* @author Deepak
14+
*/
15+
public class TrieImplementation {
16+
17+
/**
18+
* Root Node
19+
*/
20+
private TrieNode root;
21+
22+
/**
23+
* Default Constructor
24+
*/
25+
public TrieImplementation() {
26+
root = new TrieNode();
27+
}
28+
29+
/**
30+
* Method to insert a word in Trie
31+
* @param word
32+
*/
33+
public void insert(String word) {
34+
HashMap<Character, TrieNode> children = root.children;
35+
for (int i = 0; i < word.length(); i++) {
36+
char character = word.charAt(i);
37+
TrieNode node = null;
38+
if (children.containsKey(character)) {
39+
node = children.get(character);
40+
} else {
41+
node = new TrieNode(character);
42+
children.put(character, node);
43+
}
44+
children = node.children;
45+
if (i == word.length() - 1) {
46+
node.isLeaf = true;
47+
}
48+
}
49+
}
50+
51+
/**
52+
* Method to check if a word in Trie starts with the given prefix
53+
* @param prefix
54+
* @return {@link boolean}
55+
*/
56+
public boolean startsWith(String prefix) {
57+
if (searchNode(prefix) != null) {
58+
return true;
59+
}
60+
return false;
61+
}
62+
63+
/**
64+
* Method to search TrieNode based on the word
65+
* @param word
66+
* @return {@link TrieNode}
67+
*/
68+
public TrieNode searchNode(String word) {
69+
HashMap<Character, TrieNode> children = root.children;
70+
TrieNode node = null;
71+
for (int i = 0; i < word.length(); i++) {
72+
char character = word.charAt(i);
73+
if (children.containsKey(character)) {
74+
node = children.get(character);
75+
children = node.children;
76+
} else {
77+
return null;
78+
}
79+
}
80+
return node;
81+
}
82+
83+
/**
84+
* Method to search the given word in the Trie
85+
* @param word
86+
* @return {@link boolean}
87+
*/
88+
public boolean searchWord(String word) {
89+
TrieNode node = searchNode(word);
90+
if (null != node && node.isLeaf) {
91+
return true;
92+
}
93+
return false;
94+
}
95+
96+
/**
97+
* Static class TrieNode
98+
*
99+
* @author Deepak
100+
*/
101+
public static class TrieNode {
102+
103+
/* Attributes of TrieNode*/
104+
char c;
105+
HashMap<Character, TrieNode> children = new HashMap<>();
106+
boolean isLeaf;
107+
108+
/**
109+
* Default Constructor
110+
*/
111+
public TrieNode() {}
112+
113+
/**
114+
* Parameterized Constructor
115+
* @param c
116+
*/
117+
public TrieNode(char c) {
118+
this.c = c;
119+
}
120+
121+
}
122+
123+
}

src/com/deepak/data/structures/Trie/TrieIntroduction.md

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Data-Structures-in-Java
3+
* TrieTest.java
4+
*/
5+
package com.deepak.data.structures.Trie;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases to test Trie Data Structure
12+
*
13+
* @author Deepak
14+
*/
15+
public class TrieTest {
16+
17+
/**
18+
* Test case to test all the Trie functionalities
19+
*/
20+
@Test
21+
public void testTrieFunctionality() {
22+
TrieImplementation trie = new TrieImplementation();
23+
trie.insert("Zebra");
24+
trie.insert("Cat");
25+
trie.insert("Dog");
26+
Assert.assertTrue(trie.startsWith("D"));
27+
Assert.assertNotNull(trie.searchNode("Cat"));
28+
Assert.assertNotNull(trie.searchNode("Dog"));
29+
Assert.assertTrue(trie.startsWith("Zebra"));
30+
Assert.assertFalse(trie.startsWith("M"));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)