zoukankan      html  css  js  c++  java
  • Trie树

    字典树Trie

    Trie树,又称为前缀树(Prefix Tree)、单词查找树或键树,是一种多叉树结构。

    性质

    ①根节点不包含字符,除根节点外的每一个子节点都包含一个字符。 ②从根节点到某一个节点,路径上经过的字符连接起来,为该节点对应的字符串。 ③每个节点的所有子节点包含的字符互不相同。 PS:通常在实现的时候,会在结点结构中设置一个标志,用来标记该节点处是否构成一个单词 ###操作 以下用链表来实现,也可以用数组来实现。给出的是基本的Trie,未经改进。 插入: ```C++ const int N = 26; //字符串中包含字母种类数决定 struct trie { trie* next[N]; int count; }; typedef trie* link; link create() { link p = new trie; p->count = 0; for (int i = 0; i < N; ++i) p->next[i] = NULL; return p; } void insert(char* s, link root) { char* p = s; link node = root; while (*p) { if (node->next[*p - 'a']==NULL) node->next[*p - 'a'] = create(); node = node->next[*p - 'a']; ++p; } node->count++; return; } ``` 查询(返回查询串出现的次数): ```C++ int query(link root, char* s) { link node = root; char* p = s; while(*p && node!=NULL) { node = node->next[*p-'a']; ++p; } if(node == NULL) return 0; else return node->count; } ```
  • 相关阅读:
    跨域踩坑经验总结(内涵:跨域知识科普)
    Nginx location规则匹配
    CentOS 命令
    Centos 修改源
    Ubuntu下获取内核源码
    Ubuntu用户自定义脚本开机启动
    ubuntu 14.04安装x11VNC
    texi格式文件的读取
    更换主机后SSH无法登录的问题
    ubuntu操作系统的目录结构
  • 原文地址:https://www.cnblogs.com/orangee/p/8912971.html
Copyright © 2011-2022 走看看