1 | package de.ugoe.cs.eventbench.ppm;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import de.ugoe.cs.util.StringTools;
|
---|
8 |
|
---|
9 |
|
---|
10 | class TrieNode<T> {
|
---|
11 |
|
---|
12 | private int count;
|
---|
13 | private final T symbol;
|
---|
14 |
|
---|
15 | private List<TrieNode<T>> children;
|
---|
16 |
|
---|
17 | public TrieNode(T symbol) {
|
---|
18 | if( symbol==null ) {
|
---|
19 | throw new InvalidParameterException("symbol must not be null.");
|
---|
20 | }
|
---|
21 | this.symbol = symbol;
|
---|
22 | count = 0;
|
---|
23 | children = new LinkedList<TrieNode<T>>();
|
---|
24 | }
|
---|
25 |
|
---|
26 | public void add(List<T> subsequence) {
|
---|
27 | if( !subsequence.isEmpty() ) {
|
---|
28 | if( !symbol.equals(subsequence.get(0)) ) { // should be guaranteed by the recursion/TrieRoot!
|
---|
29 | throw new AssertionError("Invalid trie operation!");
|
---|
30 | }
|
---|
31 | count++;
|
---|
32 | subsequence.remove(0);
|
---|
33 | if( !subsequence.isEmpty() ) {
|
---|
34 | T nextSymbol = subsequence.get(0);
|
---|
35 | getChildCreate(nextSymbol).add(subsequence);
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public T getSymbol() {
|
---|
41 | return symbol;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public int getCount() {
|
---|
45 | return count;
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected TrieNode<T> getChildCreate(T symbol) {
|
---|
49 | TrieNode<T> node = getChild(symbol);
|
---|
50 | if( node==null ) {
|
---|
51 | node = new TrieNode<T>(symbol);
|
---|
52 | children.add(node);
|
---|
53 | }
|
---|
54 | return node;
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected TrieNode<T> getChild(T symbol) {
|
---|
58 | for( TrieNode<T> child : children ) {
|
---|
59 | if( child.getSymbol().equals(symbol) ) {
|
---|
60 | return child;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | return null;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | public TrieNode<T> find(List<T> subsequence) {
|
---|
69 | TrieNode<T> result = null;
|
---|
70 | if( subsequence.isEmpty() ) {
|
---|
71 | result = this;
|
---|
72 | } else {
|
---|
73 | TrieNode<T> node = getChild(subsequence.get(0));
|
---|
74 | if( node!=null ) {
|
---|
75 | subsequence.remove(0);
|
---|
76 | result = node.find(subsequence);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return result;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public List<T> getFollowingSymbols() {
|
---|
83 | List<T> followingSymbols = new LinkedList<T>();
|
---|
84 | for( TrieNode<T> child : children ) {
|
---|
85 | followingSymbols.add(child.getSymbol());
|
---|
86 | }
|
---|
87 | return followingSymbols;
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public String toString() {
|
---|
92 | String str = symbol.toString()+" #"+count;
|
---|
93 | if( !children.isEmpty() ) {
|
---|
94 | str += StringTools.ENDLINE + children.toString();
|
---|
95 | }
|
---|
96 | return str;
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|