首页 > 代码库 > tire树
tire树
#pragma once #include <string>using namespace std;#define MAX_CHAR 26struct node { bool isWord; node* next[MAX_CHAR]; node() { isWord = false; for(int i = 0;i<MAX_CHAR;i++) next[i] = NULL; }};class Tire{public: Tire(){root = NULL;} void insert(string& str) { if(!root) root = new node; node* location = root; for(int i = 0;i< (int)str.length();i++) { int num = str[i] - ‘a‘; if(!location->next[num]) { location->next[num] = new node; } location = location->next[num]; } location->isWord = true; } bool search(string& str) { if(!root) return false; node* location = root; for (int i = 0;i<(int)str.length();i++) { int num = str[i] - ‘a‘; if(!location->next[num]) return false; location = location->next[num]; } return location->isWord; }public: node* root;};
tire树
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。