搜索二叉树基本概念请看上篇博客

这两个问题都是典型的K(key)V(value)问题,我们用KV算法解决。

  1. 判断一个单词是否拼写正确:假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单词拼写是否正确就是在树中查找该单词是否存在(查找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问题)的更多相关文章

  1. [Swust OJ 648]--简单字典(数位dp)

    题目链接:http://acm.swust.edu.cn/problem/0648/ Time limit(ms): 1000 Memory limit(kb): 65535   有这样一本字典,它每 ...

  2. POJ2503(Babelfish)--简单字典树

    思路:就是用一个字典树翻译单词的问题,我们用题目中给出的看不懂的那些单词建树,这样到每个单词的叶子结点中存放原来对应的单词就好. 这样查询到某个单词时输出叶子结点存的就行,查不到就"en&q ...

  3. 华为2015 简单 字典输入法 java

    题目摘自http://blog.csdn.net/dongyi91/article/details/38639915 写了2个小时,水平太菜了 入法的编码原理为:根据已有编码表,当输入拼音和数字后输出 ...

  4. hdu 1251简单字典树

    #include<stdio.h> #include<iostream> #include<string.h> using namespace std; struc ...

  5. 利用Runtime实现简单的字典转模型

    前言 我们都知道,开发中会有这么一个过程,就是将服务器返回的数据转换成我们自己定义的模型对象.当然服务器返回的数据结构有xml类型的,也有json类型的.本文只讨论json格式的. 大家在项目中一般是 ...

  6. (python)数据结构---字典

    一.描述 由键值key-value组成的数据的集合 可变.无序的,key不可以重复 字典的键key要可hash(列表.字典.集合不可哈希),不可变的数据结构是可哈希的(字符串.元组.对象.bytes) ...

  7. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

  8. python 根据字典中的key,value进行排序

    #coding=utf-8 import requests,json,collections,base64,datetime def sort(datas): data=json.dumps(data ...

  9. Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict)

    Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.封装和结构 #!/usr/bin/env pytho ...

随机推荐

  1. ms17-010漏洞利用教程

    ms17-010 漏洞利用并拿下服务器教程 攻击环境: 攻击机win2003 ip:192.168.150.129 Window2003 Python环境及工具 攻击机kali: ip:192.168 ...

  2. C++ Circle

    作业链接 https://github.com/How-Come/object-oriented/tree/master/Circle

  3. EclEmma安装与使用

    安装 EclEmma 插件的过程和大部分 Eclipse 插件相同,我们既可以通过 Eclipse 标准的 Update 机制来远程安装 EclEmma 插件(图 1),也可以从站点(参阅参考资源)下 ...

  4. CORS跨域与Nginx反向代理跨域优劣对比

    最近写了一些关于前后端分离项目之后,跨域相关方案的基本原理和常见误区的帖子,主要包括CORS和Nginx反向代理.这两种方案项目中都有在用,各有优缺,关于具体使用哪种方案,大家的观点也不大一致,本文主 ...

  5. openfalcon-0.2 配置

    hbs 配置文件 { "debug": true, "database": "openfalcon:123456@tcp(172.16.230.188 ...

  6. Robot Framework自动化测试---Selenium API

    一.浏览器驱动 通过不同的浏览器执行脚本. Open Browser Htpp://www.xxx.com chrome 浏览器对应的关键字: firefox FireFox ff internete ...

  7. webbench安装和简单使用

    一.安装流程 wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar ...

  8. hdfs的java接口简单示例

    public class HDFSDemo { private FileSystem fs = null; @Before public void init() throws IOException, ...

  9. CSDN博客清理缓存

    CSDN博客清理缓存 清着清着 訪问量少了将近2w 积分少了几百 唉 你这是要闹什么呀 说9点结束 如今都几点了

  10. C#窗口皮肤制作(二):创建窗口库项目以及最小化、最大化、关闭button的实现

    非常高兴有朋友关注这篇博客,同一时候也十分抱歉让关注的朋友久等了,隔上一篇博客也有3个月没有更新,主要是因为3月份辞职,4月份初离职到期离开了北京高德,来到了上海张江.眼下新工作也处于熟悉其中,希望大 ...