-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTries.java
More file actions
117 lines (102 loc) · 3.19 KB
/
Tries.java
File metadata and controls
117 lines (102 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package dsa;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Tries {
private class TrieNode{
private char value;
private HashMap<Character, TrieNode> children = new HashMap<>();
private boolean isEndOfWord;
public TrieNode(char value) {
this.value = value;
}
@Override
public String toString() {
return "value : " + value;
}
public boolean hasChild(char ch){
return children.containsKey(ch);
}
public void addChild(char ch){
children.put(ch, new TrieNode(ch));
}
public TrieNode getChild(char ch){
return children.get(ch);
}
private TrieNode[] getChildren(){
return children.values().toArray(new TrieNode[0]);
}
public boolean hasChildren(){
return !children.isEmpty();
}
public void removeChild(char ch){
children.remove(ch);
}
}
private TrieNode root = new TrieNode(' ');
public void insert(String word){
var current = root;
for (char ch : word.toCharArray()) {
if (!current.hasChild(ch))
current.addChild(ch);
current = current.getChild(ch);
current.isEndOfWord = true;
}
}
public boolean contains(String word){
if (word == null) return false;
var current = root;
for (char ch : word.toCharArray()) {
if(!current.hasChild(ch)) return false;
current = current.getChild(ch);
}
return current.isEndOfWord;
}
public void traverse(){
traverse(root);
}
private void traverse(TrieNode root){
System.out.print(root.value + " "); //pre-order Traversal
for (var child : root.getChildren()) {
traverse(child);
}
}
public void remove(String word){
if(word == null) return;
remove(root, word, 0);
}
private void remove(TrieNode root, String word, int index){
if(index == word.length()){
root.isEndOfWord = false;
return;
}
var ch = word.charAt(index);
var child = root.getChild(ch);
if(child == null) return;
remove(child, word, index + 1);
if(!child.hasChildren() && !child.isEndOfWord)
root.removeChild(ch);
}
public List <String> findWords(String prefix){
List <String> words = new ArrayList<>();
var lastNode = findLasTrieNode(prefix);
findWords(lastNode, prefix, words);
return words;
}
private void findWords(TrieNode root, String prefix, List <String> words){
if(root == null) return;
if(root.isEndOfWord) words.add(prefix);
for (var child : root.getChildren()) {
findWords(child, prefix + child.value, words);
}
}
private TrieNode findLasTrieNode(String prefix){
var current = root;
for (var ch : prefix.toCharArray()) {
var child = current.getChild(ch);
if(child == null) return null;
current = child;
}
return current;
}
}