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

举例:模拟实现一个简单的中英互译的字典

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值