Trie(前缀树)


Trie(前缀树)

前缀树是指:

  1. 单个字符串中,字符从前到后加到一棵多叉树上。
  2. 字符放在路上,字节点上有专属的数据项(常见的是pass值和end值,pass表示有多少路径经过该节点,end表示有多少路径以该节点结尾)
  3. 所有样本都这样添加,如果没有路就新建,如果有路就复用
  4. 沿途节点的pass值增加1,每个字符串结束时来到的节点end值增加1
    在这里插入图片描述

代码实现(vector和map)

  • 使用vector:
    vector适合字符串中字符单一的情况,比如字符全是小写字母等:
class TrieNode
{
public:
	TrieNode()
	{
		pass = 0;
		end = 0;
		//假设字符都为小写字母
		nexts.resize(26,nullptr);
	}
	void insert(string word)
	{
		if (word == "")return;
		TrieNode* node = this;
		node->pass++;
		
		int path = 0;
		for (int i = 0; i < word.size(); i++)
		{
			path = word[i] - 'a';
			if (node->nexts[path] == nullptr)
			{
				node->nexts[path] = new TrieNode();
			}
			node = node->nexts[path];
			node->pass++;
		}
		node->end++;
	}
	//所有加入的字符串中,有几个是以pre这个字符串作为前缀的
	int prefixNumber(string pre)
	{
		if (pre == "") return 0;
		TrieNode* node = this;
		int path = 0;
		for (int i = 0; i < pre.size(); i++)
		{
			path = pre[i] - 'a';
			if (node->nexts[path] == nullptr)
			{
				return 0;
			}
			node = node->nexts[path];
		}
		return node->pass;
	}
	//所有加入的字符串中,是否有word
	bool search(string word)
	{
		if (word == "") return false;
		TrieNode* node = this;

		int path = 0;
		for (int i = 0; i < word.size(); i++)
		{
			path = word[i] - 'a';
			if (node->nexts[path] == nullptr)
			{
				return false;
			}
			node = node->nexts[path];
		}
		return node->end>0;
	}
	//递归删除多余的字符串,防止内存泄漏
private:
	void _deleteNextNode(string word,TrieNode*cur,int i)
	{
		if (i == word.size())return;
		else
		{
			int path= word[i] - 'a';
			_deleteNextNode(word, cur->nexts[path], i+1);
			delete cur->nexts[path];
		}
	}
public:
	//删除字符串
	void deleteString(string word)
	{
		if (search(word) != 0)
		{
			TrieNode* node = this;
			node->pass--;
			int path = 0;
			for (int i = 0; i < word.size(); i++)
			{
				path = word[i] - 'a';
				if (--node->nexts[path]->pass==0)
				{
					_deleteNextNode(word, node->nexts[path], i);
					node->nexts[path] = nullptr;
					return;
				}
				node = node->nexts[path];
			}
			node->end--;
		}
	}
	//析构函数,将new出来的空间全部释放掉
	~TrieNode()
	{
		for (auto& e : this->nexts)
		{
			if (e != nullptr)
			{
				delete e;
			}
		}
	}

private:
	//pass:经过该节点的路径有几条
	int pass;
	//end:以该节点结尾的路径有几条
	int end;
	//下级的数组
	vector<TrieNode*> nexts;
};

在这里插入图片描述

  • map实现
    map适合字符串中字符种类很多的情况
class TrieNodeMap
{
public:
	TrieNodeMap()
	{
		pass = 0;
		end = 0;
		//假设字符都为小写字母
	}
	void insert(string word)
	{
		if (word == "")return;
		TrieNodeMap* node = this;
		node->pass++;
		int path = 0;
		for (int i = 0; i < word.size(); i++)
		{
			path = (int)word[i];
			if (node->nexts.find(path) == node->nexts.end())
			{
				node->nexts[path] = new TrieNodeMap();
			}
				node = node->nexts[path];
				node->pass++;
		}
		node->end++;
	}
	//所有加入的字符串中,有几个是以pre这个字符串作为前缀的
	int prefixNumber(string pre)
	{
		if (pre == "") return 0;
		TrieNodeMap* node = this;
		int path = 0;
		for (int i = 0; i < pre.size(); i++)
		{
			path = (int)pre[i];
			if (node->nexts.find(path) == node->nexts.end())
			{
				return 0;
			}
			node = node->nexts[path];
		}
		return node->pass;
	}
	//所有加入的字符串中,是否有word
	bool search(string word)
	{
		if (word == "") return false;
		TrieNodeMap* node = this;

		int path = 0;
		for (int i = 0; i < word.size(); i++)
		{
			path = (int)word[i];
			if (node->nexts.find(path) == node->nexts.end())
			{
				return false;
			}
			node = node->nexts[path];
		}
		return node->end > 0;
	}
	//递归删除多余的字符串,防止内存泄漏
private:
	void _deleteNextNode(string word, TrieNodeMap* cur, int i)
	{
		if (i == word.size())return;
		else
		{
			int path = (int)word[i];
			_deleteNextNode(word, cur->nexts[path], i + 1);
			delete cur->nexts[path];
			cur->nexts.erase(path);
		}
	}
public:
	//删除字符串
	void deleteString(string word)
	{
		if (search(word) != 0)
		{
			TrieNodeMap* node = this;
			node->pass--;
			int path = 0;
			for (int i = 0; i < word.size(); i++)
			{
				path = (int)word[i];
				if (--node->nexts[path]->pass == 0)
				{
					_deleteNextNode(word, node->nexts[path], i);
					node->nexts.erase(path);
					return;
				}
				node = node->nexts[path];
			}
			node->end--;
		}
	}
public:
	//析构函数,将new出来的空间全部释放掉
	~TrieNodeMap()
	{
		for (auto& e : this->nexts)
		{
			if (e.second != nullptr)
			{
				delete e.second;
				e.second = nullptr;
			}
		}
		return;
	}

private:
	//pass:经过该节点的路径有几条
	int pass;
	//end:以该节点结尾的路径有几条
	int end;
	//下级的数组
	map<int,TrieNodeMap*> nexts;
};

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天也要写bug、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值