/**
* 实现单词补全功能
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h> #define MAX_CHILD 26 #define error(...) \
logger(stderr, __LINE__, __VA_ARGS__) #define notice(...) \
logger(stdout, __LINE__, __VA_ARGS__) /**
* 定义trie树节点
*/
typedef struct node_s{
int count;
struct node_s *child[MAX_CHILD];
char words[];
} node_t; /**
* 日志
*/
void logger(FILE *fp, int line, const char *fmt, ...){
fprintf(fp, "[line]:%d | ", line);
va_list ap;
va_start(ap, fmt);
vfprintf(fp, fmt, ap);
va_end(ap);
fprintf(fp, "\n");
} /**
* 创建节点
*/
node_t *createNode(){
node_t *node = (node_t *)calloc(, sizeof(node_t));
if(node == NULL){
error("createNode fail, errno[%d]", errno);
}
} /**
* 添加
*/
int insert(node_t *root, char *words){
if(!root || words[] == '\0'){
error("insert fail, root or words is null");
return -;
}
node_t *node = root;
node_t *tmp;
char *s = words;
while(*s != '\0'){
if(node->child[*s - 'a'] == NULL){
tmp = createNode();
if(tmp == NULL){
goto err;
}
node->child[*s - 'a'] = tmp;
}
node = node->child[*s - 'a'];
s++;
}
node->count++;
memcpy(node->words, words, strlen(words));
notice("insert success, words = %s", words);
return ;
err:
return -;
} /**
* 搜索指定单词
*/
int search(node_t *root, char *words){
if(!root || words[] == '\0'){
error("search fail, root or words is null");
return -;
}
char *s = words;
node_t *node = root;
while(*s != '\0'){
if(node->child[*s - 'a'] == NULL){
break;
}
node = node->child[*s - 'a'];
s++;
}
if(*s == '\0'){
if(node->count == ){
printf("没有搜索到这个字符串,但是它是某个字符串的前缀\n");
}else{
printf("搜索到此字符串,出现次数为:%d\n", node->count);
} searchChild(node);
}else{
printf("没有搜索到这个字符串\n");
}
} /**
* 遍历出来的子节点是排序后的
*/
void searchChild(node_t *node){
if(!node){
error("searchChild fail, node is null");
return;
}
int i;
if(node->count){
printf("%s\n", node->words);
}
for(i = ; i < MAX_CHILD; i++){
if(node->child[i]){
searchChild(node->child[i]);
}
}
} /**
* 递归回收内存
*/
void del(node_t *root){
if(!root){
error("del fail, root is null");
return;
} int i;
for(i = ; i < MAX_CHILD; i++){
if(root->child[i]){
del(root->child[i]);
}
}
free(root); } int main(){
char *str = "jimmy";
node_t *root = createNode();
if(!root){
return -;
} //insert(root, str);
//search(root, "j");
FILE *fp = fopen("one.txt", "r");
if(!fp){
perror("open file fail");
}
char words[];
while(!feof(fp)){
fgets(words, sizeof(words), fp);
//去掉回车符
words[strlen(words) - ] = '\0';
insert(root, words);
memset(words, , sizeof(words));
} while(scanf("%s", words)){
search(root, words);
} del(root);
}

运行效果如下:

通过trie树实现单词自动补全的更多相关文章

  1. 通过trie树单词自动补全(二)

    经常使用iciba进行单词查询, 关于他的搜索建议是通过单词前缀做的索引, 所以自己想动手实现下, 当然如果借助mysql的话,一条sql语句就能实现, 网上查询了下trie正适合做这个,所以通过C语 ...

  2. 关于在php中变量少写了一个$和页面不断转圈的问题排查和vim的自动补全方式

    php中的所有变量都是页面级的, 即任何一个页面, 最多 都只能在一个文件 : 当前页面内使用, 不存在跨 文件/ 跨页面的 作用域的变量! 因此, 即使是 $GLOBALS 这个变量, 虽然叫全局 ...

  3. 我的Vim配置(自动补全/树形文件浏览)

    配置文件的下载路径在这里  http://files.cnblogs.com/files/oloroso/vim.configure.xz.gz 这实际上是一个 xz 格式的文件,添加的 gz 文件后 ...

  4. IntelliJ IDEA 设置代码提示或自动补全的快捷键 (附IntelliJ IDEA常用快捷键)

    修改方法如下: 点击 文件菜单(File) –> 点击 设置(Settings- Ctrl+Alt+S), –> 打开设置对话框. 在左侧的导航框中点击 KeyMap. 接着在右边的树型框 ...

  5. [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  6. [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  7. Redis 实战 —— 08. 实现自动补全、分布式锁和计数信号量

    自动补全 P109 自动补全在日常业务中随处可见,应该算一种最常见最通用的功能.实际业务场景肯定要包括包含子串的情况,其实这在一定程度上转换成了搜索功能,即包含某个子串的串,且优先展示前缀匹配的串.如 ...

  8. vim自动补全功能

    1.首先下载一个插件:ctags 输入:sudo apt-get install ctags 2.Ctrl+n进行单词的自动补全

  9. Bash的自动补全

    内置补全命令 Bash内置两个补全命令,分别是compgen和complete.compgen命令根据不同的参数,生成匹配单词的候选补全列表,例子如下: monster@monster-Z:~$ co ...

随机推荐

  1. AJAX与PHP(PHP笔记)--动态验证用户名

    在PHP基础的学习过程中经常会遇到对页面的局部刷新. 比如说,我们在填写用户名的同时,对数据库中的信息进行验证,检查是否充分. 这时就要用到AJAX实现页面的动态加载. 下面例子是简单的PHP与AJA ...

  2. JS图片自动和可控的轮播切换特效

    点击这里查看效果:http://hovertree.com/texiao/js/1.htm HTML文件代码如下: <!DOCTYPE html> <html xmlns=" ...

  3. checkbox & radio 的对齐问题

    不仅不同浏览器不同,不同的字体,不同的文字大小也会表现不一样. 重置 form checkbox & radio 因为不同浏览器解析不一样,有些是默认margin,有些是默认padding,还 ...

  4. JavaScript基本语法(五)

    BOM 浏览器对象模型 BOM (浏览器对象模型),它提供了与浏览器窗口进行交互的对象. 一.window对象 Window对象表示整个浏览器窗口. 所有浏览器都支持 window 对象.它表示浏览器 ...

  5. 64位win7+vs2010编译.net3.5以前的版本问题

    一般编译会出现 1.“ResGen.exe”已退出,代码为2 问题处理 2.“错误 2 “LC.exe”已退出,代码为 -1. NBGIS.MainGIS” 3.“未能加载文件或程序集“ESRI.Ar ...

  6. iOS 支付宝的使用

    支付宝相关资源下载地址:支付宝开放平台  在移动支付功能处下载. 一.使用官方的Demo 需要配置基本信息: 打开“APViewController.m”文件,对以下三个参数进行编辑. 二.集成支付宝 ...

  7. android 性能分析案例

    本章以实际案例分析在android开发中,性能方面的优化和处理.设计到知识点有弱引用,memory monitor,Allocation Tracker和leakcanary插件. 1.测试demo ...

  8. CSS3-06 样式 5

    浮动(Float) 关于浮动,要说的可能就是:一个设置了浮动的元素会尽量向左移动或向右移动,且会对其后的元素造成影响,其后的元素会排列在其围绕在其左下或右下部.似乎就这么简单,但是在实际开发中,它应用 ...

  9. Windows on Device 项目实践 5 - 姿态控制灯制作

    在前面几篇文章中,我们学习了如何利用Intel Galileo开发板和Windows on Device来设计并完成PWM调光灯.感光灯.火焰报警器和智能风扇的制作,涉及到了火焰传感器.DC直流电机. ...

  10. Write on ……… failed: 112(failed to retrieve text for this error. Reason: 15105)

    早上检查数据库的备份邮件时,发现一台Microsoft SQL Server 2008 R2 (SP2)数据库的Maintenance Report有错误 在SSMS里面执行Exec YourSQLD ...