搜索二叉树之字典实现
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,利用搜索二叉树判断一个单词是否拼写正确:假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单词拼写是否正确就是在树中查找该单词是否存在(查找key是否存在)。/*************
千家信息网最后更新 2025年02月02日搜索二叉树之字典实现
利用搜索二叉树判断一个单词是否拼写正确:
假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单词拼写是否正确就是在树中查找该单词是否存在(查找key是否存在)。
/******************************************Date:2018年3月26日14:42:54*Author: Meng*WebSite:msyci.com*QQ:3515955122*****************************************/# include# include # include #include 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# include # include #include 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;}
运行结果:
搜索
单词
函数
字符
成绩
浮点
结果
节点
中国
运行
字典
中英
就是
性质
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
谷歌网络安全倡议
电视台机房网络安全工作总结
图形数据库软件中文版
社旗县网络安全宣传周
svn服务器管理平台
服务器sata盘支持热插拔吗
计算机网络技术职业就业分析
互联网科技讲评
山西文档软件开发erp
30岁了自学软件开发什么最快
控制台打开数据库的命令是
网络安全法要求贯穿
数据库系统概论作业
轻量应用服务器
网络安全中sws是什么协议
多级数据库是什么意思
软件开发计税方法
最近网络安全专家
研究生 数据库 工作
网络安全相关的软考认证
php投票的数据库格式
网络安全为人民的手公报
淘宝上的数据库是mysql吗
河南微创网络技术有限公司
怎样学好网络安全专业
武汉好商汇网络技术有限公司
服务器升级扩容一般多久
移动服务器集成
电脑文件数据库管理
南宁青秀区公安局网络技术岗