举例:模拟实现一个简单的中英互译的字典
SBT_Topic_2.h
#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
typedef char * _SBTKeyVType;
typedef char * _SBTValueKType;
typedef struct SearchBinaryTreeNodeKV
{
struct SearchBinaryTreeNodeKV * _left;
struct SearchBinaryTreeNodeKV * _right;
_SBTKeyVType _data;
_SBTValueKType _value;
}SearchBinaryTreeNodeKV, *pSearchBinaryTreeNodeKV;
pSearchBinaryTreeNodeKV BuySearchBinaryTreeNodeKV(_SBTKeyVType x, _SBTValueKType y);
int InsertSearchBinaryTreeRKV(pSearchBinaryTreeNodeKV * tree, _SBTKeyVType x, _SBTValueKType y);
int RemoveSearchBinaryTreeRKV(pSearchBinaryTreeNodeKV * tree, _SBTKeyVType x);
pSearchBinaryTreeNodeKV FindSearchBinaryTreeRKV(pSearchBinaryTreeNodeKV * tree, _SBTKeyVType x);
SBT_Topic_2.c
#include"SBT_Topic_2.h"
pSearchBinaryTreeNodeKV BuySearchBinaryTreeNodeKV(_SBTKeyVType x, _SBTValueKType y)
{
pSearchBinaryTreeNodeKV node = (pSearchBinaryTreeNodeKV)malloc(sizeof(SearchBinaryTreeNodeKV));
assert(node);
node->_data = x;
node->_value = y;
node->_left = NULL;
node->_right = NULL;
return node;
}
int InsertSearchBinaryTreeRKV(pSearchBinaryTreeNodeKV * tree, _SBTKeyVType x, _SBTValueKType y)
{
if (*tree == NULL)
{
*tree = BuySearchBinaryTreeNodeKV(x, y);
return 1;
}
if (strcmp((*tree)->_data, x) > 0)
{
return InsertSearchBinaryTreeRKV(&(*tree)->_left, x, y);
}
else if (strcmp((*tree)->_data, x) < 0)
{
return InsertSearchBinaryTreeRKV(&(*tree)->_right, x, y);
}
else
{
return 0;
}
}
int RemoveSearchBinaryTreeRKV(pSearchBinaryTreeNodeKV * tree, _SBTKeyVType x)
{
if (*tree == NULL)
{
return 0;
}
if (strcmp((*tree)->_data, x) > 0)
{
return RemoveSearchBinaryTreeRKV(&(*tree)->_left, x);
}
else if (strcmp((*tree)->_data, x) < 0)
{
return RemoveSearchBinaryTreeRKV(&(*tree)->_right, x);
}
else
{
pSearchBinaryTreeNodeKV del = *tree;
if ((*tree)->_left == NULL)
{
*tree = (*tree)->_right;
free(del);
}
else if ((*tree)->_right == NULL)
{
*tree = (*tree)->_left;
free(del);
}
else
{
pSearchBinaryTreeNodeKV replace = (*tree)->_right;
while (replace->_left)
{
replace = replace->_left;
}
(*tree)->_data = replace->_data;
return RemoveSearchBinaryTreeRKV(&(*tree)->_right, replace->_data);
}
}
return 1;
}
pSearchBinaryTreeNodeKV FindSearchBinaryTreeRKV(pSearchBinaryTreeNodeKV * tree, _SBTKeyVType x)
{
if (*tree == NULL)
{
return NULL;
}
if (strcmp((*tree)->_data, x) > 0)
{
return FindSearchBinaryTreeRKV(&(*tree)->_left, x);
}
else if (strcmp((*tree)->_data, x) < 0)
{
return FindSearchBinaryTreeRKV(&(*tree)->_right, x);
}
else
{
return *tree;
}
}
Test.c
#include"SBT_Topic_2.h"
void InOrderSearchBinaryTreeKV(pSearchBinaryTreeNodeKV * tree)
{
if (*tree == NULL)
{
return NULL;
}
InOrderSearchBinaryTreeKV(&(*tree)->_left);
printf("%s %s\n", (*tree)->_data, (*tree)->_value);
InOrderSearchBinaryTreeKV(&(*tree)->_right);
}
void testSBT_Topic_2()
{
char str[10] = {'0'};
pSearchBinaryTreeNodeKV tree = NULL;
pSearchBinaryTreeNodeKV node = NULL;
InsertSearchBinaryTreeRKV(&tree, "tree", "树");
InsertSearchBinaryTreeRKV(&tree, "sort", "排序");
InsertSearchBinaryTreeRKV(&tree, "binary", "二分");
InsertSearchBinaryTreeRKV(&tree, "return", "返回");
InsertSearchBinaryTreeRKV(&tree, "hash", "哈希");
InsertSearchBinaryTreeRKV(&tree, "list", "链表");
InOrderSearchBinaryTreeKV(&tree);
while (1)
{
printf("Please Input:");
scanf_s("%s", str, 10);
if (strcmp(str, "-1") == 0)
{
break;
}
node = FindSearchBinaryTreeRKV(&tree, str);
if (node)
{
printf("%s:%s\n", node->_data, node->_value);
}
else
{
printf("Not Found!\n");
}
}
}
int main()
{
testSBT_Topic_2();
return 0;
}