Skip to content

Commit 806c15e

Browse files
committed
fix: Fix bug in pruning method
1 parent 1ea2f4c commit 806c15e

File tree

5 files changed

+29
-31
lines changed

5 files changed

+29
-31
lines changed

docs/assets/images/Trie.png

454 KB
Loading

src/main/java/dataStructures/trie/README.md

Whitespace-only changes.

src/main/java/dataStructures/trie/Trie.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dataStructures.trie;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.List;
6+
import java.util.Map;
57

68
/**
79
* Implementation of a Trie; Here we consider strings (not case-sensitive)
@@ -13,6 +15,18 @@ public Trie() {
1315
root = new TrieNode();
1416
}
1517

18+
private class TrieNode {
19+
// CHECKSTYLE:OFF: VisibilityModifier
20+
public Map<Character, TrieNode> children; // or array of size 26 (assume not case-sensitive) to denote each char
21+
// CHECKSTYLE:OFF: VisibilityModifier
22+
public boolean isEnd; // a marker to indicate whether the path from the root to this node forms a known word
23+
24+
public TrieNode() {
25+
children = new HashMap<Character, TrieNode>();
26+
isEnd = false;
27+
}
28+
}
29+
1630
/**
1731
* Inserts a word into the trie.
1832
* @param word
@@ -90,7 +104,7 @@ public void deleteAndPrune(String word) {
90104
char curr = word.charAt(i);
91105
TrieNode nodeBeforeCurr = trackNodes.get(i);
92106
TrieNode nextNode = nodeBeforeCurr.children.get(curr);
93-
if (nextNode.children.size() == 0) { // this node essentially doesn't track anything, remove
107+
if (!nextNode.isEnd && nextNode.children.size() == 0) { // node essentially doesn't track anything, remove
94108
nodeBeforeCurr.children.remove(curr);
95109
} else { // children.size() > 0; i.e. this node is still useful; no need to further prune upwards
96110
break;

src/main/java/dataStructures/trie/TrieNode.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/test/java/dataStructures/trie/TrieTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
import org.junit.Assert;
88
import org.junit.Test;
99

10-
/**
11-
* insert
12-
* search
13-
* delete
14-
* deleteAndPrune
15-
* wordsWithPrefix
16-
* getAllWords
17-
*/
1810
public class TrieTest {
1911
@Test
2012
public void search_shouldFindWordIfExists() {
@@ -72,6 +64,20 @@ public void deleteAndPrune_shouldPrune() {
7264
Assert.assertTrue(trie.checkNodeExistsAtPosition("cs3230", 2));
7365
}
7466

67+
@Test
68+
public void deleteAndPrune_shouldNotDeletePrefixWords() {
69+
Trie trie = new Trie();
70+
trie.insert("art");
71+
trie.insert("artist");
72+
trie.insert("artistic");
73+
trie.insert("artistically");
74+
75+
Assert.assertTrue(trie.search("artistically"));
76+
trie.deleteAndPrune("artistically");
77+
Assert.assertTrue(trie.search("artistic")); // should not be pruned away
78+
Assert.assertTrue(trie.search("art")); // should not be pruned away
79+
}
80+
7581
@Test
7682
public void wordsWithPrefix_shouldDisplayCurrentWordsThatMatches() {
7783
Trie trie = new Trie();

0 commit comments

Comments
 (0)