搜索二叉树2

#pragma once 
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

typedef int BSTDataType;
typedef char* BSTKeyType;
typedef char* BSTValueType;

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

/////////////////////////////////////////////////////// 
// 非递归 
//BSTreeNode* BuyBSTreeNode(BSTDataType x);
int BSTreeInsert(BSTreeNode** pptree, BSTDataType x);
//const BSTreeNode* BSTreeFind(BSTreeNode** pptree, BSTDataType x);
//int BSTreeRemove(BSTreeNode** pptree, BSTDataType x);
//void BSTreeInOrder(BSTreeNode** pptree);

/////////////////////////////////////////////////////// 
// 递归 
//int BSTreeInsertR(BSTreeNode** pptree, BSTDataType x);
//const BSTreeNode* BSTreeFindR(BSTreeNode** pptree, BSTDataType x);
//int BSTreeRemoveR(BSTreeNode** pptree, BSTDataType x);

/////////////////////////////////////////////////////// 
//1. 判断一个单词是否拼写正确
//2. 请模拟实现一个简单的中英互译的字典
//3. log文件中有许多异常重复的IP地址,请统计出每个异常IP出现了多少次?
BSTreeNode* BuyBSTreeNodeR(BSTKeyType key, BSTValueType value);
int BSTreeInsertR(BSTreeNode** pptree, BSTKeyType key, BSTValueType value);
BSTreeNode* BSTreeFindR(BSTreeNode** pptree, BSTKeyType key);
int BSTreeRemoveR(BSTreeNode** pptree, BSTKeyType key);
void BSTreeInOrderR(BSTreeNode** ppTree);
#include"BinarySearchTree.h"

/////////////////////////////////////////////////////// 
// 非递归 
BSTreeNode* BuyBSTreeNode(BSTDataType x)
{
	BSTreeNode * node = (BSTreeNode *)malloc(sizeof(BSTreeNode));
	node->_left = NULL;
	node->_right = NULL;
	node->_data = x;
	return node;
}
int BSTreeInsert(BSTreeNode** pptree, BSTKeyType x)
{
	BSTreeNode* cur=NULL, *parent=NULL;
	if (*pptree == NULL)
	{
		*pptree = BuyBSTreeNode(x);
		return 1;
	}

	cur = *pptree;
	while (cur)
	{
		if (cur->_data > x)
		{
			parent = cur;
			cur = cur->_left;
		}
		else if (cur->_data < x)
		{
			parent = cur;
			cur = cur->_right;
		}
		else
		{
			return 0;
		}
	}

	if (parent->_key < x)
	{
		parent->_right = BuyBSTreeNode(x);
	}
	else
	{
		parent->_left = BuyBSTreeNode(x);
	}

	return 1;
}
//const BSTreeNode* BSTreeFind(BSTreeNode** pptree, BSTDataType x)
//{
//	BSTreeNode *cur = *pptree;
//	while (cur)
//	{
//		if (cur->_data > x)
//		{
//			cur = cur->_left;
//		}
//		else if (cur->_data<x)
//		{
//			cur = cur->_right;
//		}
//		else
//		{
//			return cur;
//		}
//	}
//	return NULL;
//}
//int BSTreeRemove(BSTreeNode** pptree, BSTDataType x)
//{
//	BSTreeNode * cur, *parent;
//	parent = NULL;
//	cur = *pptree;
//	while (cur)
//	{
//		if (cur->_data > x)
//		{
//			cur = cur->_left;
//		}
//		else if (cur->_data<x)
//		{
//			cur = cur->_right;
//		}
//		else
//		{
//			//开始删除
//			//左为空/右为空
//			//左右都不为空
//			if (cur->_left == NULL)
//			{
//				if (parent == NULL)
//				{
//					*pptree= cur->_right;
//				}
//				else
//				{
//					if (cur == parent->_left)
//					{
//						parent->_left = cur->_right;
//					}
//					else
//					{
//						parent->_right = cur->_right;
//					}
//				}		
//			}
//			else if (cur->_right == NULL)
//			{
//				if (parent == NULL)
//				{
//					*pptree= cur->_left;
//				}
//				else
//				{
//					if (cur == parent->_left)
//					{
//						parent->_left = cur->_left;
//					}
//					else
//					{
//						parent->_right = cur->_left;
//					}
//				}
//			}
//			//两个孩子都不为空
//			else
//			{
//				BSTreeNode * replace = cur->_right;
//				while (replace->_left)
//				{
//					replace = replace->_left;
//					
//				}
//				cur->_data = replace->_data;
//				return BSTreeRemove(&cur->_right, replace->_data);
//			}
//			free(cur);
//			return 1;
//		}
//	}
//	return 0;
//}
void BSTreeInOrder(BSTreeNode** pptree)
{
	if (*pptree== NULL)
	{
		return;
	}
	BSTreeInOrder(&(*pptree)->_left);
	printf("%d", (*pptree)->_data);
	BSTreeInOrder(&(*pptree)->_right);
}

/////////////////////////////////////////////////////// 
// 递归 
//int BSTreeInsertR(BSTreeNode** pptree, BSTDataType x)
//{
//	if (*pptree== NULL)
//	{
//		*pptree= BuyBSTreeNode(x);
//		return 1;
//	}
//	if ((*pptree)->_data > x)
//		return  BSTreeInsertR(&(*pptree)->_left, x);
//	else if ((*pptree)->_data < x)
//		return  BSTreeInsertR(&(*pptree)->_right, x);
//	else
//		return 0;
//}
//const BSTreeNode* BSTreeFindR(BSTreeNode** pptree, BSTDataType x)
//{
//	if (*pptree== NULL)
//	{
//		return NULL;
//	}
//	if ((*pptree)->_data > x)
//	{
//		return BSTreeFindR(&(*pptree)->_left, x);
//	}
//	else if ((*pptree)->_data < x)
//	{
//		return BSTreeFindR(&(*pptree)->_right, x);
//	}
//	else
//	{
//		return *pptree;
//	}
//}
//
//
//int BSTreeRemoveR(BSTreeNode** pptree, BSTDataType x)
//{
//	if (*pptree== NULL)
//	{
//		return -1;
//	}
//	if ((*pptree)->_data < x)
//	{
//		return BSTreeRemoveR((&(*pptree)->_right), x);
//	}
//	else if ((*pptree)->_data > x)
//	{
//		return BSTreeRemoveR((&(*pptree)->_left), x);
//	}
//	else
//	{
//		BSTreeNode* del = *pptree;
//		if ((*pptree)->_left == NULL)
//		{
//			*pptree= (*pptree)->_right;
//			free(del);
//		}
//		else if ((*pptree)->_right == NULL)
//		{
//			*pptree= (*pptree)->_left;
//			free(del);
//		}
//		else
//		{
//			BSTreeNode* replace = (*pptree)->_right;
//			while (replace->_left)
//			{
//				replace = replace->_left;
//			}
//			(*pptree)->_data = replace->_data;
//			return BSTreeRemoveR(&(*pptree)->_right, replace->_data);
//		}
//	}
//	return 1;
//}
BSTreeNode* BuyBSTreeNodeR(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 BSTreeInsertR(BSTreeNode** pptree, BSTKeyType key, BSTValueType value)
{
	BSTreeNode* cur,*parent=NULL;
	if (*pptree == NULL)
	{
		*pptree = BuyBSTreeNodeR(key, value);
		return 1;
	}

	cur = *pptree;
	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 = BuyBSTreeNodeR(key, value);
	}
	else
	{
		parent->_left = BuyBSTreeNodeR(key, value);
	}

	return 1;
}

BSTreeNode* BSTreeFindR(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 BSTreeInOrderR(BSTreeNode** pptree)
{
	BSTreeNode* root = *pptree;
	if (*pptree == NULL)
	{
		return;
	}

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

test.c

#include"BinarySearchTree.h"
void TestBSTree2()
{
	BSTreeNode* ppptree = NULL;

	BSTreeInsertR(&ppptree, "tree", "树");
	BSTreeInsertR(&ppptree, "sort", "排序");
	BSTreeInsertR(&ppptree, "binary", "二分");
	BSTreeInsertR(&ppptree, "return", "返回");
	BSTreeInsertR(&ppptree, "hash", "哈希");
	BSTreeInsertR(&ppptree, "list", "链表");

	printf("%s\n", BSTreeFindR(&ppptree, "tree")->_value);
	printf("%s\n", BSTreeFindR(&ppptree, "return")->_value);
	printf("%s\n", BSTreeFindR(&ppptree, "hash")->_value);
	printf("%s\n", BSTreeFindR(&ppptree, "list")->_value);
}
void TestBSTree1()
{
	char str[100] = { '0' };
	BSTreeNode* pTree= NULL;
	BSTreeInsert(&pTree, "insert");
	BSTreeInsert(&pTree, "sort");
	BSTreeInsert(&pTree, "find");
	BSTreeInsert(&pTree, "tree");
	BSTreeInsert(&pTree, "destory");

	while (1)
	{
		scanf_s("%s", &str);
		if (BSTreeFindR(&pTree, str))
		{
			printf("正确的单词\n");
		}
		else
		{
			printf("错误的单词\n");
		}
	}
}

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

		node = BSTreeFindR(&pptree, str);
		if (node)
		{
			node->_value++;
		}
		else
		{
			BSTreeInsertR(&pptree, str, 1);
		}
	}

	BSTreeInOrder(&pptree);
}
int main()
{
	//TestBSTree2();
	//TestBSTree1();
	TestBSTree3();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41268108/article/details/82795230