搜索二叉树之字典实现

利用搜索二叉树判断一个单词是否拼写正确:

假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单词拼写是否正确就是在树中查找该单词是否存在(查找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;
}

运行结果:

原文地址:http://blog.51cto.com/liam2199/2091165

时间: 2024-07-30 02:12:56

搜索二叉树之字典实现的相关文章

搜索二叉树应用——简单字典实现

搜索二叉树基本概念请看上篇博客这两个问题都是典型的K(key)V(value)问题,我们用KV算法解决. 判断一个单词是否拼写正确:假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单词拼写是否正确就是在树中查找该单词是否存在(查找key是否存在). 结构声明下 typedef char* KeyType; typedef struct BSTreeNode { struct BSTreeNode *_left; struct BSTreeNode *_right; KeyType

判断一个数列是不是搜索二叉树后续遍历输出的结果

剑平面阿里被问到这个,刚开始画了下看有什么性质,乱蒙了几个都被推翻了,初始感觉就是要O(n)的,因为印象中BST的构树都可以O(nlogn)搞定.然后剑平说最后一个数肯定是根节点,一下反应过来了,就是二分出间隔点然后两边递归判断,不过这好像还是构树的思路,可以把整棵树构造出来.然后剑平说不是二分,直接遍历.不科学啊,明显的二分,然后去网上搜一下,都是遍历的,O(n^2)的吧.想了想,二分是当做合法的情况来构树的,不合法怎么判断?构造出搜索二叉树后中序遍历一下不就行了么,妥妥的O(nlogn)吧.

搜索二叉树

二叉查找树(BinarySearch Tree,也叫二叉搜索树,或称二叉排序树Binary Sort Tree)或者是一棵空树,或具有如下性质: 每个节点都有一个作为搜索依据的关键码(key),所有节点的关键码互不相同. 左子树上所有节点的关键码(key)都小于根节点的关键码(key). 右子树上所有节点的关键码(key)都大于根节点的关键码(key). 左右子树都是二叉搜索树. 二叉搜索树相关操作: (1)插入:新节点 在二叉查找树中插入新结点,要保证插入新结点后仍能满足二叉查找树的性质.插入

java递归方法建立搜索二叉树,具备查找关键字,插入新节点功能

二叉排序树的定义: 二叉排序树满足以下三个性质(BST性质): <1>若它的左子树非空,则左子树上所有节点的值均小于根节点的值 <2>若它的右子树非空,则右子树上所有节点的值均大于根节点的值 <3>左,右子树本身又各是一棵二叉排序树 根据二叉排序树的BST性质,可以说二叉排序树每个节点上的值(或称关键字)都是唯一的,并且二叉排序树以中序遍历输出的结果必然是一个有序的递增序列. 如下图所示: 用递归方法建立二叉排序树,减少了繁复的比较程序,效率较高.只需要知道每个节点的值

数据结构--‘搜索二叉树’

'二叉树'是数据结构中比较重要的一部分,这里主要讨论一下'搜索二叉树',针对'搜索二叉树的插入.删除和查找节点进行分情况讨论,希望能够帮助读者更加的理解搜索二叉树的原理. ◆搜索二叉树的性质: 1.每个节点都有一个一个作为搜索依据的关键码,所有节点的关键码都不相同. 2.左子树所有的关键码(key)都小于根节点的关键码(key). 3.右子树所有的关键码(key)都大于根节点的关键码(key). 4.左.右子树都是二叉搜索树. 实现'搜索二叉树'的节点结构可以实现为K形式,和K.V形式,若实现K

【数据结构】搜索二叉树的(递归与非递归)实现,包括:增Insert,删Remove,查Find

搜索二叉树,是二叉树一种特殊的结构. 特点: (1)每个节点都有一个关键码,并且关键码不重复. (2)左子树上的每个节点的关键码都小于根节点的关键码. (3)右子树上的每个节点的关键码都大于根节点的关键码. (4)左右子树都是搜索二叉树. 下面,为方便大家理解,我举例画一个搜索二叉树,如下: 代码见下: #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //搜索二叉树的节点结构 template&

《github一天一道算法题》:搜索二叉树接口实现大合集

读书.思考.写代码! 说明: (1)这里实现了搜索二叉树的全部常用操作 (2)限于时间和精力,实现的较为粗糙,内存泄露.成员变量访问控制.返回类型.异常安全等没有照顾的到 (3)一切实现的手段都是贴近底层操作,关注原理.以后可能对推倒重来,实现一个完备的接口系统. /********************************************* * [email protected] * 题目:二叉树接口实现大合集 * 具体:二叉树的创建.插入.最大值.最小值.前中后序递归遍历与非递

搜索二叉树的操作

搜索二叉树的数据结构定义: /*二叉搜索树的结构定义*/ typedef struct TreeNode* SearchTree; typedef struct TreeNode* Position; struct TreeNode { int Element; SearchTree Left; SearchTree Right; } 搜索二叉树的插入操作: SearchTree Insert(int x, SearchTree T) { if(T == NULL)//空树 { T = mall

栈和队列----将搜索二叉树转换成双向链表

将搜索二叉树转换成双向链表 对于BST 来说,有本身的值域,有指向左孩子和右孩子的两个指针:对于双向链表来说,有本身的值域,有指向上一个节点和下一个节点的指针.将这个BST转换成双向链表,对于每一个节点来说,原来的right指针等价于转换后的next指针,原来的left指针等价于转换后的left指针,最后返回双向链表的头节点. [解析] 用队列等容器收集二叉树 中序遍历的结果的方法.其时间复杂度是O(N),空间复杂度是O(N). 1. 生成一个队列,记为queue,按照二叉树的中序遍历的顺序,将