添加与搜索单词——数据结构设计

添加时一样,搜索不一样。深度搜索Trie

node为正在搜索的节点,word指向正在搜索的单词起始,查找成功返回真;否则返回假;

 

当遍历到单词结束时(word指向’\0’:

    如果node指向的结点标记为单词的结尾(is_end为真)

                    返回真

            否则返回假

如果word指向 ’.’:

    遍历node的全部孩子指针:

                    如果孩子指针为真,继续递归深搜该孩子子树,单词指针向前移动一个位置,

                            如果递归深搜为真,返回真

否则(‘a’ - ‘z’)小写字母

    计算孩子位置pos = 当前字符 - ‘a’;

    如果pos指向的孩子指针为真,继续递归深搜该孩子子树,单词指针向前移动一个位置,

                    如果递归深搜结果为真,则返回真

最终返回假


#include<vector>

#define TRIE_MAX_CHAR_NUM 26
struct TrieNode{
	TrieNode* child[TRIE_MAX_CHAR_NUM];
	bool is_end;
	TrieNode() :is_end(false){
		for(int i = 0; i < TRIE_MAX_CHAR_NUM; ++i)
			child[i] = 0;
	}
};

class TrieTree{
public:
	TrieTree(){}
	~TrieTree(){
		for(int i = 0; i < _node_vec.size(); ++i)
			delete _node_vec[i];
	}
	//插入字符串
	void insert(const char* word){
		TrieNode* ptr = &_root;
		while(*word)
		{
			int pos = *word - 'a';
			if(!ptr->child[pos])
				ptr->child[pos] = new_node();

			ptr = ptr->child[pos];
			word++;
		}
		ptr->is_end = true;
	}
	//搜索trie树里有没有某个字符串
	bool search(const char* word){
		TrieNode* ptr = &_root;
		while(*word)
		{
			int pos = *word - 'a';
			if(!ptr->child[pos])
				return false;
			ptr = ptr->child[pos];
			word++;
		}
		return ptr->is_end;
	}
	//判断trie树中有没有以某个字符串开头的字符串,有就返回true
	bool startWith(const char* prefix){
		TrieNode* ptr = &_root;
		while(*prefix)
		{
			int pos = *prefix - 'a';
			if(!ptr->child[pos])
				return false;
			ptr = ptr->child[pos];
			prefix++;
		}
		return true;
	}

	TrieNode* root()
	{
		return &_root;
	}

private:
	TrieNode* new_node(){
		TrieNode* node = new TrieNode();
		_node_vec.push_back(node);
		return node;
	}
	std::vector<TrieNode*> _node_vec;
	TrieNode _root;
};


class WordDictionary {
public:
    /** Initialize your data structure here. */
    WordDictionary() {
        
    }
    
    /** Adds a word into the data structure. */
    void addWord(string word) {
        _trie_tree.insert(word.c_str());
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    bool search(string word) {
        return search_trie(_trie_tree.root(),word.c_str());
    }
    
    bool search_trie(TrieNode* node,const char* word)
    {
        if(*word == '\0')
        {
            if(node->is_end )
                return true;
            return false;
        }
        if(*word == '.')
        {
            for(int i = 0;i < TRIE_MAX_CHAR_NUM; ++i)
            {
                if(node->child[i] && search_trie(node->child[i], word+1))
                    return true;
            }
        }
        else
        {
            int pos = *word - 'a';
            if(node->child[pos] && search_trie(node->child[pos] ,word+1))
                return true;
        }
        return false;
    }
private:
    TrieTree _trie_tree;
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * bool param_2 = obj.search(word);
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值