1. 程式人生 > >搜索二叉樹之字典實現

搜索二叉樹之字典實現

搜索二叉樹、源碼分析、中英文字典


利用搜索二叉樹判斷一個單詞是否拼寫正確:

假設把所有單詞都按照搜索樹的性質插入到搜索二叉樹中,我們判斷一個單詞拼寫是否正確就是在樹中查找該單詞是否存在(查找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 ) //創建節點
{
	BSTreeNode *node = (BSTreeNode*)malloc(sizeof(BSTreeNode));
	assert(node);

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

	return node;
}

//插入、查找函數
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;
}

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

int main(void)
{
	TestApplication();

	char c = getchar();
	return 0;
}


運行結果:

技術分享圖片


實現簡單的中英文字典


/*****************************************
*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 ) //創建節點
{
	BSTreeNode *node = (BSTreeNode*)malloc(sizeof(BSTreeNode));
	assert(node);

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

	return node;
}

//插入、查找函數
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;
}

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

int main(void)
{
	TestApplication();

	char c = getchar();
	return 0;
}

運行結果:

技術分享圖片


搜索二叉樹之字典實現