- Timestamp:
- 04/11/11 15:42:42 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/Trie.java
r5 r6 21 21 public class Trie<T> { 22 22 23 // Children of the Trie root 24 // should contain counts of all elements 25 private List<TrieNode<T>> children = new LinkedList<TrieNode<T>>(); 23 private final TrieNode<T> rootNode; 24 25 public Trie() { 26 rootNode = new TrieNode<T>(); 27 } 26 28 27 29 … … 30 32 subsequence = new LinkedList<T>(subsequence); // defensive copy! 31 33 T firstSymbol = subsequence.get(0); 32 getChildCreate(firstSymbol).add(subsequence); 34 TrieNode<T> node = getChildCreate(firstSymbol); 35 node.add(subsequence); 33 36 } 34 37 } 35 38 36 // FIXME clones of TrieNode.getChildCreate37 39 protected TrieNode<T> getChildCreate(T symbol) { 38 TrieNode<T> node = getChild(symbol); 39 if( node==null ) { 40 node = new TrieNode<T>(symbol); 41 children.add(node); 42 } 43 return node; 40 return rootNode.getChildCreate(symbol); 44 41 } 45 42 46 // FIXME clones of TrieNode.getChild47 43 protected TrieNode<T> getChild(T symbol) { 48 for( TrieNode<T> child : children ) { 49 if( child.getSymbol().equals(symbol) ) { 50 return child; 51 } 52 } 53 return null; 44 return rootNode.getChild(symbol); 54 45 } 55 46 … … 73 64 74 65 public TrieNode<T> find(List<T> sequence) { 66 if( sequence==null || sequence.isEmpty() ) { 67 return rootNode; 68 } 75 69 List<T> sequenceCopy = new LinkedList<T>(sequence); 76 70 TrieNode<T> result = null; 77 if( !sequenceCopy.isEmpty() ) { 78 TrieNode<T> node = getChild(sequenceCopy.get(0)); 79 if( node!=null ) { 80 sequenceCopy.remove(0); 81 result = node.find(sequenceCopy); 82 } 71 TrieNode<T> node = getChild(sequenceCopy.get(0)); 72 if( node!=null ) { 73 sequenceCopy.remove(0); 74 result = node.find(sequenceCopy); 83 75 } 84 76 return result; … … 88 80 public List<T> getFollowingSymbols(List<T> sequence) { 89 81 List<T> result = new LinkedList<T>(); 90 if( sequence==null || sequence.isEmpty() ) { 91 for( TrieNode<T> child : children ) { 92 result.add(child.getSymbol()); 93 } 94 } else { 95 TrieNode<T> node = find(sequence); 96 if( node!=null ) { 97 result = node.getFollowingSymbols(); 98 } 82 TrieNode<T> node = find(sequence); 83 if( node!=null ) { 84 result = node.getFollowingSymbols(); 99 85 } 100 86 return result; … … 102 88 103 89 // longest suffix of context, that is contained in the tree and whose children are leaves 90 // possibly already deprecated 104 91 public List<T> getContextSuffix(List<T> context) { 105 92 List<T> contextSuffix = new LinkedList<T>(context); // defensive copy … … 140 127 private Tree<TrieVertex, Edge> getGraph() { 141 128 DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>(); 142 143 TrieVertex root = new TrieVertex("root"); 144 graph.addVertex(root); 145 146 for( TrieNode<T> node : children ) { 147 node.getGraph(root, graph); 148 } 149 129 rootNode.getGraph(null, graph); 150 130 return graph; 151 131 } … … 181 161 @Override 182 162 public String toString() { 183 return children.toString();163 return rootNode.toString(); 184 164 } 185 165 }
Note: See TracChangeset
for help on using the changeset viewer.