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; } ```
  • 相关阅读:
    集合Hashtable Dictionary Hashset
    ArrayList 排序Sort()方法扩展
    Visual Studio 2019使用Intel C++ Compiler
    Visual Studio工具 vcpkg简介
    PE结构学习
    netapi32的一些利用方式
    windows api和创建服务
    导出firfox保存的密码
    在Active Directory中滥用无约束Kerberos委派
    Service Principal Name (SPN)
  • 原文地址:https://www.cnblogs.com/orangee/p/8912971.html
Copyright © 2011-2022 走看看