zoukankan      html  css  js  c++  java
  • 简单字典实现(KV问题)

    搜索二叉树基本概念请看上篇博客
    这两个问题都是典型的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 
    ", BSTreeNodeFindR(tree,"char")->_key);
        printf("%s 
    ", BSTreeNodeFindR(tree,"double")->_key);
        printf("%s 
    ", BSTreeNodeFindR(tree,"int")->_key);
        printf("%s 
    ", BSTreeNodeFindR(tree,"float")->_key);
        printf("%s 
    ", BSTreeNodeFindR(tree,"hello")->_key);
        printf("%s 
    ", BSTreeNodeFindR(tree,"world")->_key);
        printf("%p 
    ", BSTreeNodeFindR(tree,"chars"));
        printf("%p 
    ", 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 
    ", BSTreeNodeFindR(tree,"char")->_value);
        printf("%s 
    ", BSTreeNodeFindR(tree,"int")->_value);
        printf("%s 
    ", BSTreeNodeFindR(tree,"float")->_value);
        printf("%s 
    ", BSTreeNodeFindR(tree,"hello")->_value);
        printf("%s 
    ", BSTreeNodeFindR(tree,"world")->_value);
        printf("%p 
    ", BSTreeNodeFindR(tree,"double"));
    }
    

    测试结果
    中英文字典

    构建测试案例尽量构建全面,防止特殊情况被忽略。

  • 相关阅读:
    selenium—用NoSuchElementException异常判断页面元素是否存在
    CentOS7 Nginx安装及配置反向代理
    CentOS7 安装 jexus-5.8.2-x64
    Windows Server 2008 R2远程协助选项 灰色
    IIS8.5 Error Code 0x8007007e HTTP 错误 500.19的解决方法
    记一次 windows server 2012R2 上安装 MSSQL2005 及网站发布
    记一次《系统集成实施的相关知识》培训自己感悟
    MySql 远程连接的条件
    CentOS7 下安装mysql历程
    VirtualBox虚拟机网络设置(四种方式)
  • 原文地址:https://www.cnblogs.com/zhonglongbo/p/8480618.html
Copyright © 2011-2022 走看看