Trie (字典树) 的基本操作---插入,删除,单词前缀匹配等等

#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;


const int R = 26;
typedef struct TrieTree* Position;

enum Color{red,black};

struct TrieTree {
	char ch;
	Color color;//red表示一个单词到此处结束,black表示单词还未结束。
	Position next[R];
};
class Trie {
public:
/* TrieTree类(前缀树)
 * 接口:
 * MakeEmpty:重置功能,重置整颗前缀树
 * keys:获取功能,获取TrieTree中的所有单词,并储存在一个向量中
 * Insert:插入功能,向单词树中插入新的单词
 * Delete:删除功能,删除单词树的指定单词
 * IsEmpty:空函数,判断单词树是否为空
 * Find:查找函数,查找对应的单词,并返回查找情况:查找到返回true,否则返回false
 * KeysWithPrefix:查找以指定字符串为前缀的单词;
 */
	Trie();
	~Trie();
	//接口函数
	void MakeEmpty(void);
	vector<string>keys();
	void Insert(string);
	void Delete(string);
	bool isempty();
	bool Find(string) const;
	vector<string>KeysWithPrefix(string)const;
private:
	//辅助函数
	void MakeEmpty(Position);
	void Insert(string, Position&, int);
	void Delete(string, Position&, int);
	Position Find(string, Position,int)const;
	void Collect(string, Position, vector<string>&)const;
	Position root;
};
Trie::Trie() {
	root = new struct TrieTree;
	if (root == NULL) {
		cout << "申请失败"<<endl;
		return;
	}
	else {
		root->color = black;
		for (int i = 0; i < R; i++) {
			root->next[i] = NULL;
		}
	}
}
Trie::~Trie() {
	MakeEmpty(root);
}
void Trie::MakeEmpty(void) {
	for (int i = 0; i < R; i++) {
		if (root->next[i] != NULL) {
			MakeEmpty(root->next[i]);
		}
	}
}
void Trie::MakeEmpty(Position h) {
	for (int i = 0; i < R; i++) {
		if (h->next[i] != NULL) {
			MakeEmpty(h->next[i]);
		}
	}
	delete h;
	h = NULL;
}
vector<string>Trie::keys() {
	// 返回所有以""为前缀的单词,即所有单词
	return KeysWithPrefix("");
}
void Trie::Insert(string key) {
	Insert(key, root, 0);
}
void Trie::Insert(string key, Position& r, int k) {
	if (r == NULL) {
		r = new struct TrieTree;
		if (r == NULL) {
			cout << "申请失败" << endl;
			return;
		}
		else {
			r->color = black;
			if (k - 1 >= 0) {
				r->ch = key[k - 1];
			}
			for (int i = 0; i < R; i++) {
				r->next[i] = NULL;
			}
		}
	}
	if (k == key.size()) {
		r->color = red;
		return;
	}
	int pos = tolower(key[k]) - 'a';
	Insert(key, r->next[pos], k + 1);
}
void Trie::Delete(string key) {
	Delete(key, root, 0);
}
void Trie::Delete(string key, Position& r, int k) {
	//bool flag = false;
	if (r == NULL) {
		return;
	}
	if (k == key.length()) {
		r->color = black; //flag = true;
	}
	else {
		int pos = tolower(key[k]) - 'a';
		Delete(key, r->next[pos], k + 1);
	}
	if (r->color == red) {
		return;
	}
	for (int i = 0; i < R; ++i) {
		if (r->next[i] != NULL) return;
	}
	delete r;
	r = NULL;
}
bool Trie::isempty() {
	for (int i = 0; i < R; i++) {
		if (root->next[i] != NULL) {
			return false;
		}
	}
	return true;
}
bool Trie::Find(string s)const {
	Position p = Find(s, root, 0);
	if (p == NULL) return false;
	if (p->color == red) return true;
	else return true;
}
Position Trie::Find(string s,Position r,int k) const{
	if (r == NULL) {
		return NULL;
	}
	if (k == s.size()) {
		return r;
	}
	int pos = tolower(s[k]) - 'a';
	return Find(s, r->next[pos], k + 1);
}
/* 前缀查找驱动:将当前层次所有符合前缀要求的单词存入向量
 * 参数:key:指定的前缀,r:当前的节点层次,str:用于储存的向量
 * 返回值:无
 */
void Trie::Collect(string key, Position r, vector<string>& str)const {
	if (r == NULL) {
		return;
	}
	if (r->color == red) {
		str.push_back(key);
	}
	for (int i = 0; i < R; i++) {
		char ch = 'a' + i;
		Collect(key + ch, r->next[i], str);
	}
}
/* 前缀查找:查找TrieTree中所有以指定字符串为前缀的单词
 * 参数:key:指定的前缀
 * 返回值:vector<string>:储存了所有目标单词的向量
 */
vector<string>Trie::KeysWithPrefix(string key)const {
	vector<string>V;
	Collect(key, Find(key,root,0), V);
	return V;
}
int main()
{
	Trie Mytrie;
	Mytrie.Insert("dog");
	Mytrie.Insert("cat");
	Mytrie.Insert("father");
	Mytrie.Insert("ddt");
	Mytrie.Insert("fafad");
	Mytrie.Insert("cai");
	vector<string>S = Mytrie.KeysWithPrefix("fa");
	for (int i = 0; i < S.size(); ++i) {
		cout << S[i] << " ";
	}
	cout << endl;
	Mytrie.Delete("dog");
	vector<string>V = Mytrie.KeysWithPrefix("d");
	for (int i = 0; i < V.size(); i++) {
		cout << V[i] << " ";
	}
	cout << endl;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值