大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
搜索二叉树基本概念请看上篇博客
这两个问题都是典型的K(key)V(value)问题,我们用KV算法解决。
成都创新互联于2013年创立,是专业互联网技术服务公司,拥有项目网站设计制作、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元惠济做网站,已为上家服务,为惠济各地企业和个人服务,联系电话:18980820575
结构声明下
typedef char* KeyType;
typedef struct BSTreeNode
{
struct BSTreeNode *_left;
struct BSTreeNode *_right;
KeyType _key;
}BSTreeNode;
插入,查找函数代码如下:
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"));
}
测试结果:
构建测试案例尽量构建全面,防止特殊情况被忽略。