-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrieNode.java
62 lines (54 loc) · 1.54 KB
/
TrieNode.java
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
import java.util.*;
public class TrieNode implements Iterable<TrieNode>{
List<Stop> stops;
HashMap<Character, TrieNode> children;
public TrieNode() {
stops = new ArrayList<>();
children = new HashMap<>();
}
/**
* Get all Character keys that belong to this node's children
* @return set of keys that bind to all of this node's children
*/
public Set<Character> getChildChars() {
return children.keySet();
}
/**
* Get child node corresponding to input Character
* @param c Character/key to find
* @return child node corresponding to Character
*/
public TrieNode getChild(Character c) {
return children.get(c);
}
/**
* Get stops belonging to this node only
* @return List of Stops in this node
*/
public List<Stop> getStops() {
return stops;
}
/**
* Add a new and empty child node to this current node
* Must call addStop() to add objects to this node
* @param c Character that binds to this new node
*/
public void addChild(Character c) {
children.put(c, new TrieNode());
}
/**
* Add Stop belonging to this node
* @param s Stop belonging to this node
*/
public void addStop(Stop s) {
stops.add(s);
}
/**
* Allows us to iterate through all children of this node
* e.g. using foreach loop
* @return iterator of children collection
*/
public Iterator<TrieNode> iterator() {
return children.values().iterator();
}
}