[搜索]Trie树的实现

trie这种树也被称为线索,搜索树。

正如图

以下是用stl 的map来实现

class trie_item_c
{
public:
	trie_item_c(){}
	trie_item_c(const char nm)
	{
		name = nm;
	}

	void set_name(const char nm)
	{
		name = nm;
	}

	trie_item_c * get_child(const char nm)
	{
		map<const char ,trie_item_c*>::const_iterator it = children.find(nm);
		if(it != children.end())
			return it->second;
		else
		{
			trie_item_c *cld = new trie_item_c(nm);
			children.insert(pair <const char ,trie_item_c*>(nm,cld));
			return cld;
		}
	}
	bool find(const char* dic)
	{
		if(!dic || *dic == '\0')
		{
			if(children.end() != children.find('\0'))
				return true;
			return false;
		}
		const char* temp = dic;
		map<const char ,trie_item_c*>::const_iterator it = children.find(*temp);
		if(it == children.end())
			return false;
		return it->second->find(++temp);
	}
	void print()
	{
		printf("%c",name);
		map<const char ,trie_item_c*>::const_iterator it = children.begin();
		for(;it != children.end();it++)
		{
			it->second->print();
		}
		printf("\n");
	}
	virtual ~trie_item_c()
	{
		map<const char ,trie_item_c*>::const_iterator it = children.begin();
		while(it != children.end())
		{
			delete it->second;
			children.erase(it);
			it = children.begin();
		}
	}
private:
	map<const char ,trie_item_c*> children;
	char name;

};

以下代码是构建树的过程

for(const char* dic = lst.first_string();dic;dic = lst.next_string())
	{

		trie_item_c *temp = root;
		for(int i=0;i<str_temp.str_len();i++)
		{
			char c = str_temp.get(i);
			trie_item_c *item = temp->get_child(c);
			temp = item;
		}
		//add one null child
		temp->get_child('\0');
	}

推断是否一个字root在,只需要调用root->find("book");您可以。



时间: 2024-08-26 05:52:12

[搜索]Trie树的实现的相关文章

[搜索]Trie树的一种实现

trie树也叫字典树,搜索树等. 如图所示 下面是用stl 的map来实现 class trie_item_c { public: trie_item_c(){} trie_item_c(const char nm) { name = nm; } void set_name(const char nm) { name = nm; } trie_item_c * get_child(const char nm) { map<const char ,trie_item_c*>::const_ite

【数据结构】Trie树

1.Trie树简介 Trie树,又称字典树.前缀树,被用于信息检索(information retrieval)的数据结构.Trie一词便来自于单词retrieval.基本思想:用字符串的公共前缀降低查询时间.比如,在最优的查询二叉树中查询关键字的时间复杂度为M * log N,M是字符串最大长度,N为字符串数量:而用Trie树时,只需O(M)时间. [1] 中给出一个简单Trie树例子,蓝色表示一个单词结尾:该Trie树存储的单词为the, their, there, a, any, answ

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

[POJ 1204]Word Puzzles(Trie树暴搜&amp;amp;AC自己主动机)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

Trie 树——搜索关键词提示

当你在搜索引擎中输入想要搜索的一部分内容时,搜索引擎就会自动弹出下拉框,里面是各种关键词提示,这个功能是怎么实现的呢?其实底层最基本的就是 Trie 树这种数据结构. 1. 什么是 "Trie" 树 Trie 树也叫 "字典树".顾名思义,它是一个树形结构,专门用来处理在一组字符串集合中快速查找某个字符串的问题. 假设我们有 6 个字符串,它们分别是:how,hi,her,hello,so,see.我们希望在这里面多次查找某个字符串是否存在,如果每次都拿要查找的字符

跳跃表,字典树(单词查找树,Trie树),后缀树,KMP算法,AC 自动机相关算法原理详细汇总

第一部分:跳跃表 本文将总结一种数据结构:跳跃表.前半部分跳跃表性质和操作的介绍直接摘自<让算法的效率跳起来--浅谈"跳跃表"的相关操作及其应用>上海市华东师范大学第二附属中学 魏冉.之后将附上跳跃表的源代码,以及本人对其的了解.难免有错误之处,希望指正,共同进步.谢谢. 跳跃表(Skip List)是1987年才诞生的一种崭新的数据结构,它在进行查找.插入.删除等操作时的期望时间复杂度均为O(logn),有着近乎替代平衡树的本领.而且最重要的一点,就是它的编程复杂度较同类

剑指Offer——Trie树(字典树)

剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高. Trie的核心思想是空间换时间.利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的. Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子右兄弟的方法建树的话,可能会好点.可见,优

【转】B树、B-树、B+树、B*树、红黑树、 二叉排序树、trie树Double Array 字典查找树简介

B  树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B树的搜索,从根结点开始,如果查询的关键字与结点的关键字相等,那么就命中:否则,如果查询关键字比结点关键字小,就进入左儿子:如果比结点关键字大,就进入右儿子:如果左儿子或右儿子的指针为空,则报告找不到相应的关键字: 如果B树的所有非叶子结点的左右子树的结点数目均保持差不多(平衡),那么B树的搜索性

AVL树,红黑树,B-B+树,Trie树原理和应用

前言:本文章来源于我在知乎上回答的一个问题 AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中? 看完后您可能会了解到这些数据结构大致的原理及为什么用在这些场景,文章并不涉及具体操作(如插入删除等等) 目录 AVL树 AVL树原理与应用 红黑树 红黑树原理与应用 B/B+树 B/B+树原理与应用 Trie树 Trie树原理与应用 AVL树 简介: AVL树是最早的自平衡二叉树,在早期应用还相对来说比较广,后期由于旋转次数过多而被红黑树等结构取代(二者都是用来搜索的),AVL树内