TrieTree的实现
2016-06-23 16:43
176 查看
TrieTree是字典树,在文本处理中非常好用。
先建立节点
/**
* Created by kyle on 2016/6/23.
*/
public class TrieNode {
private final static int NUMBER = 26;
private char _value ;
private boolean isWorld;
private TrieNode[] _children = new TrieNode[NUMBER];
public TrieNode(char _value) {
this._value = _value;
for(TrieNode tn : _children){
tn = null;
}
isWorld = false;
}
public boolean isWorld() {
return isWorld;
}
public void setWorld(boolean world) {
isWorld = world;
}
public char get_value() {
return _value;
}
public void set_value(char _value) {
this._value = _value;
}
public TrieNode[] getChildren() {
return _children;
}
}
那么TrieTree的操作
import java.util.Arrays;
import java.util.Stack;
/**
* Created by kyle on 2016/6/23.
*/
public class TrieTree {
TrieNode _root;
public TrieTree() {
this._root =new TrieNode(' ');
}
public void insertIntoTree(String _word) {//插入一个单词
if(_root == null || _word == null || _word.equals("")){
return;
}
char[] cs = _word.toCharArray();
TrieNode current = _root;
for(int i = 0; i< cs.length ; i++){
int offset = (cs[i]- 'a');
if(current.getChildren()[offset] == null){
current.getChildren()[offset] = new TrieNode(cs[i]);
}
current = current.getChildren()[offset];
}
current.setWorld(true);
}
private void _printTree(TrieNode root, Stack<Character> stack){
if(root == null){
return;
}
stack.push(root.get_value());
if(root.isWorld()){
System.out.println(stack.toString());
}
for(TrieNode trieNode : root.getChildren()){
_printTree(trieNode, stack);
}
stack.pop();
}
public void printTree(){
Stack<Character> stack = new Stack<>();
_printTree(_root,stack);
}
public boolean searchWord(String _word){
if(_root == null || _word == null || _word.equals("")){
return false;
}
char[] cs = _word.toCharArray();
TrieNode current = _root;
for(int i = 0; i< cs.length ; i++){
int offset = (cs[i]- 'a');
if(current.getChildren()[offset] == null){
return false;
}else {
current = current.getChildren()[offset];
}
}
// if(current.get_value() == cs[cs.length-1]){
// return true;
// }
return current.isWorld();
// if(current.isWorld() == true){
// return true;
// }
// return false;
}
public boolean deletWord(String _word){
if(_root == null || _word == null || _word.equals("")){
return false;
}
char[] cs = _word.toCharArray();
TrieNode current = _root;
for(int i = 0; i< cs.length ; i++){
int offset = (cs[i]- 'a');
if(current.getChildren()[offset] == null){
return false;
}else {
current = current.getChildren()[offset];
}
}
return true;
}
}
相关测试为:
/**
* Created by kyle on 2016/6/23.
*/
public class Main {
public static void main(String[] args){
System.out.println("hello");
String[] words={"hello", "hi", "what", "how", "wow", "he", "heel"};
TrieTree trieTree = new TrieTree();
for(String word: words){
trieTree.insertIntoTree(word);
}
trieTree.printTree();
System.out.println(trieTree.searchWord("he"));
}
}
先建立节点
/**
* Created by kyle on 2016/6/23.
*/
public class TrieNode {
private final static int NUMBER = 26;
private char _value ;
private boolean isWorld;
private TrieNode[] _children = new TrieNode[NUMBER];
public TrieNode(char _value) {
this._value = _value;
for(TrieNode tn : _children){
tn = null;
}
isWorld = false;
}
public boolean isWorld() {
return isWorld;
}
public void setWorld(boolean world) {
isWorld = world;
}
public char get_value() {
return _value;
}
public void set_value(char _value) {
this._value = _value;
}
public TrieNode[] getChildren() {
return _children;
}
}
那么TrieTree的操作
import java.util.Arrays;
import java.util.Stack;
/**
* Created by kyle on 2016/6/23.
*/
public class TrieTree {
TrieNode _root;
public TrieTree() {
this._root =new TrieNode(' ');
}
public void insertIntoTree(String _word) {//插入一个单词
if(_root == null || _word == null || _word.equals("")){
return;
}
char[] cs = _word.toCharArray();
TrieNode current = _root;
for(int i = 0; i< cs.length ; i++){
int offset = (cs[i]- 'a');
if(current.getChildren()[offset] == null){
current.getChildren()[offset] = new TrieNode(cs[i]);
}
current = current.getChildren()[offset];
}
current.setWorld(true);
}
private void _printTree(TrieNode root, Stack<Character> stack){
if(root == null){
return;
}
stack.push(root.get_value());
if(root.isWorld()){
System.out.println(stack.toString());
}
for(TrieNode trieNode : root.getChildren()){
_printTree(trieNode, stack);
}
stack.pop();
}
public void printTree(){
Stack<Character> stack = new Stack<>();
_printTree(_root,stack);
}
public boolean searchWord(String _word){
if(_root == null || _word == null || _word.equals("")){
return false;
}
char[] cs = _word.toCharArray();
TrieNode current = _root;
for(int i = 0; i< cs.length ; i++){
int offset = (cs[i]- 'a');
if(current.getChildren()[offset] == null){
return false;
}else {
current = current.getChildren()[offset];
}
}
// if(current.get_value() == cs[cs.length-1]){
// return true;
// }
return current.isWorld();
// if(current.isWorld() == true){
// return true;
// }
// return false;
}
public boolean deletWord(String _word){
if(_root == null || _word == null || _word.equals("")){
return false;
}
char[] cs = _word.toCharArray();
TrieNode current = _root;
for(int i = 0; i< cs.length ; i++){
int offset = (cs[i]- 'a');
if(current.getChildren()[offset] == null){
return false;
}else {
current = current.getChildren()[offset];
}
}
return true;
}
}
相关测试为:
/**
* Created by kyle on 2016/6/23.
*/
public class Main {
public static void main(String[] args){
System.out.println("hello");
String[] words={"hello", "hi", "what", "how", "wow", "he", "heel"};
TrieTree trieTree = new TrieTree();
for(String word: words){
trieTree.insertIntoTree(word);
}
trieTree.printTree();
System.out.println(trieTree.searchWord("he"));
}
}
相关文章推荐
- 【最短系列】linux命令行中,各颜色意义
- LTE 下行调度过程
- Shiro的 rememberMe 功能使用指导(为什么rememberMe设置了没作用?)
- 运动目标检测--鬼影消除总结
- Android客户端与服务端交互之登陆示例
- $.inArray()使用方法
- js面向对象与继承
- 修改chrome下保存用户名密码的默认字体
- InstallShild LE 应用文件相对路径设置
- elasticsearch 集群的搭建
- PHP自动给URl添加http://前缀
- POJ-2386 Lake Counting
- android sqlite数据库简测
- 判断一棵树是否为满二叉树
- 运动目标检测--基于直方图匹配的鬼影消除
- 如何插入谷歌地图并获取javascript api 秘钥
- java 友好的显示时间
- mysql group_concat函数被截断的问题
- 容器间建立连接案例-mysql
- PostgreSQL错误代码速查