Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 692eafa

Browse filesBrowse files
authored
Fix Trie implementation (eugenp#4668)
* encoding * Fix Trie implementation
1 parent c51a976 commit 692eafa
Copy full SHA for 692eafa

File tree

Expand file treeCollapse file tree

4 files changed

+24
-30
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+24
-30
lines changed
Open diff view settings
Collapse file

‎data-structures/src/main/java/com/baeldung/trie/Trie.java‎

Copy file name to clipboardExpand all lines: data-structures/src/main/java/com/baeldung/trie/Trie.java
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.baeldung.trie;
22

3-
public class Trie {
3+
class Trie {
44
private TrieNode root;
55

66
Trie() {
77
root = new TrieNode();
88
}
99

10-
public void insert(String word) {
10+
void insert(String word) {
1111
TrieNode current = root;
1212

1313
for (int i = 0; i < word.length(); i++) {
@@ -16,11 +16,11 @@ public void insert(String word) {
1616
current.setEndOfWord(true);
1717
}
1818

19-
public boolean delete(String word) {
19+
boolean delete(String word) {
2020
return delete(root, word, 0);
2121
}
2222

23-
public boolean containsNode(String word) {
23+
boolean containsNode(String word) {
2424
TrieNode current = root;
2525

2626
for (int i = 0; i < word.length(); i++) {
@@ -34,7 +34,7 @@ public boolean containsNode(String word) {
3434
return current.isEndOfWord();
3535
}
3636

37-
public boolean isEmpty() {
37+
boolean isEmpty() {
3838
return root == null;
3939
}
4040

@@ -51,7 +51,7 @@ private boolean delete(TrieNode current, String word, int index) {
5151
if (node == null) {
5252
return false;
5353
}
54-
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
54+
boolean shouldDeleteCurrentNode = delete(node, word, index + 1) && !node.isEndOfWord();
5555

5656
if (shouldDeleteCurrentNode) {
5757
current.getChildren().remove(ch);
Collapse file

‎data-structures/src/main/java/com/baeldung/trie/TrieNode.java‎

Copy file name to clipboardExpand all lines: data-structures/src/main/java/com/baeldung/trie/TrieNode.java
+4-14Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,18 @@
44
import java.util.Map;
55

66
class TrieNode {
7-
private Map<Character, TrieNode> children;
7+
private final Map<Character, TrieNode> children = new HashMap<>();
88
private boolean endOfWord;
99

10-
public TrieNode() {
11-
children = new HashMap<>();
12-
endOfWord = false;
13-
}
14-
15-
public Map<Character, TrieNode> getChildren() {
10+
Map<Character, TrieNode> getChildren() {
1611
return children;
1712
}
1813

19-
public void setChildren(Map<Character, TrieNode> children) {
20-
this.children = children;
21-
}
22-
23-
public boolean isEndOfWord() {
14+
boolean isEndOfWord() {
2415
return endOfWord;
2516
}
2617

27-
public void setEndOfWord(boolean endOfWord) {
18+
void setEndOfWord(boolean endOfWord) {
2819
this.endOfWord = endOfWord;
2920
}
30-
3121
}
Collapse file

‎data-structures/src/test/java/com/baeldung/trie/TrieTest.java‎

Copy file name to clipboardExpand all lines: data-structures/src/test/java/com/baeldung/trie/TrieTest.java
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.trie;
22

33
import org.junit.Test;
4+
import org.junit.jupiter.api.Assertions;
45

56
import static org.junit.Assert.assertFalse;
67
import static org.junit.Assert.assertTrue;
@@ -53,6 +54,19 @@ public void givenATrie_whenDeletingElements_thenTreeDoesNotContainThoseElements(
5354
assertFalse(trie.containsNode("Programming"));
5455
}
5556

57+
@Test
58+
public void givenATrie_whenDeletingOverlappingElements_thenDontDeleteSubElement() {
59+
60+
Trie trie1 = new Trie();
61+
62+
trie1.insert("pie");
63+
trie1.insert("pies");
64+
65+
trie1.delete("pies");
66+
67+
Assertions.assertTrue(trie1.containsNode("pie"));
68+
}
69+
5670
private Trie createExampleTrie() {
5771
Trie trie = new Trie();
5872

Collapse file

‎spring-boot-ops/README.md‎

Copy file name to clipboardExpand all lines: spring-boot-ops/README.md
-10Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.