Implement Trie Tree by hashmap

Trie树实现与应用
本文介绍了一种使用哈希映射实现的Trie树数据结构,并提供了插入、搜索及前缀匹配等核心功能的代码示例。通过递归与非递归方式实现了字符串查找,同时展示了如何获取所有以特定前缀开头的单词。

Here is the implementation of Trie Tree. Using array is good but using hashmap is more straightforward.

The key interface need to be implement is insert and search.

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

class TrieNode {
public:
	unordered_map<char, TrieNode*> children;
	bool isWord;
	TrieNode(){
		isWord = false;
	}
	
	void insert(string word, int index){
		if(index == word.length()){
			this->isWord = true;
			return;
		}
		char ch = word.at(index);
		if(children.find(ch) == children.end()){
			children[ch] = new TrieNode();
		}
		children[ch]->insert(word, index+1);
	}
	// recursive
	TrieNode* find(string word, int index){
		if(index == word.length()){
			return this;
		}
		char ch = word.at(index);
		if(children.find(ch) == children.end()){
			return NULL;
		}
		return children[ch]->find(word, index+1);
	}
	// non recursive
	TrieNode * find(string word){
		if(word.length() == 0){
			return NULL;
		}
		TrieNode* node = NULL;
		unordered_map<char, TrieNode*> sons = children;
		for(int i = 0; i < word.length(); i++){
			char ch = word.at(i);
			if(sons.find(ch) == sons.end()){
				return NULL;
			}
			node = sons[ch];
			sons = node->children;
		}
		return node;
	}
};

class Trie {
private:
	TrieNode * root;
public:
	Trie(){
		root = new TrieNode();
	}
	Trie(vector<string> dictionary){
		root = new TrieNode();
		// build the trie tree
		for(string word : dictionary){
			root->insert(word, 0);
		}
	}
	void insert(string word){
		root->insert(word, 0);
	}
	bool search(string word){
		TrieNode* node = root->find(word, 0);
		return (node != NULL) && (node->isWord);
	}
	bool startWith(string prefix){
		TrieNode* node = root->find(prefix, 0);
		return node != NULL;
	}
	
	///////////////////////////////////TEST PURPOSE////////////////////////////////////////////////
	// get all strings that start with prefix
	void helper(vector<string> &result, string &path, TrieNode * node, string prefix){
		if(node == NULL || node->children.size() == 0){
			result.push_back(prefix + path);
			return;
		}
		for(auto it = node->children.begin();
			     it != node->children.end();
				 it++){	
			path.push_back(it->first);
			helper(result, path, it->second, prefix);
			path.pop_back();
		}
	}
	vector<string> getAll(string prefix){
		vector<string> ans;
		TrieNode * node = root->find(prefix, 0);
		if(node == NULL || node->children.size() == 0){
			return ans;
		}
		
		string path;
		helper(ans, path, node, prefix);
		
		return ans;
	}
	////////////////////////////////////////////////////////////////////////////////////////////
};

int main(void){
	vector<string> dictionary = {"hello world", "goodman", "good engineer", "emc", "sql language", "google", "amazon"};
	Trie trie(dictionary);
	vector<string> output = trie.getAll("goo");
	
	for(string str : output){
		cout << str <<endl;
	}
	return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值