前缀树:

  假设一个字符串数组,“abcd”,"bcd","gef" , 构建一颗树,字母是在路径上,节点上最基本的存储的信息包括:

    以这个节点结尾的 字符串的数量;

    经过这个节点的字符串的数量;

一个字符串类型的数组arr1,另一个字符串类型的数组arr2;

(1)arr2中有哪些字符,是arr1中出现的?请打印

(2)arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?请 打印

(3)arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?请打印 arr2中出现次数最大的前缀

 package my_basic.class_7;

 public class Code_00_TrieTree {
//前缀树
public static class TrieNode{
public int end;
public int path;
public TrieNode[] nexts; public TrieNode() {
end = 0;
path = 0;
nexts = new TrieNode[26]; //之前没写 空指针
} } public static class Trie{
private TrieNode root;
public Trie() {
root = new TrieNode();
} public void insert(String word) {
if (word == null) {
return;
}
char[] chs = word.toCharArray();
int index = 0;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
node.nexts[index] = new TrieNode(); }
node = node.nexts[index];
node.path++;
}
node.end++;
} public int search(String word) {
if (word == null) {
return 0;
}
char[] chs = word.toCharArray();
int index = 0;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.end;
} public void delete(String word) {
if (word == null) {
return;
}
char[] chs = word.toCharArray();
int index = 0;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (--node.nexts[index].path == 0) {
node.nexts[index] = null; //后面的节点直接删去
return;
}
node = node.nexts[index];
}
node.end--;
} public int prefixNumber(String word) {
if (word == null) {
return 0;
}
char[] chs = word.toCharArray();
int index = 1;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.path;
}
} public static void main(String[] args) {
String[] arr1 = {"abc","bcd","def","bcf"};
String[] arr2 = {"b","a","bc"};
Trie trie = new Trie();
for (int i = 0; i < arr1.length; i++) {
trie.insert(arr1[i]);
}
//arr2中有哪些字符,是arr1中出现的
for (int i = 0; i < arr2.length; i++) {
if (trie.search(arr2[i]) != 0) {
System.out.print(arr2[i] + " ");
}
}
System.out.println(" ");
System.out.println("===================================");
//arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?
for (int i = 0; i < arr2.length; i++) {
if (trie.prefixNumber(arr2[i]) != 0) {
System.out.print(arr2[i] + " ");
}
}
System.out.println(" ");
System.out.println("==================================="); String res = null;
int pre = 0;
for (int i = 0; i < arr2.length; i++) {
int prefixNum = trie.prefixNumber(arr2[i]);
if (prefixNum != 0) {
System.out.print(arr2[i] + " ");
if(prefixNum >= pre) {
res = arr2[i];
pre = prefixNum;
}
}
}
System.out.println();
System.out.println(res); // Trie trie = new Trie();
// System.out.println(trie.search("zuo"));
// trie.insert("zuo");
// System.out.println(trie.search("zuo"));
// trie.delete("zuo");
// System.out.println(trie.search("zuo"));
// trie.insert("zuo");
// trie.insert("zuo");
// trie.delete("zuo");
// System.out.println(trie.search("zuo"));
// trie.delete("zuo");
// System.out.println(trie.search("zuo"));
// trie.insert("zuoa");
// trie.insert("zuoac");
// trie.insert("zuoab");
// trie.insert("zuoad");
// trie.delete("zuoa");
// System.out.println(trie.search("zuoa"));
// System.out.println(trie.prefixNumber("zuo")); }
}

  

最新文章

  1. C#----对时间结构DateTime的使用(时间日期的使用)
  2. 用Access作为后台数据库支撑。
  3. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition)
  4. iOS NSNotification的使用
  5. Using GET_GROUP_SELECTION For Record Groups in Oracle Forms
  6. java异常处理的两种方法
  7. [51NOD1127]最短的包含字符串(尺取法)
  8. CPU卡及NFC供应商
  9. spring的主要特性
  10. Android 动画之RotateAnimation应用详解
  11. 转:基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】
  12. httrack,webdup,WinHTTrack,WebZip
  13. printdir-deldir-bmp
  14. POJ 2253 Frogger floyd算法
  15. 【CentOS】阿里云ECS申请CA证书配置SSL
  16. 为什么PPIO要设计支付代理节点?
  17. [Linux]Ubuntu 16.04 远程桌面
  18. 关于Socket.IO的知识点记录
  19. 当一个HTML元素需要添加mouseon、mouseout与click事件,或者mouserenter、mouseleave和click事件时,click事件无法触发
  20. Codeforce 733B - Parade (枚举)

热门文章

  1. List Control控件中及时捕获checkbox被选中的消息的解决方案
  2. python数值类型与序列类型
  3. 2017&quot;百度之星&quot;程序设计大赛 - 初赛(A)小C的倍数问题
  4. [NWPU2016][寒假作业][正常版第三组]R&amp;&amp;HDU1240
  5. (转)网站DDOS攻击防护实战老男孩经验心得分享
  6. js获取ISO8601规范时间
  7. Redis的数据类型(lists、Sets)
  8. AJPFX关于JDK,JRE,JVM的区别与联系
  9. 定时器 &amp; 日期时间对象 &amp; 正则
  10. 解释器模式和php实现