资讯专栏INFORMATION COLUMN

算法(第4版) Chapter 5.2 单词查找树

tigerZH / 1271人阅读

摘要:谢路云单词查找树查找所需要的单词的时间和键的长度成正比查找未命中只需检查若干个单词单词查找树单词查找树基本性质每个链接对应一个字符每个结点可能有一个值有值,说明存在从根结点到这个结点的字符串。它的存在是为了简化查询。

Algorithms Fourth Edition
Written By Robert Sedgewick & Kevin Wayne
Translated By 谢路云
Chapter 5 Section 2 单词查找树

查找所需要的单词的时间和键的长度成正比

查找未命中只需检查若干个单词

单词查找树 单词查找树API

基本性质

每个链接对应一个字符

每个结点可能有一个值

有值,说明存在从根结点到这个结点的字符串。

没有值,说明不存在从根结点到这个结点的字符串。没有对应的键值。它的存在是为了简化查询。

查找

命中: 对应结点有值(注意不单单是指向该对应结点d链接存在,并且该结点一定要有值!)

未命中: 没有值 or 链接为空

结点的表示

每个结点下面有R个链接,一个链接对应一个字符

键隐式地保存在结点里

TrieST 代码

</>复制代码

  1. public class TrieST {
  2. private static int R = 256; // radix
  3. private Node root; // root of trie
  4. private static class Node {
  5. private Object val;
  6. private Node[] next = new Node[R];
  7. }
  8. public Value get(String key) {
  9. Node x = get(root, key, 0);
  10. if (x == null)
  11. return null;
  12. return (Value) x.val;
  13. }
  14. // Return value associated with key in the subtrie rooted at x.
  15. private Node get(Node x, String key, int d) {
  16. if (x == null)
  17. return null;
  18. if (d == key.length())
  19. return x;
  20. char c = key.charAt(d); // Use dth key char to identify subtrie.
  21. return get(x.next[c], key, d + 1);
  22. }
  23. public void put(String key, Value val) {
  24. root = put(root, key, val, 0);
  25. }
  26. // Change value associated with key if in subtrie rooted at x.
  27. private Node put(Node x, String key, Value val, int d) { //神一般的递归思想
  28. if (x == null)
  29. x = new Node();
  30. if (d == key.length()) {
  31. x.val = val;
  32. return x;
  33. }
  34. char c = key.charAt(d); // Use dth key char to identify subtrie.
  35. x.next[c] = put(x.next[c], key, val, d + 1); //神来之笔
  36. return x;
  37. }
  38. public Iterable keys() {
  39. return keysWithPrefix("");
  40. }
  41. public Iterable keysWithPrefix(String pre) {
  42. Queue q = new Queue();
  43. collect(get(root, pre, 0), pre, q);
  44. return q;
  45. }
  46. private void collect(Node x, String pre, Queue q) {
  47. if (x == null)
  48. return;
  49. if (x.val != null)
  50. q.enqueue(pre);
  51. for (char c = 0; c < R; c++) //这个效率简直了。。。烂到爆炸
  52. collect(x.next[c], pre + c, q);
  53. }
  54. }

方法keys:返回一个Queue,里面有所有的字符串

方法keysWithPrefix(String pre):返回一个Queue,里面有所有以给定字符串pre开头的所有字符串

方法collect:递归用

通配符匹配

结构差异

不含通配符的结构。keysWithPrefix(); get(); collect();

含通配符的结构。keysWithPrefix(); collect();

为什么结构和之前不一样呢?因为get方法要重写。直接把重写的get方法归并进collect方法里去了。

</>复制代码

  1. public Iterable keysThatMatch(String pat) {
  2. Queue q = new Queue();
  3. collect(root, "", pat, q);
  4. return q;
  5. }
  6. public void collect(Node x, String pre, String pat, Queue q) {
  7. int d = pre.length();
  8. // begin修改于原get方法()和collect方法()
  9. if (x == null) // get&collect
  10. return;
  11. if (d == pat.length() && x.val != null) // 前collect,后get
  12. q.enqueue(pre);
  13. if (d == pat.length())// get
  14. return;
  15. // end 修改于原get方法()和collect方法()
  16. char next = pat.charAt(d);
  17. for (char c = 0; c < R; c++)
  18. if (next == "." || next == c) //如果next=="." 就要遍历接在当前字符后面的所有字符!!!
  19. collect(x.next[c], pre + c, pat, q); //还是神一般的递归想法
  20. }

</>复制代码

  1. private Node get(Node x, String key, int d) {
  2. if (x == null)
  3. return null;
  4. if (d == key.length())
  5. return x;
  6. char c = key.charAt(d); // Use dth key char to identify subtrie.
  7. return get(x.next[c], key, d + 1);
  8. }
最长前缀

</>复制代码

  1. public String longestPrefixOf(String s) {
  2. int length = search(root, s, 0, 0);
  3. return s.substring(0, length);
  4. }
  5. //当前搜索的是第d位字符,返回的是具有最长length位前缀的子字符
  6. private int search(Node x, String s, int d, int length) {
  7. if (x == null) // 查到单词树的尽头了
  8. return length;
  9. if (x.val != null) // 如果存在这个单词,就更新length值
  10. length = d;
  11. if (d == s.length()) // 查到字符串的尽头了(一定要先做上一步)
  12. return length;
  13. char c = s.charAt(d);
  14. return search(x.next[c], s, d + 1, length); //递归搜索下一位
  15. }
删除操作

依旧是神一般的递归思路

如果它的链接不为空

删去这个结点的即可

如果它的所有链接都为空

删去这个结点

检查这个结点的父结点的所有链接是否为空

不为空,结束

为空,删去父结点并检查父结点的父结点是否为空
……循环如此

</>复制代码

  1. public void delete(String key) {
  2. root = delete(root, key, 0);
  3. }
  4. private Node delete(Node x, String key, int d) {
  5. if (x == null)
  6. return null;
  7. if (d == key.length()) //找到了的话就将该结点的值删去
  8. x.val = null;
  9. else {
  10. char c = key.charAt(d);
  11. x.next[c] = delete(x.next[c], key, d + 1); //依旧是神一般的递归思路
  12. }
  13. if (x.val != null) //如果该结点有值,则不管当前结点链接是否为空,该结点都在树里,不能被删去。
  14. return x;
  15. for (char c = 0; c < R; c++) //否则就看该结点链接是否为空(即该结点没有值)
  16. if (x.next[c] != null) //如果当前结点链接不为空,则返回当前结点
  17. return x;
  18. return null; //否则返回为空。(因为是返回上一层递归, 即把自己置为空,也即删除了自己)
  19. }
复杂度

字母表大小为R,N个随机键组成的单词查找树中

时间:

查找和插入最差时间为:键的长度+1

查找未命中的平均时间为:logRN (查找未命中的时间和键的长度无关

空间

与 R和所有键的字符总数之积 成正比

链接

一棵单词查找树的链接总数为RN到RNw之间,w为键的平均长度

当所有键较短时,链接总数接近RN

当所有键较长时,链接总数接近RNw

缩小R能节省大量空间

三向单词查找树

避免空间消耗

每个结点含有一个字符,三个链接(小于,等于,大于),可能含有一个值

字符是显式保存在每个结点中

TST 代码

</>复制代码

  1. public class TST {
  2. private Node root; // root of trie
  3. private class Node {
  4. char c; // character
  5. Node left, mid, right; // left, middle, and right subtries
  6. Value val; // value associated with string
  7. }
  8. public Value get(String key) {
  9. Node x = get(root, key, 0);
  10. if (x == null)
  11. return null;
  12. return (Value) x.val;
  13. }
  14. private Node get(Node x, String key, int d) {
  15. if (x == null)
  16. return null;
  17. char c = key.charAt(d);
  18. if (c < x.c)
  19. return get(x.left, key, d);
  20. else if (c > x.c)
  21. return get(x.right, key, d);
  22. else if (d < key.length() - 1)
  23. return get(x.mid, key, d + 1);
  24. else
  25. return x;
  26. }
  27. public void put(String key, Value val) {
  28. root = put(root, key, val, 0);
  29. }
  30. private Node put(Node x, String key, Value val, int d) {
  31. char c = key.charAt(d);
  32. if (x == null) {
  33. x = new Node();
  34. x.c = c;
  35. }
  36. if (c < x.c)
  37. x.left = put(x.left, key, val, d);
  38. else if (c > x.c)
  39. x.right = put(x.right, key, val, d);
  40. else if (d < key.length() - 1)
  41. x.mid = put(x.mid, key, val, d + 1);
  42. else
  43. x.val = val;
  44. return x;
  45. }
  46. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66546.html

相关文章

  • 算法4Chapter 4.3 最小生成

    摘要:算法图示代码复杂度时间初始化优先队列,最坏情况次比较每次操作成本次比较,最多还会多次和次操作,但这些成本相比的增长数量级可忽略不计详见空间 Algorithms Fourth EditionWritten By Robert Sedgewick & Kevin WayneTranslated By 谢路云Chapter 4 Section 3 最小生成树 定义 树是特殊的图 图的生...

    asoren 评论0 收藏0
  • 算法4Chapter 4.2 强联通性 Tarjan算法补充

    摘要:在实际的测试中,算法的运行效率也比算法高左右。此外,该算法与求无向图的双连通分量割点桥的算法也有着很深的联系。学习该算法,也有助于深入理解求双连通分量的算法,两者可以类比组合理解。固属于同一连通分支。 参考资料http://blog.csdn.net/acmmmm/a...https://www.byvoid.com/blog/s...http://blog.csdn.net/noth...

    maybe_009 评论0 收藏0
  • 算法4Chapter 1

    摘要:由的位置决定乘以几,依次为,,,,,。递归算法递归算法就是本次结果用另外的参数调用自己。然而这个函数方法本身并没有用,因为方法中若传递参数为基本型如,在方法中对其值的改变并不会在主函数中产生影响。 Algorithms Fourth EditionWritten By Robert Sedgewick & Kevin WayneTranslated By 谢路云 笔记 二分查找 Bin...

    Jacendfeng 评论0 收藏0
  • 算法4Chapter 4.4 最短路径

    摘要:相关操作就是判断的不等号符号改反,初始值设为负无穷副本的最短路径即为原图的最长路径。方法是同上面一样构造图,同时会添加负权重边,再将所有边取反,然后求最短路径最短路径存在则可行没有负权重环就是可行的调度。 Algorithms Fourth EditionWritten By Robert Sedgewick & Kevin WayneTranslated By 谢路云Chapter ...

    leap_frog 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<