Trie树的常见应用大总结(面试+附代码实现)
(一)Trie的简单介绍
Trie树,又称字典树,单词查找树或者前缀树。是一种用于高速检索的多叉树结构,如英文字母的字典树是一个26叉树。数字的字典树是一个10叉树。
他的核心思想是空间换时间,空间消耗大可是插入和查询有着非常优秀的时间复杂度。
(二)Trie的定义
Trie树的键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的全部子孙都有同样的前缀(prefix),从根节点到当前结点的路径上的全部字母组成当前位置的字符串。结点能够保存当前字符串、出现次数、指针数组(指向子树)以及是否是结尾标志等等。
typedef struct Trie_Node
{
char count[15]; //单词前缀出现的次数
struct Trie_Node* next[MAXN]; //指向各个子树的指针
bool exist; //标记结点处是否构成单词
}Trie;
Trie树能够利用字符串的公共前缀来节约存储空间,例如以下图所看到的:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
它有3个基本性质:
(1) 根节点不包括字符,除根节点外每个节点都仅仅包括一个字符。
(2) 从根节点到某一节点,路径上经过的字符连接起来,为该节点相应的字符串。
(3) 每一个节点的全部子节点包括的字符都不同样。
(三)Trie树的基本操作
(1)插入操作
按下标索引逐个插入字母,若当前字母存在则继续下一个,否则new出当前字母的结点,所以插入的时间复杂度仅仅和字符串的长度n有关,为O(n)。
void Insert(Trie *root, char* s,char *add)
{
Trie *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
p->next[*s-'a']=createNode();
}
p=p->next[*s-'a'];
// p->count=add;
++s;
}
p->exist=true;
strcpy(p->count,add);
}
(2)查询操作
和插入操作相仿,若查询途中某一个结点并不存在,则直接就return返回。否则继续下去,当字符串结束时,trie树上也有结束标志。那么证明此字符串存在,return true;
int Search(Trie* root,const char* s)
{
Trie *p=root;
while(*s!='\0')
{
p=p->next[*s-'a'];
if(p==NULL)
return 0;
++s;
}
return p->count;
}
(3)删除操作
一般来说,对Trie单个结点的删除操作不常见,所以我在这里也仅仅提供递归删除整个树的操作
void del(Trie *root)
{
for(int i=0;i<MAXN;i++)
{
if(root->next[i]!=NULL)
{
del(root->next[i]);
}
}
// free(root);
delete root;
}
(4)遍历操作
假设我们想要将trie中的字符串排序输出,直接先序遍历就可以。
void Print(Trie *root)
{
Trie *p=root;
if(p->exist)
cout<<p->name<<": "<<p->count<<endl;
for(int i=0;i<26;i++)
{
if(p->next[i]!=NULL){
Print(p->next[i]);
}
}
}
(四)Trie树的详细应用
(1)统计前缀出现的次数
这是Trie最主要的应用,每一个结点的字母使用count记录出现的次数就可以。
这里提供一道题目,hdu 1251供大家练习。
//hdu 1251 统计前缀出现次数
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int MAXN=26;
typedef struct Trie_Node
{
int count; //单词前缀出现的次数
struct Trie_Node* next[MAXN]; //指向各个子树的指针
bool exist; //标记结点处是否构成单词
}Trie;
Trie* createNode()
{
//Trie* p =(Trie*)malloc(sizeof(Trie));
Trie *p=new Trie;
p->count=0;
p->exist=false;
memset(p->next,0,sizeof(p->next));
return p;
}
void Insert(Trie *root, const char* s)
{
Trie *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
p->next[*s-'a']=createNode();
}
p=p->next[*s-'a'];
p->count+=1;
++s;
}
p->exist=true;
} int Search(Trie* root,const char* s)
{
Trie *p=root;
while(*s!='\0')
{
p=p->next[*s-'a'];
if(p==NULL)
return 0;
++s;
}
return p->count;
} void del(Trie *root)
{
for(int i=0;i<MAXN;i++)
{
if(root->next[i]!=NULL)
{
del(root->next[i]);
}
}
// free(root);
delete root;
}
int main()
{
char s[15];
bool flag=false;
Trie* root=createNode();
while(gets(s))
{
if(flag)
{
int ans=Search(root,s);
printf("%d\n",ans);
}
else
{
if(strlen(s)!=0)
Insert(root,s);
}
if(strlen(s)==0)
flag=true;
}
del(root);
return 0;
}
(2)翻译(password,明文)
给定一组字符串s。k我们输入k则须要翻译成s。也就是说两者是映射关系。接下来我们给出一段话,让你翻译出正常的文章。
用map固然简便。可是Trie的效率更加高。
仅仅须要在k的结尾结点出记录下s就可以。
这里也提供一道题目。hdu 1075。(被凝视的是我原来的程序,wa了,有大神看出来麻烦告诉我一下。谢谢)。
/*
//hdu 1075映射
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
using namespace std;
const int MAXN=26;
typedef struct Trie_Node
{
char count[15]; //单词前缀出现的次数
struct Trie_Node* next[MAXN]; //指向各个子树的指针
bool exist; //标记结点处是否构成单词
}Trie;
Trie* createNode()
{
Trie* p =(Trie*)malloc(sizeof(Trie));
p->exist=false;
memset(p->next,0,sizeof(p->next));
return p;
}
void Insert(Trie *root, char* s,char *add)
{
Trie *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
p->next[*s-'a']=createNode();
}
p=p->next[*s-'a'];
// p->count=add;
++s;
}
p->exist=true;
strcpy(p->count,add);
} void Search(Trie* root, const char* s)
{
Trie *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
printf("%s",s);
return ;
} p=p->next[*s-'a'];
++s;
}
if(p->exist)
printf("%s",p->count);
else
printf("%s",s);
} void del(Trie *root)
{
for(int i=0;i<MAXN;i++)
{
if(root->next[i]!=NULL)
{
del(root->next[i]);
}
}
free(root);
}
int main()
{
char text[3013],from[15],to[15];
Trie* root=createNode();
scanf("%s",from);
while(scanf("%s",from),strcmp(from,"END"))
{
scanf("%s",to);
Insert(root,to,from);
}
scanf("%s",from);
getchar();
while(gets(text),strcmp(text,"END"))
{
int len=strlen(text);
for(int i=0;i<len;i++)
{
if(islower(text[i]))
{
int j=0;
char temp[15];
memset(temp,'\0',sizeof(temp));
while(islower(text[i]))
temp[j++]=text[i++];
Search(root,temp); }
if(!islower(text[i]))
printf("%c",text[i]);
}
printf("\n");
}
return 0;
}
*/ #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string> using namespace std; struct node{
char dic[15];
node * next[26];
bool flag;
}*root; node *build()
{
node *p=(node *)malloc(sizeof(node));
for(int i=0;i<26;i++)
p->next[i]=NULL;
p->flag=false;
return p;
} void insert(char *earth,char *mars)
{
int len=strlen(mars);
node *p;
p=root;
for(int i=0;i<len;i++)
{
if(p->next[mars[i]-'a']==NULL)
p->next[mars[i]-'a']=build();
p=p->next[mars[i]-'a'];
}
p->flag=true;
strcpy(p->dic,earth);
} void query(char *earth)
{
int len=strlen(earth);
node *p;
p=root;
for(int i=0;i<len;i++)
{
if(p->next[earth[i]-'a']==NULL)
{
printf("%s",earth);
return;
}
p=p->next[earth[i]-'a'];
}
if(p->flag)
printf("%s",p->dic);
else
printf("%s", earth);
} int main()
{
char earth[15],mars[15],ask[3010]; scanf("%s",earth);
root=build();
while(scanf("%s",earth),strcmp(earth,"END"))
{
scanf("%s",mars);
insert(earth,mars);
} scanf("%s",earth);
getchar();
while(gets(ask),strcmp(ask,"END"))
{
int len=strlen(ask);
for(int i=0;i<len;i++)
{
if(islower(ask[i]))
{
int j=0;
memset(earth,'\0',sizeof(earth));
while(islower(ask[i]))
earth[j++]=ask[i++];
query(earth);
}
if(!islower(ask[i]))
printf("%c",ask[i]);
}
printf("\n");
} return 0;
}
(3)实现搜索引擎的热门搜索排名
我的初步想法是和(1)类似。对(1)中的trie进行先序遍历,将字符串和出现次数存进一个结构体,最后对这个数组进行高速排序。时间复杂度为O(nlogn),看网上说能够利用分治+trie
+最小堆。我还没有细致搞清楚,以后研究完在加入。
(4)输入自己主动补全
事实上原理都差点儿相同。把字符串结尾处的结点当作root。进行先序遍历,就可以得出全部以输入的字符串为前缀的答案。
/ 自己主动补全
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int MAXN=26;
typedef struct Trie_Node
{
int count; //单词出现的次数
struct Trie_Node* next[MAXN]; //指向各个子树的指针
bool exist; //标记结点处是否构成单词
char name[15];
}Trie;
Trie* createNode()
{
Trie* p =(Trie*)malloc(sizeof(Trie));
p->count=0;
p->exist=false;
memset(p->next,0,sizeof(p->next));
return p;
}
void Insert(Trie *root,char* word)
{
Trie *p=root;
char *s=word;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
p->next[*s-'a']=createNode();
}
p=p->next[*s-'a'];
++s;
}
p->exist=true;
p->count+=1;
strcpy(p->name,word);
} Trie* Search(Trie* root, char* s)
{
Trie *p=root;
while(*s!='\0')
{
p=p->next[*s-'a'];
if(p==NULL)
return 0;
++s;
}
return p;
} void del(Trie *root)
{
for(int i=0;i<MAXN;i++)
{
if(root->next[i]!=NULL)
{
del(root->next[i]);
}
}
free(root); }
void Print(Trie *root)
{
Trie *p=root;
if(p->exist)
cout<<p->name<<": "<<p->count<<endl;
for(int i=0;i<26;i++)
{
if(p->next[i]!=NULL){
Print(p->next[i]);
}
}
}
int main()
{
char s[15];
Trie* root=createNode();
for(int i=0;i<5;i++)
{
cin>>s;
Insert(root,s);
}
while(cin>>s)
{
Trie *ans=Search(root,s);
if(ans)
Print(ans);
}
del(root);
return 0;
}
Trie树的常见应用大总结(面试+附代码实现)的更多相关文章
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- 剑指Offer——Trie树(字典树)
剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种的单词.对于每一个单词,我们要判断他出没出现过,如果出现了,求第一次出现在第几个位 ...
- poj_3630 trie树
题目大意 给定一系列电话号码,查看他们之间是否有i,j满足,号码i是号码j的前缀子串. 题目分析 典型的trie树结构.直接使用trie树即可.但是需要注意,若使用指针形式的trie树,则在大数据量下 ...
- 双数组Trie树 (Double-array Trie) 及其应用
双数组Trie树(Double-array Trie, DAT)是由三个日本人提出的一种Trie树的高效实现 [1],兼顾了查询效率与空间存储.Ansj便是用DAT(虽然作者宣称是三数组Trie树,但 ...
- Trie树之C-实现
title: Trie树之C++实现 comments: true date: 2016-10-02 16:59:54 categories: 算法 tags: Trie树 前言 之前写了一篇偏向于理 ...
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...
- 大数据处理-Trie树
大数据处理--Trie树 1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被 ...
- 数据结构 | 30行代码,手把手带你实现Trie树
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第28篇文章,我们一起来聊聊一个经典的字符串处理数据结构--Trie. 在之前的4篇文章当中我们介绍了关于博弈论的 ...
- [转]双数组TRIE树原理
原文名称: An Efficient Digital Search Algorithm by Using a Double-Array Structure 作者: JUN-ICHI AOE 译文: 使 ...
随机推荐
- python购物车系统
购物车系统模拟:product_list = [ ('java',100), ('python',200), ('键盘',500), ('电脑',4000), ('mac Book',7000),]S ...
- 题解 P3128 【[USACO15DEC]最大流Max Flow】
此类型题目有两种比较常见的做法:树链剖分和树上差分. 本题有多组修改一组询问,因此树上差分会比树链剖分优秀很多. 这里两种方法都进行介绍. 树链剖分和树上差分的本质都是将一颗树转换为一个区间,然后进行 ...
- ActiveMQ学习总结(9)——Linux中安装ActiveMQ
1.新建一个文件夹activeMQ mkdir /server 2.授权 chmod 777 /server 3.下载activeMQ安装包,拷贝到/activeMQ目录下 apache-a ...
- solr环境搭建&基本使用
分步指南 solr服务与tomcat整合 solr使用配置步骤 solr使用 推荐分词工具 相关的文章 一.Solr服务与tomcat整合 1.solr相关版本下载路径:http://archive. ...
- JavaScript(14)jQuery(JavaScript 库)
JavaScript 框架(库) JavaScript 高级程序设计(特别是对浏览器差异的复杂处理),通常非常困难也非常耗时.为了应对这些调整,很多的 JavaScript (helper) 库应运而 ...
- hdu3468 Treasure Hunting 二分匹配
//给一个n*m的图 //.表示空白地 //*表示有黄金 //#表示墙 //一个人须要依照A...Z..a..z的顺序以最短路径走到下一个 //每次仅仅能在他的路线上经过的地方取一块黄金 //问最多能 ...
- Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)
题目链接:http://codeforces.com/contest/448/problem/B --------------------------------------------------- ...
- MongoDB学习笔记<七>
继续MongoDB的学习 1.导出数据(中断其它操作) 把数据库test中的books集合导出 在终端下进行操作 mongoexport -d test -c books -o /home/hadoo ...
- RecyclerView借助ItemTouchHelper实现拖动和滑动删除功能
RecyclerView是官方推荐代替ListView的空间,怎样实现RecyclerView列表元素的拖动呢? 官方提供了ItemTouchHelper类使用过程例如以下: 定义ItemTouchH ...
- Gcc/MinGW/Cygwin/Msys 分别是什么?
一.GCC的历史 GCC是一个原本用于Unix-like系统下编程的编译器. 不过,现在GCC也有了许多Win32下的移植版本. 所以,也许对于许多Windows开发者来说,GCC还是一个比较陌生的东 ...