-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrieSet.java
More file actions
103 lines (79 loc) · 2.33 KB
/
TrieSet.java
File metadata and controls
103 lines (79 loc) · 2.33 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
import java.util.*;
public class TrieSet implements Set<String>{
boolean isWord;
HashMap<Character, TrieSet> children = new HashMap<>();
public boolean add(String word){
return add(word, 0);
}
private boolean add(String word, int i){
if(i == word.length()){
boolean was = this.isWord;
this.isWord = true;
return was;
}
else{
char c = word.charAt(i);
TrieSet child;
if(!children.containsKey(c)){
children.put(c, new TrieSet());
}
child = children.get(c);
return child.add(word, i+1);
}
}
public boolean contains(Object o){
return contains((String) o, 0);
}
private boolean contains(String word, int i){
if(i == word.length()){
return this.isWord;
}
else{
if(children.containsKey(word.charAt(i))){
TrieSet child = children.get(word.charAt(i));
return child.contains(word, i+1);
}
else
return false;
}
}
//TODO
public boolean isEmpty(){
return size()!=0;
}
public int size(){
throw new UnsupportedOperationException();
}
public Object[] toArray(){
throw new UnsupportedOperationException();
}
public Iterator<String> iterator(){
throw new UnsupportedOperationException();
}
public void clear(){
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection<?> c){
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection<?> c){
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends String> c){
throw new UnsupportedOperationException();
}
public boolean containsAll(Collection<?> c){
throw new UnsupportedOperationException();
}
public boolean remove(Object o){
throw new UnsupportedOperationException();
}
public <T> T[] toArray(T[] a){
throw new UnsupportedOperationException();
}
public static void main(String[] args){
TrieSet t = new TrieSet();
t.add("pigs");
System.out.println(t.contains("pbgs"));
}
}