【数据结构】搜索二叉树的key-value模型

本文介绍了一种使用二叉搜索树(BST)进行单词拼写检查的方法,并展示了如何通过该数据结构实现单词翻译及输入单词重复次数的统计。通过具体的代码实现,文章详细解释了BST的插入、查找、遍历等操作,适用于需要理解和应用BST模型解决实际问题的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 采用key-value模型判断一个单词是否拼写正确,并且显示翻译。

BSVTree.h

#pragma once

#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

typedef char* BSTKeyType;
typedef char* BSTValueType;

typedef struct BSTreeNode
{
	struct BSTreeNode* _left;
	struct BSTreeNode* _right;
	BSTKeyType _key;
	BSTValueType _value;
}BSTreeNode;

int BSTreeInsert(BSTreeNode** ppTree, BSTKeyType key, BSTValueType value);
int BSTreeRemove(BSTreeNode** ppTree, BSTKeyType key);
BSTreeNode* BSTreeFind(BSTreeNode** ppTree, BSTKeyType key);
void BSTreeInOrder(BSTreeNode** ppTree);
void TestBSTree1();

BSVTree.c

#define  _CRT_SECURE_NO_WARNINGS 0


#include "BSVTree.h"


// key/value


BSTreeNode* BuyBSTreeNode(BSTKeyType key, BSTValueType value)
{
	BSTreeNode* node = (BSTreeNode*)malloc(sizeof(BSTreeNode));
	node->_left = NULL;
	node->_right = NULL;
	node->_key = (BSTKeyType)malloc(strlen(key) + 1);
	strcpy(node->_key, key);
	node->_value = value;

	return node;
}

int BSTreeInsert(BSTreeNode** ppTree, BSTKeyType key, BSTValueType value)
{
	BSTreeNode* cur = *ppTree;
	BSTreeNode* parent = NULL;
	if (*ppTree == NULL)
	{
		*ppTree = BuyBSTreeNode(key, value);
		return 1;
	}
	while (cur)
	{
		if (strcmp(cur->_key, key) > 0)
		{
			parent = cur;
			cur = cur->_left;
		}
		else if (strcmp(cur->_key, key) < 0)
		{
			parent = cur;
			cur = cur->_right;
		}
		else
		{
			return 0;
		}
	}
	if (strcmp(parent->_key, key) < 0)
	{
		parent->_right = BuyBSTreeNode(key, value);
	}
	else
	{
		parent->_left = BuyBSTreeNode(key, value);
	}

	return 1;
}

BSTreeNode* BSTreeFind(BSTreeNode** ppTree, BSTKeyType key)
{
	BSTreeNode* cur = *ppTree;
	while (cur)
	{
		if (strcmp(cur->_key, key) < 0)
		{
			cur = cur->_right;
		}
		else if (strcmp(cur->_key, key) > 0)
		{
			cur = cur->_left;
		}
		else
		{
			return cur;
		}
	}

	return NULL;
}

void BSTreeInOrder(BSTreeNode** ppTree)
{
	BSTreeNode* root = *ppTree;
	if (*ppTree == NULL)
	{
		return;
	}

	BSTreeInOrder(&root->_left);
	printf("%s:%d\n", root->_key, root->_value);
	BSTreeInOrder(&root->_right);
}

void TestBSTree1()
{
	char str[10];
	BSTreeNode* pTree = NULL;
	BSTreeInsert(&pTree, "insert", "插入");
	BSTreeInsert(&pTree, "sort", "分类");
	BSTreeInsert(&pTree, "find", "找");
	BSTreeInsert(&pTree, "tree", "树");
	BSTreeInsert(&pTree, "destory", "销毁");
	while (1)
	{
		scanf("%s", str);
		if (BSTreeFind(&pTree, str))
		{
			printf("正确的单词\n");
			printf("%s\n", BSTreeFind(&pTree, str)->_value);
		}
		else
		{
			printf("错误的单词\n");
		}
	}
}


int main()
{
	TestBSTree1();
	system("pause");
	return 0;
}

2. 采用key-value模型求出输入单词重复出现的次数?

BSVTree.h

#pragma once

#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

typedef char* BSTKeyType;
typedef char* BSTValueType;

typedef struct BSTreeNode
{
	struct BSTreeNode* _left;
	struct BSTreeNode* _right;
	BSTKeyType _key;
	BSTValueType _value;
}BSTreeNode;

int BSTreeInsert(BSTreeNode** ppTree, BSTKeyType key, BSTValueType value);
int BSTreeRemove(BSTreeNode** ppTree, BSTKeyType key);
BSTreeNode* BSTreeFind(BSTreeNode** ppTree, BSTKeyType key);
void BSTreeInOrder(BSTreeNode** ppTree);
void TestBSTree1();

BSVTree.c

#define  _CRT_SECURE_NO_WARNINGS 0


#include "BSVTree.h"


// key/value


BSTreeNode* BuyBSTreeNode(BSTKeyType key, BSTValueType value)
{
	BSTreeNode* node = (BSTreeNode*)malloc(sizeof(BSTreeNode));
	node->_left = NULL;
	node->_right = NULL;
	node->_key = (BSTKeyType)malloc(strlen(key) + 1);
	strcpy(node->_key, key);
	node->_value = value;

	return node;
}

int BSTreeInsert(BSTreeNode** ppTree, BSTKeyType key, BSTValueType value)
{
	BSTreeNode* cur = *ppTree;
	BSTreeNode* parent = NULL;
	if (*ppTree == NULL)
	{
		*ppTree = BuyBSTreeNode(key, value);
		return 1;
	}
	while (cur)
	{
		if (strcmp(cur->_key, key) > 0)
		{
			parent = cur;
			cur = cur->_left;
		}
		else if (strcmp(cur->_key, key) < 0)
		{
			parent = cur;
			cur = cur->_right;
		}
		else
		{
			return 0;
		}
	}
	if (strcmp(parent->_key, key) < 0)
	{
		parent->_right = BuyBSTreeNode(key, value);
	}
	else
	{
		parent->_left = BuyBSTreeNode(key, value);
	}

	return 1;
}

BSTreeNode* BSTreeFind(BSTreeNode** ppTree, BSTKeyType key)
{
	BSTreeNode* cur = *ppTree;
	while (cur)
	{
		if (strcmp(cur->_key, key) < 0)
		{
			cur = cur->_right;
		}
		else if (strcmp(cur->_key, key) > 0)
		{
			cur = cur->_left;
		}
		else
		{
			return cur;
		}
	}

	return NULL;
}

void BSTreeInOrder(BSTreeNode** ppTree)
{
	BSTreeNode* root = *ppTree;
	if (*ppTree == NULL)
	{
		return;
	}

	BSTreeInOrder(&root->_left);
	printf("%s:%d\n", root->_key, root->_value);
	BSTreeInOrder(&root->_right);
}


void TestBSTree1()
{
	char str[10] = { '0' };
	BSTreeNode* pTree = NULL;
	while (1)
	{
		BSTreeNode* node;
		scanf("%s", str);
		if (strcmp(str, "exit") == 0)
		{
			break;
		}

		node = BSTreeFind(&pTree, str);
		if (node)
		{
			node->_value++;
		}
		else
		{
			BSTreeInsert(&pTree, str, 1);
		}
	}

	BSTreeInOrder(&pTree);
}

int main()
{
	TestBSTree1();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值