字典树java版本
来源:互联网 发布:韩顺平php入门到精通 编辑:程序博客网 时间:2024/05/19 18:39
/*
* 字典树的基本数据结构
* 字典树的性质:
* 1:根节点无数据
* 2:除了根节点,每个节点都只有一个元素
* 3:从根节点到叶节点是一个完成的单词,每一个节点的子节点没有重复的
* */
public class TrieNode {
private int num;//当前节点出现的次数
private TrieNode[] son;
private boolean ieEnd;
private boolean visited;
private char val;
public TrieNode(char element){
this.num=1;
this.son=new TrieNode[26];
this.ieEnd=false;
this.visited=false;
this.val=element;
}
public TrieNode(){
this.num=1;
this.son=new TrieNode[26];
this.ieEnd=false;
this.visited=false;
}
public TrieNode(int num, TrieNode[] son, boolean ieEnd, boolean visited,
char val) {
super();
this.num = num;
this.son = son;
this.ieEnd = ieEnd;
this.visited = visited;
this.val = val;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public TrieNode[] getSon() {
return son;
}
public void setSon(TrieNode[] son) {
this.son = son;
}
public boolean isIeEnd() {
return ieEnd;
}
public void setIeEnd(boolean ieEnd) {
this.ieEnd = ieEnd;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public char getVal() {
return val;
}
public void setVal(char val) {
this.val = val;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + val;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TrieNode other = (TrieNode) obj;
if (val != other.val)
return false;
return true;
}
}
package trietree;
import java.util.LinkedList;
/*
* yy
* 1:字典树的插入
* 2:查找
* 3:统计前缀
* 4:遍历字典树
* */
public class TrieTree {
private TrieNode root;
public TrieNode getRoot() {
return root;
}
public void setRoot(TrieNode root) {
this.root = root;
}
public TrieTree(TrieNode root) {
super();
this.root = root;
}
public TrieTree() {
this.root=new TrieNode();
}
//向字典树插入一个字符串
public void insert(String str){
if(str==null||str.equalsIgnoreCase(""))return ;
TrieNode node=this.root;
char[] letters=str.toCharArray();
int len=str.length();
for(int i=0;i<len;i++){
int pos=letters[i]-'a';
if(node.getSon()[pos]==null){
node.getSon()[pos]=new TrieNode(letters[i]);
}else{
node.getSon()[pos].setNum(node.getSon()[pos].getNum()+1);
}
node=node.getSon()[pos];
}
node.setIeEnd(true);
}
//计算给定当前前缀有多少字符串
public int countPrefix(String prefix){
if(prefix==null || "".equalsIgnoreCase(prefix))return -1;
TrieNode node=this.root;
char[] letters=prefix.toCharArray();
int len=prefix.length();
for(int i=0;i<len;i++){
int pos=letters[i]-'a';
if(node.getSon()[pos]==null){
return 0;
}else{
node=node.getSon()[pos];
}
}
return node.getNum();
}
//查看当前的字典树是否包含某个单词
public boolean has(String str){
if(str==null||"".equalsIgnoreCase(str))return false;
TrieNode node=this.root;
char[] letters=str.toCharArray();
int len=str.length();
for(int i=0;i<len;i++){
int pos=letters[i]-'a';
if(node.getSon()[pos]==null){
return false;
}else{
node=node.getSon()[pos];
}
}
return node.isIeEnd();
}
//遍历当前的字典树
public void printALlWords(){
TrieNode rootnode=this.root;
if(rootnode==null)return ;
LinkedList<TrieNode> list=new LinkedList<TrieNode>();
for(int i=0;i<26;i++){
TrieNode node=rootnode.getSon()[i];
if(node!=null){
list.addLast(node);
while(!list.isEmpty()){
TrieNode current=list.getLast();
TrieNode firstChild=firstOfChild(current);
while(firstChild!=null){
list.addLast(firstChild);
firstChild=firstOfChild(firstChild);
}
TrieNode last=list.getLast();
if(last.isIeEnd()==true){
this.printList(list);
}
list.removeLast();
}
}
list.clear();
}
}
//返回当前节点的第一个孩子
private TrieNode firstOfChild(TrieNode current) {
if(current==null)return null;
for(int i=0;i<26;i++){
if(current.getSon()[i]!=null&¤t.getSon()[i].isVisited()==false){
current.getSon()[i].setVisited(true);
return current.getSon()[i];
}
}
return null;
}
private void printList(List<TrieNode> list){
if(list==null||list.size()==0)return ;
for(TrieNode node:list){
System.out.print(node.getVal());
}
System.out.println();
}
public static void main(String[] args) {
TrieTree tree=new TrieTree();
String[] strs={"bee","banana", "band","absolute", "acm"}; //
String[] prefix={
"ba",
"b",
"band",
"abc"
};
for(String s:strs){
tree.insert(s);
}
for(String pre:prefix){
System.out.println(pre+":"+tree.countPrefix(pre));
}
System.out.println(tree.has("banana"));
System.out.println(tree.has("ba"));
tree.printALlWords();
}
}
- 字典树java版本
- java 版字典树
- java 版字典树
- Java实现字典树
- 字典树(java)
- 字典树Java模板
- 字典树--java 定义
- 版本1.0字典类型
- 版本1.0字典条目
- 版本2.0字典类型
- 版本2.0字典条目
- java版 字典树
- java字典树 实现统计
- 字典树模板(java)
- Java实现字典树TrieTree
- 字典树与三向字典树-java实现
- 版本判断 字典转字符串
- trie树(字典树)java实现
- MySql大文件插入错误
- 《王保明老师----Linux开发学习笔记》------讲08:进程 -----多进程下的文件描述符
- 不同内核浏览器的差异以及浏览器渲染简介
- 每天一个linux命令(1):wget命令
- Linux目录规范和含义(整理)
- 字典树java版本
- [Cocoa]_[画直线、矩形、文字]
- systemctl命令用法
- 通过修改配置文件,手动创建一个Linux可登陆账户
- Centos7安装mysql缺乏yum源怎么安装
- IOS 在开发中使用KVO观测属性变化
- easyui的数据表格(datagrid)使用时formatter的技巧
- 一道有趣题目
- 《计算机是怎样跑起来的》读书笔记