搜索二叉樹基本概念請(qǐng)看上篇博客
這兩個(gè)問題都是典型的K(key)V(value)問題,我們用KV算法解決。
結(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é)果:
結(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é)果
構(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://www.rwnh.cn/article32/ijhsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站排名、商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站、微信公眾號(hào)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容