最简单的trie树

#include <iostream>
#include <string>

using namespace std;

class TrieTree{
	struct TrieNode{
		bool isStr;
		TrieNode* child[26];
		TrieNode():isStr(false){ 
			for (int i = 0; i < 26; i++)
				child[i] = NULL;
		}
	};
	TrieNode* root;
public:
	TrieTree();
	~TrieTree();
	void insert(string str);
	void remove(string str);
	bool search(string str);
};

TrieTree::TrieTree()
{
	root = new TrieNode();
}

void TrieTree::insert(string str)
{
	int index = 0;
	TrieNode *pNode = root;
	while (index < str.size()){
		if (pNode->child[str[index]-'a'] == NULL){
			pNode->child[str[index] - 'a'] = new TrieNode();
		}
		pNode = pNode->child[str[index] - 'a'];
		index++;
	}
	pNode->isStr = true;
}

void TrieTree::remove(string str)
{
	bool isLeaf = true;
	int index = 0;
	TrieNode *pPre = NULL;
	TrieNode *pNode = root;
	while (index < str.size() && pNode){
		pPre = pNode;
		pNode = pNode->child[str[index] - 'a'];
		index++;
	}
	if (pNode){
		for (int i = 0; i < 26; i++){
			if (pNode->child[i]){
				isLeaf = false;
				break;
			}	
		}
		if (pPre&&isLeaf){
			delete pNode;
			pPre->child[str[index - 1] - 'a'] = NULL;
		}
		else{
			pNode->isStr = false;
		}
	}
}

bool TrieTree::search(string str)
{
	int index = 0;
	TrieNode *pNode = root;
	while (index<str.size()&&pNode){
		pNode = pNode->child[str[index] - 'a'];
		index++;
	}
	if (pNode&&pNode->isStr)
		return true;
	return false;
}

TrieTree::~TrieTree()
{

}

int main(void)
{
	TrieTree tree;
	
	tree.insert("a");
	tree.insert("abcd");
	tree.insert("ab");
	tree.insert("bd");
	tree.insert("bcd");
	tree.insert("bdcc");
	tree.insert("b");
	tree.insert("");
	tree.remove("abcde");
	cout << tree.search("ba") << endl;
	return 0;
}

### 回答1: 这是一段基于 Python 的 trie 的代码: ``` class TrieNode: def __init__(self): self.children = {} self.is_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_word = True def search(self, word): node = self.root for char in word: if char not in node.children: return False node = node.children[char] return node.is_word def starts_with(self, prefix): node = self.root for char in prefix: if char not in node.children: return False node = node.children[char] return True ``` 这个 trie 的实现包含了三个操作:插入单词,搜索单词以及搜索前缀。 ### 回答2: Trie,也称为字典或前缀,是一种特殊的数据结构,通常用于快速检索和存储字符串集合。下面是一个用Python编写的Trie的基本代码: ```python class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): current_node = self.root for char in word: if char not in current_node.children: current_node.children[char] = TrieNode() current_node = current_node.children[char] current_node.is_end_of_word = True def search(self, word): current_node = self.root for char in word: if char not in current_node.children: return False current_node = current_node.children[char] return current_node.is_end_of_word def starts_with(self, prefix): current_node = self.root for char in prefix: if char not in current_node.children: return False current_node = current_node.children[char] return True ``` 上述代码中,`TrieNode`类表示Trie的节点。每个节点包含一个子节点字典和一个布尔值`is_end_of_word`,用于标记是否是一个单词的结尾。 `Trie`类包含了Trie的基本操作。其中`insert`方法用于向Trie中插入一个单词,`search`方法用于查找一个单词是否存在,`starts_with`方法用于检查一个前缀是否存在。 这段代码实现了一个简单Trie,可以用于处理一些与字符串相关的问题,如单词的插入、查找和前缀匹配等操作。需要注意的是,上述代码只是Trie的基本实现,仍有许多优化和扩展的空间,如节点压缩、前缀搜索等。 ### 回答3: Trie,也被称为字典或前缀,是一种常用的数据结构,用于存储和搜索字符串集合。下面是一个简单Trie代码示例: ```python class TrieNode: def __init__(self): self.children = {} # 子节点集合 self.is_word = False # 标记是否为一个完整的单词 class Trie: def __init__(self): self.root = TrieNode() # 初始化根节点 def insert(self, word): node = self.root # 从根节点开始 for char in word: if char not in node.children: node.children[char] = TrieNode() # 如果字符不在子节点集合中,则添加一个新节点 node = node.children[char] # 继续处理下一个字符 node.is_word = True # 标记最后一个节点为一个完整的单词 def search(self, word): node = self.root for char in word: if char not in node.children: return False # 如果字符在任何节点中都不存在,则单词不存在 node = node.children[char] return node.is_word # 判断最后一个节点是否为一个完整的单词 def startsWith(self, prefix): node = self.root for char in prefix: if char not in node.children: return False # 如果字符在任何节点中都不存在,则前缀不存在 node = node.children[char] return True # 返回True,表示存在以该前缀开头的单词 ``` 这段代码定义了两个类:`TrieNode`表示一个Trie的节点,包含子节点集合和标记是否为一个完整的单词;`Trie`表示Trie,其中的方法包括`insert`插入一个单词,`search`搜索是否存在一个单词,以及`startsWith`判断是否存在以某个前缀开头的单词。 这个Trie代码示例可以用于构建和查询字符串集合。通过不断向Trie插入单词,可以将它们按字符顺序分解成状结构,通过遍历节点来搜索和判断单词是否存在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值