举例:判断一个单词是否拼写正确
SBT_Topic_1.h
#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
typedef char * _SBTKeyType;
typedef struct SearchBinaryTreeNodeK
{
struct SearchBinaryTreeNodeK * _left;
struct SearchBinaryTreeNodeK * _right;
_SBTKeyType _key;
}SearchBinaryTreeNodeK, *pSearchBinaryTreeNodeK;
pSearchBinaryTreeNodeK BuySearchBinaryTreeNode(_SBTKeyType x);
int InsertSearchBinaryTreeR(pSearchBinaryTreeNodeK * tree, _SBTKeyType x);
int RemoveSearchBinaryTreeR(pSearchBinaryTreeNodeK * tree, _SBTKeyType x);
pSearchBinaryTreeNodeK FindSearchBinaryTreeR(pSearchBinaryTreeNodeK * tree, _SBTKeyType x);
SBT_Topic_1.c
#include"SBT_Topic_1.h"
pSearchBinaryTreeNodeK BuySearchBinaryTreeNode(_SBTKeyType x)
{
pSearchBinaryTreeNodeK node = (pSearchBinaryTreeNodeK)malloc(sizeof(SearchBinaryTreeNodeK));
assert(node);
node->_key = x;
node->_left = NULL;
node->_right = NULL;
return node;
}
int InsertSearchBinaryTreeR(pSearchBinaryTreeNodeK * tree, _SBTKeyType x)
{
if (*tree == NULL)
{
*tree = BuySearchBinaryTreeNode(x);
return 1;
}
if (strcmp((*tree)->_key, x) > 0)
{
return InsertSearchBinaryTreeR(&(*tree)->_left, x);
}
else if (strcmp((*tree)->_key, x) < 0)
{
return InsertSearchBinaryTreeR(&(*tree)->_right, x);
}
else
{
return 0;
}
}
int RemoveSearchBinaryTreeR(pSearchBinaryTreeNodeK * tree, _SBTKeyType x)
{
if (*tree == NULL)
{
return 0;
}
if (strcmp((*tree)->_key, x) > 0)
{
return RemoveSearchBinaryTreeR(&(*tree)->_left, x);
}
else if (strcmp((*tree)->_key, x) < 0)
{
return RemoveSearchBinaryTreeR(&(*tree)->_right, x);
}
else
{
pSearchBinaryTreeNodeK del = *tree;
if ((*tree)->_left == NULL)
{
*tree = (*tree)->_right;
free(del);
}
else if ((*tree)->_right == NULL)
{
*tree = (*tree)->_left;
free(del);
}
else
{
pSearchBinaryTreeNodeK replace = (*tree)->_right;
while (replace->_left)
{
replace = replace->_left;
}
(*tree)->_key = replace->_key;
return RemoveSearchBinaryTreeR(&(*tree)->_right, replace->_key);
}
}
return 1;
}
pSearchBinaryTreeNodeK FindSearchBinaryTreeR(pSearchBinaryTreeNodeK * tree, _SBTKeyType x)
{
if (*tree == NULL)
{
return NULL;
}
if (strcmp((*tree)->_key, x) > 0)
{
return FindSearchBinaryTreeR(&(*tree)->_left, x);
}
else if (strcmp((*tree)->_key, x) < 0)
{
return FindSearchBinaryTreeR(&(*tree)->_right, x);
}
else
{
return *tree;
}
}
Test.c
#include"SBT_Topic_1.h"
void InOrderSearchBinaryTreeK(pSearchBinaryTreeNodeK * tree)
{
if (*tree == NULL)
{
return NULL;
}
InOrderSearchBinaryTreeK(&(*tree)->_left);
printf("%s ", (*tree)->_key);
InOrderSearchBinaryTreeK(&(*tree)->_right);
}
void testSBT_Topic_1()
{
char buf[10] = { '0' };
pSearchBinaryTreeNodeK tree = NULL;
InsertSearchBinaryTreeR(&tree, "string");
InsertSearchBinaryTreeR(&tree, "binary");
InsertSearchBinaryTreeR(&tree, "tree");
InsertSearchBinaryTreeR(&tree, "insert");
InsertSearchBinaryTreeR(&tree, "remove");
InsertSearchBinaryTreeR(&tree, "find");
printf("%p\n", FindSearchBinaryTreeR(&tree, "binary"));
printf("%p\n", FindSearchBinaryTreeR(&tree, "queue"));
InOrderSearchBinaryTreeK(&tree);
printf("\n");
while (1)
{
printf("Please Input:");
scanf_s("%s", buf, 10);
if (strcmp(buf, "-1") == 0)
{
break;
}
if (FindSearchBinaryTreeR(&tree, buf))
{
printf("Spelling true!\n");
}
else
{
printf("Spelling false!\n");
}
}
}
int main()
{
testSBT_Topic_1();
return 0;
}