搜索二叉樹應(yīng)用——簡單字典實(shí)現(xiàn)-創(chuàng)新互聯(lián)

搜索二叉樹基本概念請看上篇博客
這兩個(gè)問題都是典型的K(key)V(value)問題,我們用KV算法解決。

公司專注于為企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、微信公眾號開發(fā)、電子商務(wù)商城網(wǎng)站建設(shè),小程序設(shè)計(jì),軟件定制網(wǎng)站制作等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會仔細(xì)了解各客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,成都創(chuàng)新互聯(lián)公司更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。
  1. 判斷一個(gè)單詞是否拼寫正確:假設(shè)把所有單詞都按照搜索樹的性質(zhì)插入到搜索二叉樹中,我們判斷一個(gè)單詞拼寫是否正確就是在樹中查找該單詞是否存在(查找key是否存在)。

結(jié)構(gòu)聲明下

typedef char* KeyType;

typedef struct BSTreeNode 
{
    struct BSTreeNode *_left;
    struct BSTreeNode *_right;

    KeyType _key;
}BSTreeNode;

插入,查找函數(shù)代碼如下:

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"));
}

測試結(jié)果:
搜索二叉樹應(yīng)用——簡單字典實(shí)現(xiàn)

  1. 請模擬實(shí)現(xiàn)一個(gè)簡單的字典(簡單的中英文字典)

結(jié)構(gòu)構(gòu)建

typedef char* KeyType;
typedef int ValueType;

typedef struct BSTreeNode 
{
    struct BSTreeNode *_left;
    struct BSTreeNode *_right;

    KeyType _key;
    ValueType _count;
}BSTreeNode;

查找函數(shù)與上面相同,插入函數(shù)有所改變。

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","浮點(diǎn)型");

    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"));
}

測試結(jié)果
搜索二叉樹應(yīng)用——簡單字典實(shí)現(xiàn)

構(gòu)建測試案例盡量構(gòu)建全面,防止特殊情況被忽略。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文標(biāo)題:搜索二叉樹應(yīng)用——簡單字典實(shí)現(xiàn)-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://muchs.cn/article32/ijhsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站排名、商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站、微信公眾號、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站