当前位置: 首页 > news >正文

数据结构笔记--前缀树的实现

1--前缀树的实现

        前缀树的每一个节点拥有三个成员变量,pass表示有多少个字符串经过该节点,end表示有多少个字符串以该节点结尾,nexts表示该字符串可以走向哪些节点;

#include <iostream>
#include <unordered_map>struct TreeNode{TreeNode() : pass(0), end(0){}int pass; // 经过次数int end; // 是多少个字符串的结尾std::unordered_map<char, TreeNode*> nexts;
};class Trie{
public:// 构造函数Trie(){root = new TreeNode();}void insert(std::string word){if(word.length() == 0) return;TreeNode *node = root;node->pass++;for(int i = 0; i < word.length(); i++){if(node->nexts[word[i]] == NULL){ // 哈希表中没有该字符node->nexts[word[i]] = new TreeNode(); // 新建该字符}node = node->nexts[word[i]];node->pass++; // 该字符节点经过的次数++}node->end++; // 遍历word末尾时,节点的end++,表明以该节点结尾的字符串数++}bool search(std::string word){if(word.length() == 0) return true;TreeNode *cur = root;for(int i = 0; i < word.length(); i++){if(cur->nexts[word[i]] == NULL) return 0; // 没有该字符节点cur = cur->nexts[word[i]];}return cur->end; // end不为0表明该字符串出现过}bool startWith(std::string prefix){if(prefix.length() == 0) return 0;TreeNode *cur = root;for(int i = 0; i < prefix.length(); i++){if(cur->nexts[prefix[i]] == NULL) return 0; // 前缀没出现过cur = cur->nexts[prefix[i]];}return cur->pass; // 有多少个字符串经过该前缀,0个表明false;}private:TreeNode *root;
};int main(int argc, char *argv[]){Trie T1;std::string test1 = "hello";T1.insert(test1);bool res1 = T1.search(test1);if(res1) std::cout << "true" << std::endl;else std::cout << "false" << std::endl;bool res2 = T1.startWith("hel");if(res2) std::cout << "true" << std::endl;else std::cout << "false" << std::endl;return 0;
}

2--LeetCode真题

2-1--实现Trie(前缀树)

         本题不能自定义节点,因此将 pass、end 和 nexts 等成员变量转换成类的成员变量,新节点就是类的对象;

class Trie{
public:// 构造函数Trie(){}void insert(std::string word){if(word.length() == 0) return;Trie *node = this;node->pass++;for(int i = 0; i < word.length(); i++){if(node->nexts[word[i]] == NULL){ // 哈希表中没有该字符node->nexts[word[i]] = new Trie(); // 新建该字符}node = node->nexts[word[i]];node->pass++; // 该字符节点经过的次数++}node->end++; // 遍历word末尾时,节点的end++,表明以该节点结尾的字符串数++}bool search(std::string word){if(word.length() == 0) return true;Trie *cur = this;for(int i = 0; i < word.length(); i++){if(cur->nexts[word[i]] == NULL) return 0; // 没有该字符节点cur = cur->nexts[word[i]];}return cur->end; // end不为0表明该字符串出现过}bool startsWith(std::string prefix){if(prefix.length() == 0) return 0;Trie *cur = this;for(int i = 0; i < prefix.length(); i++){if(cur->nexts[prefix[i]] == NULL) return 0; // 前缀没出现过cur = cur->nexts[prefix[i]];}return cur->pass; // 有多少个字符串经过该前缀,0个表明false;}private:int pass = 0; // 经过次数int end = 0; // 是多少个字符串的结尾std::unordered_map<char, Trie*> nexts;
};

http://www.lryc.cn/news/123210.html

相关文章:

  • C/C++时间获取函数
  • sql中判断日期是否是同一天
  • NAS搭建指南一——服务器的选择与搭建
  • 豪越HYDO智能运维助力智慧医院信息化建设
  • Week1题目重刷
  • 考研数据结构:第七章 查找
  • 【Linux进程篇】环境变量
  • 【软件测试】Linux环境下Docker搭建+Docker搭建MySQL服务(详细)
  • 去了字节跳动,才知道年薪40W的测试有这么多?
  • linux0.95(VFS重点)源码通俗解读(施工中)
  • mac ssh连接另一台window虚拟机vm
  • 使用Python解析通达信本地lday数据结构
  • 【Mysql】修改definer
  • 图片预览插件vue-photo-preview的使用
  • 最强自动化测试框架Playwright(20)- iframe
  • leetcode 516. 最长回文子序列(JAVA)题解
  • 在Java中操作Redis(详细-->从环境配置到代码实现)
  • 分布式作业调度框架——ElasticJob
  • react如何实现数据渲染
  • 在Java中如何使用List集合实现分页,以及模糊查询后分页
  • 【JAVA】包装类、正则表达式、Arrays类、Lambda表达式
  • Java中的Maven Assembly插件是什么?
  • SpringBoot禁用Swagger3
  • 小红书Java后端2023-8-6笔试
  • metaRTC7 demo mac/ios编译指南
  • systemd-journal 占用内存的问题
  • Java # Spring(2)
  • 2021年03月 C/C++(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • 应用程序运行报错:First section must be [net] or [network]:No such file or directory
  • 【ECMAScript】ES6-ES11学习笔记