搜索二叉树1

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

typedef int BSTDataType;

typedef struct BSTreeNode
{
	struct BSTreeNode* _left;
	struct BSTreeNode* _right;

	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);
#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, BSTDataType x)
{
	BSTreeNode* cur, *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->_data < 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;
}
#include"BinarySearchTree.h"
void TestBSTree() 
{ 
		int i = 0;
		BSTreeNode* ppTree = NULL;
		int a[] = {5,3,4,1,7,8,2,6,0,9};
		for (; i < sizeof(a)/sizeof(int); ++i)
		{	
			BSTreeInsert(&ppTree, a[i]);
		}
	
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 2);
		BSTreeRemove(&ppTree, 1);
		BSTreeRemove(&ppTree, 8);
		BSTreeRemove(&ppTree, 5);
	
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 6);
		BSTreeRemove(&ppTree, 5);
	
	
		BSTreeRemove(&ppTree, 0);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 1);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		/*BSTreeRemove(&ppTree, 2);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 3);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 4);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 5);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 6);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 7);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 8);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 9);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeRemove(&ppTree, 10);
		BSTreeInOrder(&ppTree);
		printf("\n");
	
		BSTreeInOrder(&ppTree);
		printf("\n");*/
	

}
void TestBSTreeR()
{
	int i = 0;
		BSTreeNode* ppTree = NULL;
		int a[] = {5,3,4,1,7,8,2,6,0,9};
		for (; i < sizeof(a)/sizeof(int); ++i)
		{	
			BSTreeInsertR(&ppTree, a[i]);
		}
	
		BSTreeRemoveR(&ppTree, 0);
		BSTreeRemoveR(&ppTree, 1);
		BSTreeRemoveR(&ppTree, 2);
		BSTreeRemoveR(&ppTree, 3);
		BSTreeRemoveR(&ppTree, 4);
		BSTreeRemoveR(&ppTree, 5);
		BSTreeRemoveR(&ppTree, 6);
		BSTreeRemoveR(&ppTree, 7);
		BSTreeRemoveR(&ppTree, 8);
		BSTreeRemoveR(&ppTree, 9);
		BSTreeRemoveR(&ppTree, 10);
	
		BSTreeInOrder(&ppTree);
		printf("\n");
}
int main()
{
	TestBSTree();
	//TestBSTreeR();
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值