二叉搜尋樹應用-判斷一個單詞是否拼寫正確,實現簡單字典

龍躍十二發表於2018-02-27

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

測試結果
中英文字典

構建測試案例儘量構建全面,防止特殊情況被忽略。

相關文章