parent
c51a97678c
commit
692eafafea
|
@ -1,13 +1,13 @@
|
||||||
package com.baeldung.trie;
|
package com.baeldung.trie;
|
||||||
|
|
||||||
public class Trie {
|
class Trie {
|
||||||
private TrieNode root;
|
private TrieNode root;
|
||||||
|
|
||||||
Trie() {
|
Trie() {
|
||||||
root = new TrieNode();
|
root = new TrieNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insert(String word) {
|
void insert(String word) {
|
||||||
TrieNode current = root;
|
TrieNode current = root;
|
||||||
|
|
||||||
for (int i = 0; i < word.length(); i++) {
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
@ -16,11 +16,11 @@ public class Trie {
|
||||||
current.setEndOfWord(true);
|
current.setEndOfWord(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean delete(String word) {
|
boolean delete(String word) {
|
||||||
return delete(root, word, 0);
|
return delete(root, word, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsNode(String word) {
|
boolean containsNode(String word) {
|
||||||
TrieNode current = root;
|
TrieNode current = root;
|
||||||
|
|
||||||
for (int i = 0; i < word.length(); i++) {
|
for (int i = 0; i < word.length(); i++) {
|
||||||
|
@ -34,7 +34,7 @@ public class Trie {
|
||||||
return current.isEndOfWord();
|
return current.isEndOfWord();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty() {
|
boolean isEmpty() {
|
||||||
return root == null;
|
return root == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ public class Trie {
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
|
boolean shouldDeleteCurrentNode = delete(node, word, index + 1) && !node.isEndOfWord();
|
||||||
|
|
||||||
if (shouldDeleteCurrentNode) {
|
if (shouldDeleteCurrentNode) {
|
||||||
current.getChildren().remove(ch);
|
current.getChildren().remove(ch);
|
||||||
|
|
|
@ -4,28 +4,18 @@ import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
class TrieNode {
|
class TrieNode {
|
||||||
private Map<Character, TrieNode> children;
|
private final Map<Character, TrieNode> children = new HashMap<>();
|
||||||
private boolean endOfWord;
|
private boolean endOfWord;
|
||||||
|
|
||||||
public TrieNode() {
|
Map<Character, TrieNode> getChildren() {
|
||||||
children = new HashMap<>();
|
|
||||||
endOfWord = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<Character, TrieNode> getChildren() {
|
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setChildren(Map<Character, TrieNode> children) {
|
boolean isEndOfWord() {
|
||||||
this.children = children;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEndOfWord() {
|
|
||||||
return endOfWord;
|
return endOfWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEndOfWord(boolean endOfWord) {
|
void setEndOfWord(boolean endOfWord) {
|
||||||
this.endOfWord = endOfWord;
|
this.endOfWord = endOfWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.trie;
|
package com.baeldung.trie;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -53,6 +54,19 @@ public class TrieTest {
|
||||||
assertFalse(trie.containsNode("Programming"));
|
assertFalse(trie.containsNode("Programming"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenATrie_whenDeletingOverlappingElements_thenDontDeleteSubElement() {
|
||||||
|
|
||||||
|
Trie trie1 = new Trie();
|
||||||
|
|
||||||
|
trie1.insert("pie");
|
||||||
|
trie1.insert("pies");
|
||||||
|
|
||||||
|
trie1.delete("pies");
|
||||||
|
|
||||||
|
Assertions.assertTrue(trie1.containsNode("pie"));
|
||||||
|
}
|
||||||
|
|
||||||
private Trie createExampleTrie() {
|
private Trie createExampleTrie() {
|
||||||
Trie trie = new Trie();
|
Trie trie = new Trie();
|
||||||
|
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
### Relevant Articles:
|
|
||||||
|
|
||||||
- [Deploy a Spring Boot WAR into a Tomcat Server](http://www.baeldung.com/spring-boot-war-tomcat-deploy)
|
|
||||||
- [Spring Boot Dependency Management with a Custom Parent](http://www.baeldung.com/spring-boot-dependency-management-custom-parent)
|
|
||||||
- [A Custom Data Binder in Spring MVC](http://www.baeldung.com/spring-mvc-custom-data-binder)
|
|
||||||
- [Create a Fat Jar App with Spring Boot](http://www.baeldung.com/deployable-fat-jar-spring-boot)
|
|
||||||
- [Introduction to WebJars](http://www.baeldung.com/maven-webjars)
|
|
||||||
- [Intro to Spring Boot Starters](http://www.baeldung.com/spring-boot-starters)
|
|
||||||
- [A Quick Guide to Maven Wrapper](http://www.baeldung.com/maven-wrapper)
|
|
||||||
- [Shutdown a Spring Boot Application](http://www.baeldung.com/spring-boot-shutdown)
|
|
Loading…
Reference in New Issue