搜索二叉树的应用(Key模型)

举例:判断一个单词是否拼写正确 

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值