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; } ```
  • 相关阅读:
    算法视频库下载常用网址(转载)
    Python study 1
    $X-Real-Ip和$X-Forwarded-For的区别
    python装饰器
    python迭代器和生成器
    python函数动态参数详解
    python常用模块
    python 正则re模块
    pycharm5新版注册
    老男孩python自动化运维作业2
  • 原文地址:https://www.cnblogs.com/orangee/p/8912971.html
Copyright © 2011-2022 走看看