今年的百度实习生笔试题中有个题是:
相信大家都使用过百度搜索框的suggestion功能,百度搜索框中的suggestion提示功能如何实现?请给出实现思路和主要的数据结构、算法。有什么优化思路可以使得时间和空间效率最高?
这个题的基础实现方法是使用Trie树, 原理部分摘用别的. (代码部分我增加了findTips功能, 这里采用方法1,空间要求最大 26^n,,若是汉字,那不能使用了。)
树的结构可以有这三种实现方法 :
一般有三种方法:
1、对每个结点开一个字母集大小的数组,对应的下标是儿子所表示的字母,内容则是这个儿子对应在大数组上的位置,即标号;
2、对每个结点挂一个链表,按一定顺序记录每个儿子是谁;
3、使用左儿子右兄弟表示法记录这棵树。
三种方法,各有特点。第一种易实现,但实际的空间要求较大;第二种,较易实现,空间要求相对较小,但比较费时;第三种,空间要求最小,但相对费时且不易写。
http://zhangzhibiao02005.blog.163.com/blog/static/373678202010667524646/
昨天在北邮人bbs上逛时,看到有人在讨论字符窜提示的问题,有人回帖用trie树来做,而我以前也听过trie树,不过想系统学习下,于是大概用了一个下午的时间来学习trie树。网上有很多关于trie树的资料,其核心思想就时拿空间换取时间,很适合搜索前缀子窜,时间效率比较高,主要是占用空间比较大。为什么会这样呢?看下trie树的结构就知道拉。如图所示,该trie树存有abc、d、da、dda四个字符串,如果是字符串会在节点的尾部进行标记。没有后续字符的branch分支指向NULL
已知n个由小写字母构成的平均长度为10的单词,判断其中是否存在某个串为另一个串的前缀子串。下面对比3种方法:
1.
2.
3.
解释一下hash为什么不能将建立与查询同时执行,例如有串:911,911456输入,如果要同时执行建立与查询,过程就是查询911,没有,然后存入9、91、911,查询911456,没有然后存入9114、91145、911456,而程序没有记忆功能,并不知道911在输入数据中出现过。所以用hash必须先存入所有子串,然后for循环查询。
而trie树便可以,存入911后,已经记录911为出现的字符串,在存入911456的过程中就能发现而输出答案;倒过来亦可以,先存入911456,在存入911时,当指针指向最后一个1时,程序会发现这个1已经存在,说明911必定是某个字符串的前缀,
trie树的简单实现(增加了findTips功能):
#include <iostream>
#include <cstdlib>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
const int branchNum = 26;
struct TrieNode
{
bool isStr;
TrieNode *next[branchNum];
TrieNode()
:isStr(false)
{
memset(next , 0 , sizeof(next));
}
};
class Trie
{
public:
Trie();
void insert(const char *word);
bool search(const char *word);
void deleteTrie(TrieNode *root);
vector<string> findTips(const char *prefix); //here
void findTips(TrieNode *root , string track , vector<string> &tips); //here
private:
TrieNode *root;
};
Trie::Trie()
{
root = new TrieNode(); //绗竴涓猲ode鏃犵敤
}
void Trie::insert(const char *word)
{
TrieNode *location = root;
while(*word) {
if(location->next[*word - 'a'] == NULL) {
TrieNode *tmp = new TrieNode();
location->next[*word - 'a'] = tmp;
}
location = location->next[*word - 'a'];
word++;
}
location->isStr = true;
}
bool Trie::search(const char *word)
{
TrieNode *location = root;
while(*word && location) {
location = location->next[*word - 'a'];
word ++;
}
return (location != NULL && location->isStr);
}
void Trie::deleteTrie(TrieNode *root)
{
for(int i = 0 ; i < branchNum ; i++) {
if(root->next[i] != NULL) {
deleteTrie(root->next[i]);
}
}
delete root;
}
vector<string> Trie::findTips(const char *prefix)
{
vector<string> tips;
string track(prefix);
int i;
struct TrieNode *node;
for(i = 0 ,node = root ; i < strlen(prefix) && node != NULL ; i++) {
node = node->next[prefix[i] - 'a'];
}
if(node == NULL && i != strlen(prefix))
return tips; //empty
findTips(node, track , tips);
return tips;
}
void Trie::findTips(TrieNode *root,string track , vector<string> &tips) //track长度最多为最长字符串长度,
{
if(root == NULL)
return;
if(root->isStr)
tips.push_back(track);
for(int i = 0 ; i < branchNum ; i++) {
findTips(root->next[i] , track + (char)('a'+i) , tips);
}
}
int main()
{
Trie t;
t.insert("a");
t.insert("abandon");
t.insert("abandoned");
t.insert("abashed");
if(t.search("abashed"))
cout << "true" << endl;
vector<string> tips = t.findTips("ab");
cout << "ab" << " tips are :(" << tips.size() <<")" <<endl;
for(int i = 0 ; i < tips.size() ; i ++)
cout << tips[i] << endl;
}
运行后出来结果:
true
ab tips are :(3)
abandon
abandoned
abashed
至于优化的话,也许在Trie树的实现就可以进行优化。向这种实现方式的化,每个节点就得包括所有的字符集。如果换成汉字,那就是每个结点上千个了。
注意:对于汉字的话,可以弄一个索引,比如0代表啊,1代码阿,等等 , 就无法使用了。
树的深度就是最长的单词长度 。。
比如:http://linux.thai.net/~thep/datrie/datrie.html#Double双数组的实现方式