1. 程式人生 > >簡單字典實現(KV問題)

簡單字典實現(KV問題)

IT 我們 truct ins cnblogs ret 算法 ati strcmp

搜索二叉樹基本概念請看上篇博客
這兩個問題都是典型的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"));
}

測試結果:
技術分享圖片

  1. 請模擬實現一個簡單的字典(簡單的中英文字典)

結構構建

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問題)