此次代码使用了诸多新鲜玩意,比如自动类型推断,模板类等等,感觉真的超级好玩啊。

自己做了一个简易的测试,程序的健壮性什么的还是比较差的,此程序留待以后进行功能补全。

#pragma once
#include<cstddef>
#include<string>
using std::strlen;
using std::pair;
using std::make_pair; //字典树节点
template<int size>
class TrieNode
{
public:
TrieNode() :NodeSize(), TerminableSize()
{
for (size_t i = ; i < size; i++)
{
ChildNode[i] = NULL;
}
}
~TrieNode()
{
for (size_t i = ; i < size; i++)
{
delete ChildNode[i];
ChildNode[i] = NULL;
}
} public: //为了方便,此处将变量暴露,后续可以改进为通过接口操作
int NodeSize; //记录此节点子节点的个数
int TerminableSize; //存储以此节点结尾的字符串个数
TrieNode* ChildNode[size]; //记录指向子节点的指针
}; //字典树
template < int Size, typename Type> class Trie
{
public:
typedef TrieNode<Size> Node;
typedef TrieNode<Size>* pNode; public:
Trie() :root(new Node) {}
~Trie() { delete root; } public:
template<typename itr>
void insert(itr beg, itr end);
void insert(const char* str); template<typename itr>
pair<bool,int> find(itr beg, itr end);
pair<bool,int> find(const char* str); template<typename itr>
bool DownNodeAlone(itr beg); template<typename itr>
bool erase(itr beg, itr end);
bool erase(const char* str); int sizeAll(pNode);
int sizeNodeRedundant(pNode); public:
pNode root;
private:
Type index;
}; template < int Size, typename Type>
template<typename itr>
void Trie<Size, Type>::insert(itr beg, itr end)
{
pNode cur = root, pre = NULL; for (; beg != end; ++beg)
{
if (!cur->ChildNode[index[*beg]])
{
cur->ChildNode[index[*beg]] = new(Node);
++cur->NodeSize;
}
pre = cur;
cur = cur->ChildNode[index[*beg]];
}
if (pre)
++pre->TerminableSize;
} template < int Size, typename Type>
void Trie<Size, Type>::insert(const char* str)
{
return insert(str, str + strlen(str));
} template <int Size, typename Type>
pair<bool, int> Trie<Size, Type>::find(const char* str)
{
return find(str, str + strlen(str));
} template <int Size, typename Type>
template<typename itr>
pair<bool,int> Trie<Size, Type>::find(itr beg, itr end)
{
pNode cur = root, pre = NULL;
pair<bool, int> res(false, );
for (;beg != end;++beg)
{
if (!cur->ChildNode[index[*beg]])
{
return res;
}
pre = cur;
cur = cur->ChildNode[index[*beg]];
}
if (pre != NULL&&pre->TerminableSize > )
{
res.first = true;
res.second = pre->TerminableSize;
}
return res;
} template <int Size, typename Type>
template<typename itr>
bool Trie<Size, Type>::DownNodeAlone(itr beg)
{
pNode cur = root;
int terminableSum = ; while (cur->NodeSize != )
{
terminableSum += cur->TerminableSize;
if (cur->NodeSize > )
return false;
else
{
for (size_t i = ; i < Size; i++)
{
if (cur->ChildNode[i])
{
cur = cur->ChildNode[i];
}
}
}
}
if (terminableSum == )
return true;
else return false;
} template <int Size, typename Type>
template<typename itr>
bool Trie<Size, Type>::erase(itr beg, itr end)
{
auto var = find(beg, end);
if (var.first)
{
pNode cur = root, pre = NULL;
for (; beg != end; ++beg)
{
if (DownNodeAlone(cur))
{
delete cur;
cur = NULL;
return true;
}
pre = cur;
cur = cur->ChildNode[index[*beg]];
}
if (pre->TerminableSize > )
--pre->TerminableSize;
return true;
}
return false;
} template <int Size, typename Type>
bool Trie<Size, Type>::erase(const char* str)
{
auto var = find(str);
if (var.first)
{
erase(str, str + strlen(str));
return true;
}
return false;
} template <int Size, typename Type>
int Trie<Size, Type>::sizeAll(pNode ptr)
{
if (ptr == NULL)
return ;
int rev = ptr->TerminableSize;
for (size_t i = ; i < Size; i++)
{
rev += sizeAll(ptr->ChildNode[i]);
}
return rev;
} template <int Size, typename Type>
int Trie<Size, Type>::sizeNodeRedundant(pNode ptr)
{
if (NULL == ptr)
return ;
int i, rev = ;
if (ptr->TerminableSize > )
rev = ;
if (ptr->NodeSize != )
{
for (i = ; i < Size; ++i)
{
rev += sizeNodeRedundant(ptr->ChildNode[i]);
}
}
return rev;
} template<int Size>
class Index
{
public:
Index() {}
~Index() {} public:
int operator[](char vchar)
{
return (vchar - 'a') % Size;
}
};

测试:

int main(void)
{
{
Trie<, Index<>> temp;
temp.insert("hello");
temp.insert("he");
temp.insert("he");
temp.insert("her");
temp.insert("him");
temp.insert("fuck");
temp.insert("fuckyou");
temp.insert("hupu");
temp.insert("lady");
temp.insert("hahah");
temp.insert("lady");
temp.insert("lady");
temp.insert("lady"); auto var = temp.find("lady");
cout << "lady:" << boolalpha <<var.first <<" "<<var.second<< endl;
var = temp.find("heihei");
cout << "heihei:" << boolalpha << var.first << " " << var.second << endl; cout << "size:" << temp.sizeAll(temp.root) << endl;
cout << "size of NoneRedundant:" << temp.sizeNodeRedundant(temp.root) << endl; var = temp.find("hupu");
cout << "hupu:" << boolalpha << var.first <<" "<<var.second<< endl;
temp.erase("hupu");
var = temp.find("hupu");
cout << "hupu:" << boolalpha << var.first << " " << var.second << endl; cout << "size:" << temp.sizeAll(temp.root) << endl;
cout << "size of NoneRedundant:" << temp.sizeNodeRedundant(temp.root) << endl;
} cin.get();
_CrtDumpMemoryLeaks();
return ;
}

字典树的C++实现的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  10. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

随机推荐

  1. PAT (Advanced Level) Practise:1027. Colors in Mars

    [题目链接] People in Mars represent the colors in their computers in a similar way as the Earth people. ...

  2. C++继承,多重继承,虚继承的构造函数以及析构函数的调用顺序问题

    #include <iostream> using namespace std; class A{ int data_a; public: A(){ data_a = ; cout < ...

  3. UITabBarButton 点击失效问题

    开发过程: 在创建一个UIWindow时,直接在window上添加手势动作. 开发代码: UITapGestureRecognizer *tapRecognizer=[[UITapGestureRec ...

  4. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  5. select for update行锁

     select for update行锁 2008-05-26 15:15:37 分类: Oracle Select-For Update语句的语法与select语句相同,只是在select语句的后面 ...

  6. 基于XML的AOP配置-转

    http://www.cnblogs.com/yangy608/archive/2010/11/14/1876839.html AOP(Aspect-Oriented Programming,面向切面 ...

  7. Asp.net MVC 的八个扩展点

    http://www.cnblogs.com/richieyang/p/5180939.html MVC模型以低耦合.可重用.可维护性高等众多优点已逐渐代替了WebForm模型.能够灵活使用MVC提供 ...

  8. Mac系统终端命令行不执行命令 总出现command not found解决方法

    配置过安卓开发环境,改过bash_profile这个文件,最后不知怎么的只有cd命令能执行,我猜测可能修改bash_profile文件后没有保存 导致的     保存命令是:  source .bas ...

  9. mock测试到底是什么?

    ​    ​经常听人说mock测试,究竟什么是mock测试呢?mock测试能解决什么问题?mock测试要如何做呢?今天为大家做简单介绍,之后会有详细的mock测试,感谢大家对测试梦工厂的持续关注. 概 ...

  10. 【原创】新手用外挂来学C语言,外挂入门教程【2013.03.12更新V5.1版

    目录 e@vZg8Ie  第一章 配置编译环境    - 5 - W7~_XI  1.1 安装CB和gcc    - 6 - fj( WH L  1.2 使用gcc编写我们的第一个C程序    - 1 ...