搜索二叉樹(shù)之字典實(shí)現(xiàn)

    利用搜索二叉樹(shù)判斷一個(gè)單詞是否拼寫(xiě)正確:

古丈ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!

    假設(shè)把所有單詞都按照搜索樹(shù)的性質(zhì)插入到搜索二叉樹(shù)中,我們判斷一個(gè)單詞拼寫(xiě)是否正確就是在樹(shù)中查找該單詞是否存在(查找key是否存在)。

/*****************************************
*Date:2018年3月26日14:42:54
*Author: Meng
*WebSite:msyci.com
*QQ:3515955122
*****************************************/

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#include<assert.h>

typedef char* KeyType;
typedef char* ValueType;

typedef struct BSTreeNode
{
	struct BSTreeNode*  _left;
	struct BSTreeNode*  _right;
	KeyType  _key;
	ValueType _value;
}BSTreeNode;

BSTreeNode *BuyTreeNode(KeyType x,ValueType value ) //創(chuàng)建節(jié)點(diǎn)
{
	BSTreeNode *node = (BSTreeNode*)malloc(sizeof(BSTreeNode));
	assert(node);

	node->_key = x;
	node->_left = NULL;
	node->_right = NULL;
	node->_value = value;

	return node;
}

//插入、查找函數(shù)
int BSTreeNodeInsertR(BSTreeNode **tree,KeyType key, ValueType value) //搜索樹(shù)的插入
{
	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;
}

BSTreeNode*  BSTreeNodeFindR(BSTreeNode* tree, KeyType  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,"China","中國(guó)");
		BSTreeNodeInsertR(&tree,"score","成績(jī)");
		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,"China")->_value);
		printf("%s \n", BSTreeNodeFindR(tree,"score")->_value);
		printf("%p \n", BSTreeNodeFindR(tree,"double"));
		
}

int main(void)
{
	TestApplication();

	char c = getchar();
	return 0;
}

運(yùn)行結(jié)果:

搜索二叉樹(shù)之字典實(shí)現(xiàn)

實(shí)現(xiàn)簡(jiǎn)單的中英文字典


/*****************************************
*Date:2018年3月26日15:35:07
*Author: Meng
*WebSite:msyci.com
*QQ:3515955122
*****************************************/

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#include<assert.h>

typedef char* KeyType;
typedef char* ValueType;

typedef struct BSTreeNode
{
	struct BSTreeNode*  _left;
	struct BSTreeNode*  _right;
	KeyType  _key;
	ValueType _value;
}BSTreeNode;

BSTreeNode *BuyTreeNode(KeyType x,ValueType value ) //創(chuàng)建節(jié)點(diǎn)
{
	BSTreeNode *node = (BSTreeNode*)malloc(sizeof(BSTreeNode));
	assert(node);

	node->_key = x;
	node->_left = NULL;
	node->_right = NULL;
	node->_value = value;

	return node;
}

//插入、查找函數(shù)
int BSTreeNodeInsertR(BSTreeNode **tree,KeyType key, ValueType value) //搜索樹(shù)的插入
{
	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;
}

BSTreeNode*  BSTreeNodeFindR(BSTreeNode* tree, KeyType  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,"China","中國(guó)");
		BSTreeNodeInsertR(&tree,"score","成績(jī)");
		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,"China")->_value);
		printf("%s \n", BSTreeNodeFindR(tree,"score")->_value);
		printf("%p \n", BSTreeNodeFindR(tree,"double"));
		
}

int main(void)
{
	TestApplication();

	char c = getchar();
	return 0;
}

運(yùn)行結(jié)果:

搜索二叉樹(shù)之字典實(shí)現(xiàn)

網(wǎng)頁(yè)題目:搜索二叉樹(shù)之字典實(shí)現(xiàn)
鏈接分享:http://bm7419.com/article0/igosoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、標(biāo)簽優(yōu)化、微信小程序、網(wǎng)站收錄App設(shè)計(jì)、商城網(wǎng)站

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司