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

这两个问题都是典型的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. 【FLEX教程】#007 如何让JS调用SWF里的方法

    HTML中,JS如何调用SWF里面已经封装好的代码呢? 有一些事情Flex没办法实现的,需要通过调用JS来实现. eg: 当浏览器窗体关闭的时候,弹出一个对话框,提示用户是否退出?或者是否保存当前的操 ...

  2. SDN2017 第一次实验作业

    1. 安装mininet 从github上获取Mininet源码并运行安装脚本 $ git clone git://github.com/mininet/mininet $ ./mininet/uti ...

  3. Hibernate学习笔记一之注解

    1.@Entiy  实体类注解 2.@Table  映射表 (name=“”)表名 3.@Coulmn @Column( name="columnName";            ...

  4. 给 Virtualbox 中 Ubuntu 系统设置静态 IP

    虚拟机网络选择 桥接网卡 模式. 主要涉及两个步骤: 1. 修改 /etc/network/interfaces 文件: 2. 修改 dns : 第一步,修改 interfaces 文件: sudo ...

  5. 2、JVM--Java内存区域与内存溢出异常

    Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人却想出来. 2.1.概述 对于从事C.C++程序开发的开发人员来说,在内存管理领域,他们既是拥有最高 ...

  6. c++ 堆和栈以及区别

    c++中内存分成5个区:堆.栈.自由存储区.全局\静态存储区.常量存储区 栈是一种连续存储的数据结构,具有先进后出的性质.堆是一种非连续的树形存储数据结构,每个节点有一个值,整棵树是经过排序的,特点是 ...

  7. Python自动化之modelform和原生ajax

    modelform验证 `views.py` from django.shortcuts import render,HttpResponse from app01 import models fro ...

  8. windows禁用端口命令

    netstat -aon|findstr 1099 找出占用1099端口的进程 然后关闭占用该端口的进程:taskkill -f -pid 3756(进程id)

  9. Mysql 多实例实施步骤

    基本理论:利用同一套安装程序,不同配置文件,不同启动程序,不同数据目录.有公用资源,也有私有资源. 实现步骤: 1.正常安装mysql,二进制安装或者编译安装. 2.创建mysql多实例总目录,总目录 ...

  10. vmvare安装系统提示vmci.sys 版本不正确解决方法

    无法获取vmci驱动程序版本:参数不正确解决 无法获取vmci驱动程序版本:参数不正确. 驱动程序vmci.sys版本不正确. 解决办法: 1,创建好虚拟机之后,别打开电源,然后到建好的虚拟机文件夹里 ...