二叉搜索树删除节点完美版

经测试,现在无bug,欢迎指点。

//main.cpp

///

//题目描述:删除给定二叉排序树的给定节点

//

//作者:k_eckel(韦福如)

//时间:2005-09-30

//



#include <iostream>

using namespace std;



template <class T>

struct node

{

	node<T>* _lchild;

	node<T>* _rchild;



	T _key;



	node(const T& key):_lchild(NULL),_rchild(NULL),_key(key)

	{

	}

};



template <class T>

void Insert(node<T>*& root,const T& key)

{

	if (NULL == root) 

	{

		node<T>* n = new node<T>(key);

		root = n;

		return ;

	}



	if (root->_key > key)

		Insert(root->_lchild,key); 

	else

		Insert(root->_rchild,key); 

}
template <class T>

void Delete(node<T>*& root,const T& key)
{
	if(root == NULL)
		return;
	node<T>	*ptr = root, *pre = NULL;
	while(ptr != NULL && ptr->_key != key)
	{
		pre = ptr;
		if(ptr->_key > key)
			ptr = ptr->_lchild;
		else
			ptr = ptr->_rchild;
	}

	if(ptr == NULL)
		return;

	if(ptr->_lchild == NULL && ptr->_rchild == NULL)
	{
		if(ptr == root)
		{
			root = NULL;
			delete ptr;
			return;
		}
		else
		{
			if(ptr == pre->_lchild)
				pre->_lchild = NULL;
			else
				pre->_rchild = NULL;

			delete ptr;
		}
	}
	else if(ptr->_rchild == NULL)
	{
		if(ptr == root)
			root = ptr->_lchild;
		else if(pre->_lchild == ptr)
			pre->_lchild = ptr->_lchild;
		else
			pre->_rchild = ptr->_lchild;

		delete ptr;
	}
	else if(ptr->_lchild == NULL)
	{
		if(ptr == root)
			root = ptr->_rchild;
		else if(pre->_lchild == ptr)
			pre->_lchild = ptr->_rchild;
		else
			pre->_rchild = ptr->_rchild;

		delete ptr;
	}
	else
	{
		node<T>* tmp = ptr;
		pre= ptr;
		ptr = ptr->_rchild;
		while(ptr->_lchild != NULL)
		{
			pre = ptr;
			ptr = ptr->_lchild;
		}

		tmp->_key = ptr->_key;
		if(ptr->_rchild != NULL)
		{
			if(pre->_lchild == ptr)
				pre->_lchild = ptr->_rchild;
			else
				pre->_rchild = ptr->_rchild;
		}
		delete ptr;
	}
}



template <class T>

void InorderPrint(const node<T>* root,const char* info)

{

	if (NULL == root) return ;



	if (info)

		cout<<info<<endl;



	if (NULL != root)

	{

		InorderPrint(root->_lchild,NULL);

		cout<<root->_key<<" ";

		InorderPrint(root->_rchild,NULL);

	}



}



int main(int argc,char* argv[])

{

	//测试代码

	node<int>* r = NULL;

	//建立一棵用于测试的排序二叉树

	Insert<int>(r,5);

	Insert(r,2);

	Insert(r,1);

	Insert(r,3);

	Insert(r,4);

	Insert(r,7);

	Insert(r,6);

	Insert(r,8);

	Insert(r,9);



	InorderPrint<int>(r,"before delete");

	cout<<endl;



	Delete<int>(r,25); //删除不存在的节点

	InorderPrint<int>(r,"after delete");

	cout<<endl;





	//Delete<int>(r,5); //有左右子树,根节点

	//Delete<int>(r,7); //有左右子树,非根节点

	//Delete<int>(r,1); //无左右节点

	//Delete<int>(r,8); //有右子树,无左子树

	//Delete<int>(r,8); //有右子树,无左子树

	Delete<int>(r,3); 

	Delete<int>(r,4); 

	Delete<int>(r,1);   //此时有左子树,无右子树

	InorderPrint<int>(r,"after delete");

	cout<<endl;



	Delete<int>(r,7); 

	InorderPrint<int>(r,"after delete");

	cout<<endl;



	Delete<int>(r,9); 

	Delete<int>(r,8); 

	Delete<int>(r,2); 



	InorderPrint<int>(r,"after delete");

	cout<<endl;



	Delete<int>(r,5); 

	InorderPrint<int>(r,"after delete");

	cout<<endl;



	Delete<int>(r,6); //此时只有根节点,无左右子树

	InorderPrint<int>(r,"after delete");

	cout<<endl;


	system("pause");
	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值