二叉搜索树(递归和非递归分别实现)

这是在上一篇博客的基础上进行完善。

1.插入的递归

bool InsertR(const T& key)
	{
		return _InsertR(_root,key);
	}

bool _InsertR(Node*& root,const T& key)
	{
		if(root==NULL)
		{
			root=new Node(key);
			return true;
		}
		if(root->_key>key)
		{
			return _InsertR(root->_left ,key);
		}
		else if(root->_key <key)
		{
			return _InsertR(root->_right ,key);
		}
		else  //这个数存在root->_key==key
		{
			return false;
		}

	}

2.插入的非递归

bool Insert(const T& key)
	{
		//1.空树
		//2.不存在
		//3.存在
		
		//空树
		if(_root==NULL)
		{
			_root=new Node(key);
			return true;
		}
		Node* cur=_root;
		Node* prev=cur;
		while(cur)
		{
			if(cur->_key >key)
			{
				prev=cur;
				cur=cur->_left ;
			}
		    else if(cur->_key <key)
			{
				prev=cur;
				cur=cur->_right ;
			}
			else //if(cur->_key ==key)
				return false;  //存在
		}
		//不存在
		
		if(prev->_key >key)
		{
			cur=new Node(key);
		    prev->_left =cur;
		}
		else
		{
			cur=new Node(key);
		    prev->_right =cur;
		}
		return true;
	}

3.删除的非递归

bool Remove(const T& key)
	{
		if(_root==NULL)
			return false;
		Node* cur=_root;
		Node* parent=NULL;
		while(cur)
		{
			if(cur->_key <key)
			{
				parent=cur;
				cur=cur->_right ;
			}
				
			else if(cur->_key >key)
			{
				parent=cur;
				cur=cur->_left ;
			}
			else    //找到key,再删除
			{
				if(cur->_left==NULL )   //左为空
				{
					if(_root==cur)
					{
						_root=cur->_right ;
					}
					else if(parent->_left ==cur)
					{
						parent->_left =cur->_right ;
					}
					else
					{
						parent->_right =cur->_right ;
					}
					delete cur;
				}
				else if(cur->_right ==NULL)  //右为空
				{
					if(_root==cur)
					{
						_root=cur->_left ;
					}
					else if(parent->_left ==cur)
					{
						parent->_left =cur->_left ;
				    }
					else
					{
						parent->_right =cur->_left ;
					}
					delete cur;
				}
				else    //删除的节点左右都不为空
				{
					//找右子树的最左节点
					Node* righttree=cur->_right ;
					parent=cur;
					while(righttree->_left )
					{
						parent=righttree;
						righttree=righttree->_left ;
					}
					if(parent->_left ==righttree)
					{
						parent->_left =righttree->_right ;
					}
					else
					{
						parent->_right =righttree->_right ;
					}
				}
				return true;
			}
		}
		return false;
	}


4.删除的递归

bool RemoveR(const T& key)
	{
		
	    return _RemoveR(_root,key);
	}
bool _RemoveR(Node*& root,const T& key)//&这个是重点
	{
		if(root==NULL)
			return false;
		if(root->_key <key)
			return _RemoveR(root->_right ,key);
		else if(root->_key >key)
		    return _RemoveR(root->_left ,key);
		else    //找到,再删除
		{
			Node* del=root;
			if(root->_left ==NULL)
			{
				root=root->_right ;
			}
			else
			{
				root=root->_left ;
			}
			delete del;
		}
	}
5.查找的非递归

bool Find(const T& key)
	{
		Node* cur=_root;
		while(cur)
		{
			if(cur->_key <key)
			{
				cur=cur->_right ;
			}
			else if(cur->_key >key)
			{
				cur=cur->_left ;
			}
			else if(cur->_key ==key)
				return true;	
		}
		return false;
	}

6.查找的递归

bool FindR(const T& key)
	{
		_FindR(_root,key);
	}

bool _FindR(Node* root,const T& key)
	{
		if(root==NULL)
			return false;
		if(root->_key >key)
		    return _FindR(root->_left ,key);
		else if(root->_key <key)
			return _FindR(root->_right ,key);
		else
			return true;
	}

7.中序遍历

void InOrderR()
	{
		_InOrderR(_root);
		cout<<endl;
	}
	

void _InOrderR(Node* root)
	{
		if(root==NULL)
		return;
	   _InOrderR(root->_left );
	   cout<<root->_key<<" " ;
	   _InOrderR(root->_right );
	}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值