分类:
2011-01-12 13:15:50
import java.util.*;
public class TrieNode 
...{
// Attributes
private final int CHILD = 256;
private String value;
private TrieNode [] child = new TrieNode[CHILD];
private Map<Integer, List<Integer>> positions;
// Constructor
public TrieNode() ...{}
public TrieNode (String v)
...{
this.value = v;
}
// Method (set/get)
public void setValue(String v)
...{
this.value = v;
}
public String getValue()
...{
return this.value;
}
public void addChild(int index, TrieNode tn)
...{
this.child[index] = tn;
}
public TrieNode getChildAt(int index)
...{
if (index > CHILD - 1 || index<0)
...{
return null;
}
return this.child[index];
}
public void addPosition(int lineno, int pos)
...{
Integer lineInt = new Integer(lineno);
if (this.positions == null)
...{
this.positions = new TreeMap<Integer, List<Integer>>();
}
List<Integer> posList = this.positions.get(lineInt);
if (posList == null)
...{
posList = new LinkedList<Integer>();
}
posList.add(new Integer(pos));
this.positions.put(lineInt, posList);
}
public List<Integer> getPosition(int lineno)
...{
return this.positions.get(Integer.valueOf(lineno));
}
public Map<Integer, List<Integer>> getPositions()
...{
return this.positions;
}
public void dump()
...{
for (Map.Entry<Integer, List<Integer>> entry : this.positions.entrySet())
...{
System.out.print(entry.getKey().intValue() + " - ");
for (Integer pos : entry.getValue())
...{
System.out.print(pos.intValue() + ":");
}
System.out.println();
}
}
}
import java.util.regex.*;
import java.io.*;
public class Trie 
...{
private String fileName;
private TrieNode root;
public Trie(String fileName)
...{
this.fileName = fileName;
root = new TrieNode();
}
public void parse()
...{
try
...{
BufferedReader br = new BufferedReader(new FileReader(this.fileName));
String line = null;
int lineno = 0;
Pattern pattern = Pattern.compile("\w*", Pattern.UNICODE_CASE);
Matcher m;
while ((line=br.readLine())!=null)
...{
lineno++;
m = pattern.matcher(line);
while (m.find())
...{
int startPos = m.start();
String subStr = m.group();
if (subStr.length() == 0)
...{
continue;
}
this.insert(subStr, lineno, startPos);
}
}
}
catch (Exception e)
...{
e.printStackTrace();
this.root = null;
}
}
/** *//**
*
*/
private TrieNode search(byte [] strBytes, int index, TrieNode node)
...{
if (index >= strBytes.length)
...{
//System.out.println("查找次数:" + index);
return node;
}
int indexInNode = strBytes[index] & 0xFF;
TrieNode trieNode = node.getChildAt(indexInNode);
if (trieNode == null)
...{
trieNode = new TrieNode();
node.addChild(indexInNode, trieNode);
}
return search(strBytes, ++index, trieNode);
}
public TrieNode search(String search)
...{
return search(search.getBytes(), 0, this.root);
}
// 应该首先查找,直到找到该字符串中没有走到但是还需要走的路
private void insert(String str, int lineno, int pos)
...{
byte [] strBytes = str.getBytes();
TrieNode trieNode = search(strBytes, 0, this.root);
trieNode.setValue(str);
trieNode.addPosition(lineno, pos);
}
public void dump()
...{
if (this.root != null)
...{
System.out.println("-----");
String searchStr = "String";
TrieNode trieNode = this.search(searchStr);
if (trieNode.getValue() != null && trieNode.getValue().equals(searchStr))
...{
trieNode.dump();
}
}
}
// 深度优先
private void dumpAll(TrieNode trieNode)
...{
String value = trieNode.getValue();
if (value != null)
...{
System.out.println("##" + value + "##");
trieNode.dump();
}
TrieNode node;
for (int i=0 ;i<256 ;i++ )
...{
node = trieNode.getChildAt(i);
if (node != null)
...{
dumpAll(node);
}
}
}
public void dumpAll()
...{
this.dumpAll(this.root);
}
public static void main(String[] args) 
...{
Trie trie = new Trie(args[0]);
trie.parse();
trie.dump();
//trie.dumpAll();
}
}
-----
6 - 9:
8 - 13:
18 - 3:
31 - 5:
65 - 24:
70 - 21:
83 - 3:23:
94 - 2:
115 - 25: