简单字典实现(KV问题)
搜索二叉树基本概念请看上篇博客
这两个问题都是典型的K(key)V(value)问题,我们用KV算法解决。
- 判断一个单词是否拼写正确:假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单词拼写是否正确就是在树中查找该单词是否存在(查找key是否存在)。
结构声明下
typedef char* KeyType;
typedef struct BSTreeNode
{
struct BSTreeNode *_left;
struct BSTreeNode *_right;
KeyType _key;
}BSTreeNode;
插入,查找函数代码如下:
int BSTreeNodeInsertR(BSTreeNode **tree,KeyType key) //搜索树的插入
{
int tmp = 0;
if(*tree == NULL)
{
*tree = BuyTreeNode(key);
return 0;
}
tmp = strcmp((*tree)->_key,key);
if (tmp>0)
return BSTreeNodeInsertR(&(*tree)->_left,key);
else if (tmp<0)
return BSTreeNodeInsertR(&(*tree)->_right,key);
else
return -1;
}
BSTreeNode *BSTreeNodeFindR(BSTreeNode *tree,KeyType const key) //查找
{
int tmp = 0;
if (!tree)
return NULL;
tmp = strcmp(tree->_key,key);
if (tmp > 0)
return BSTreeNodeFindR(tree->_left,key);
else if (tmp < 0)
return BSTreeNodeFindR(tree->_right,key);
else
return tree;
}
测试代码:
void TestApplication()
{
BSTreeNode *tree = NULL;
BSTreeNodeInsertR(&tree,"hello");
BSTreeNodeInsertR(&tree,"world");
BSTreeNodeInsertR(&tree,"int");
BSTreeNodeInsertR(&tree,"char");
BSTreeNodeInsertR(&tree,"float");
BSTreeNodeInsertR(&tree,"double");
printf("%s \n", BSTreeNodeFindR(tree,"char")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"double")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"int")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"float")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"hello")->_key);
printf("%s \n", BSTreeNodeFindR(tree,"world")->_key);
printf("%p \n", BSTreeNodeFindR(tree,"chars"));
printf("%p \n", BSTreeNodeFindR(tree,"str"));
}
测试结果:
2. 请模拟实现一个简单的字典(简单的中英文字典)
结构构建
typedef char* KeyType;
typedef int ValueType;
typedef struct BSTreeNode
{
struct BSTreeNode *_left;
struct BSTreeNode *_right;
KeyType _key;
ValueType _count;
}BSTreeNode;
查找函数与上面相同,插入函数有所改变。
int BSTreeNodeInsertR(BSTreeNode **tree,KeyType key, ValueType value) //搜索树的插入
{
int tmp = 0;
if(*tree == NULL)
{
*tree = BuyTreeNode(key,value);
return 0;
}
tmp = strcmp((*tree)->_key,key);
if (tmp>0)
return BSTreeNodeInsertR(&(*tree)->_left,key,value);
else if (tmp<0)
return BSTreeNodeInsertR(&(*tree)->_right,key,value);
else
return -1;
}
测试代码:
void test()
{
BSTreeNode *tree = NULL;
BSTreeNodeInsertR(&tree,"hello","你好");
BSTreeNodeInsertR(&tree,"world","世界");
BSTreeNodeInsertR(&tree,"char","字符");
BSTreeNodeInsertR(&tree,"int","整形");
BSTreeNodeInsertR(&tree,"float","浮点型");
printf("%s \n", BSTreeNodeFindR(tree,"char")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"int")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"float")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"hello")->_value);
printf("%s \n", BSTreeNodeFindR(tree,"world")->_value);
printf("%p \n", BSTreeNodeFindR(tree,"double"));
}
测试结果
构建测试案例尽量构建全面,防止特殊情况被忽略。
简单字典实现(KV问题)的更多相关文章
- [Swust OJ 648]--简单字典(数位dp)
题目链接:http://acm.swust.edu.cn/problem/0648/ Time limit(ms): 1000 Memory limit(kb): 65535 有这样一本字典,它每 ...
- POJ2503(Babelfish)--简单字典树
思路:就是用一个字典树翻译单词的问题,我们用题目中给出的看不懂的那些单词建树,这样到每个单词的叶子结点中存放原来对应的单词就好. 这样查询到某个单词时输出叶子结点存的就行,查不到就"en&q ...
- 华为2015 简单 字典输入法 java
题目摘自http://blog.csdn.net/dongyi91/article/details/38639915 写了2个小时,水平太菜了 入法的编码原理为:根据已有编码表,当输入拼音和数字后输出 ...
- hdu 1251简单字典树
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; struc ...
- 利用Runtime实现简单的字典转模型
前言 我们都知道,开发中会有这么一个过程,就是将服务器返回的数据转换成我们自己定义的模型对象.当然服务器返回的数据结构有xml类型的,也有json类型的.本文只讨论json格式的. 大家在项目中一般是 ...
- (python)数据结构---字典
一.描述 由键值key-value组成的数据的集合 可变.无序的,key不可以重复 字典的键key要可hash(列表.字典.集合不可哈希),不可变的数据结构是可哈希的(字符串.元组.对象.bytes) ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- python 根据字典中的key,value进行排序
#coding=utf-8 import requests,json,collections,base64,datetime def sort(datas): data=json.dumps(data ...
- Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict)
Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.封装和结构 #!/usr/bin/env pytho ...
随机推荐
- jQuery Ajax url使用方式
jQuery Ajax的使用场景: 页面需要通过后台逻辑,但只需要局部刷新以显示新的内容. jQuery Ajax url使用方式1.servlet方式: 需要在struts.xml中写一个actio ...
- zabbix日常监控项java(四又分之一)
因zabbix自带监控JMX的模板通用性差的问题,需要我们自己新建一个属于自己的模板,于是本文就出现了.... 大部分都是参考网上其他博主的文档,在此作为梳理.总结:以方便自己日后使用查询.
- java+redis+lua生成自动增长的ID序列号
1.编写lua脚本用于生成主键ID序列号,内容如下 local key = tostring(KEYS[1]); local count = tonumber(KEYS[2]); local date ...
- 团队作业1——团队展示&教辅宝
1.队名:PHILOSOPHER 2.队员学号: [组长]金盛昌(201421122043).刘文钊(20142112255).陈笑林(201421122042). 张俊逸(201421122044) ...
- 【笔记】JS脚本为什么要放在body最后面以及async和defer的异同点
1.没有defer或async 浏览器遇到脚本的时候会暂停渲染并立即加载执行脚本(外部脚本),"立即"指的是在渲染该 script 标签之下的文档元素之前,也就是说不等待后续载入的 ...
- python3 装饰器全解
本章结构: 1.理解装饰器的前提准备 2.装饰器:无参/带参的被装饰函数,无参/带参的装饰函数 3.装饰器的缺点 4.python3的内置装饰器 5.本文参考 理解装饰器的前提:1.所有东西都是对象( ...
- gp数据库停止
greenplum是2(master)+7(segment)的集群规模 系统刚准备上线,是用来做统计数据库的,正在帮忙一个hadoop集群核对其数据的准确性,在这个greenplum库中入了清单数据 ...
- JdkDynamicAopProxy-笔记
这个接口的继承体系图: 一.AopProxy InvocationHandler就不说了,看看AopProxy的源码. /** * Delegate interface for a configure ...
- Python3中内置类型bytes和str用法及byte和string之间各种编码转换
Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str ...
- Jenkins + GitLab 通过 Webhook 自动触发构建爬坑记录
前言 在局域网搭建了一个Jenkins服务,用于自动构建和发布,在调通了构建程序之后,想使用内网的GitLab的Webhook功能触发代码推送事件,然后进行自动构建.后来发现在GitLab上做测试 ...