【数据结构】二叉搜索树(增、删、查)的递归与非递归实现

前言:

    二叉搜索树是二叉树中的一种特殊结构,具有如下的性质:

➢每个节点都有一个作为搜索依据的关键码(key),所有节点的关键码互不相同。

➢左子树上所有节点的关键码(key)都小于根节点的关键码(key)。

➢右子树上所有节点的关键码(key)都大于根节点的关键码(key)。

➢左右子树都是二叉搜索树。

➢中序遍历是有序的


看下图就能够理解二叉搜索树的结构特征了:


实现代码如下:(具体的过程就不解释了,看代码注释)

#ifndef __SBTREE_H__
#define __SBTREE_H__


//K,V对实现二叉搜索树结构
template<class K,class V>
struct SBTNode
{
	SBTNode* _left;
	SBTNode* _right;
	K _key;
	V _value;

	SBTNode(const K& key, const V& value)
		:_left(NULL)
		, _right(NULL)
		, _key(key)
		, _value(value)
	{}

};


template<class K,class V>
class SBTree
{
	typedef SBTNode<K, V> Node;

public:
	SBTree()
		:_root(NULL)
	{}

	
	bool Insert(const K& key, const V& value)
	{
		if (_root == NULL)
		{
			_root = new Node(key, value);
		}

		Node* cur = _root;
		Node* parent = NULL;
		//先去找到要插入的位置
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}

			else
			{
				return false;
			}
		}

		//找到插入位置以后,判断插入父亲节点的左边还是右边
		if (parent->_key > key)
		{
			parent->_left = new Node(key, value);
		}
		else
		{
			parent->_right = new Node(key, value);
		}

		return true;
	}

	Node* Find(const K&key)
	{
		Node* cur = _root;
		//遍历查找即可
		while (cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left; 
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}

		return NULL;
	}

	bool Remove(const K&key)
	{
		if (_root == NULL)
			return false;

		//如果只有一个节点的情况
		if (_root->_left == NULL && _root->_right == NULL)
		{
			if (_root->_key == key)
			{
				delete _root;
				_root = NULL;
				return true;
			}

			return false;
		}

		Node* parent = NULL;
		Node* cur = _root;
		//遍历查找要删除节点的位置
		while (cur)
		{
			Node* del = NULL;
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}  
			else
			{
				//要删除节点的左边为空的情况
				if (cur->_left == NULL)
				{
					//注意点,需要判断父节点是否为空,如果父节点为空,说明要删除的节点为根节点,如:只有根节点5与其右节点9的情况
					if (parent == NULL)
					{
						_root = cur->_right;
						delete cur;
						cur = NULL;
						return true;
					}
					if (parent->_key >cur->_key)
					{
						del = cur;
						parent->_left = cur->_right;
						delete del;
						return true;
					}
					else if (parent->_key < key)
					{
						del = cur;
						parent->_right = cur->_right;
						delete del;
						return true;
					}
				}
				//要删除节点的右子树为空的情况
				else if (cur->_right == NULL)
				{
					//需要判断父节点是否为空,如果为空说明删除节点为根节点,如只有根节点5与其左节点3
					if (parent == NULL)
					{
						_root = cur->_left;
						delete cur;
						cur = NULL;
						return true;
					}
					if (parent->_key >cur->_key)
					{
						del = cur;
						parent->_left = cur->_left;
						delete del;
						return true;
					}
					else if (parent->_key < cur->_key)
					{
						del = cur;
						parent->_right = cur->_left;
						delete del;
						return true;
					}
				}
				//左右子树都不为空的情况
				else
				{
					Node* del=cur;
					Node* parent = NULL;
					Node* RightFirst = cur->_right;
					//右边第一个节点的左子树为空的情况
					if (RightFirst->_left ==NULL)
					{
						swap(RightFirst->_key, cur->_key);
						swap(RightFirst->_value, cur->_value);
						del = RightFirst;
						cur->_right = RightFirst->_right;
						delete del;
						return true;
					}

					//右边第一个节点的左子树不为空的情况
					while (RightFirst->_left)
					{
						parent = RightFirst;
						RightFirst = RightFirst->_left;
					}

					swap(RightFirst->_key, cur->_key);
					swap(RightFirst->_value, cur->_value);

					del = RightFirst;
					parent->_left = RightFirst->_right;
					delete del;
					return true;

				}
			}
		}
		return false;
	}

	bool Insert_R(const K&key,const V&value)
	{
		return _Insert_R(_root,key, value);
	}

	Node* Find_R(const K& key)
	{
		return _Find_R(_root, key);
	}
	

	bool Remove_R(const K&key)
	{
		return _Remove_R(_root, key);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

protected:
	bool _Remove_R(Node*& root, const K& key)
	{
		//没有节点
		if (root == NULL)
			return false;

		//只有一个根节点,判断是不是然后删除
		if (root->_left == NULL && root->_right == NULL)
		{
			if (root->_key == key)
			{
				delete root;
				root = NULL;
				return true;
			}
			else
				return false;
		}

		if (root->_key > key)
		{
			_Remove_R(root->_left, key);
		}
		else if (root->_key < key)
		{
			_Remove_R(root->_right, key);
		}
		else
		{
			Node* del = NULL;
			if (root->_left == NULL)
			{
				del = root;
				root = root->_right;//本层的root等价于上一层传过来的root->right或者root->left,因为传值时传的引用
				delete del;
				del = NULL;
				return true;
			}
			else if (root->_right == NULL)
			{
				del = root;
				root = root->_left;//本层的root等价于上一层传过来的root->right或者root->left,因为传值时传的引用
				delete del;
				del = NULL;
				return true;
			}
			else
			{
				Node* RightFirst = root->_right;
				while (RightFirst->_left)
				{
					RightFirst = RightFirst->_left;
				}

				swap(root->_key, RightFirst->_key);
				swap(root->_value, RightFirst->_value);



				//删除的时候为什么选择root->_right,当其为作为空时,就相当于把要删除的节点的前一个节点传过去了,为什么不传RightFirst这个变量呢,不具有引用的传递性,从而选择了root->_right代替,能够衔接上引用的传递,该节点的root就是上一个节点的root->_right或者root->_left
				//为什么不用root作为删除节点的根?因为交换以后root的值比要删除的这个值大,会往左边去找,从而出错,实际上交换以后在右边的第一个的最左边。
				_Remove_R(root->_right, key);

				return true;
			}
		}
	}

	Node* _Find_R(Node* root, const K& key)
	{
		if (root == NULL)
			return NULL;

		if (root->_key > key)
		{
			return _Find_R(root->_left, key);
		}
		else if (root->_key < key)
		{
			return _Find_R(root->_right, key);
		}

		else
		{
			return root;
		}
	}

	bool _Insert_R(Node*& root,const K&key, const V&value)
	{
		if (root == NULL)
		{
			root = new Node(key, value);
			return true;
		}

		if (root->_key > key)
			return _Insert_R(root->_left, key, value);

		else if (root->_key < key)
			return _Insert_R(root->_right, key, value);

		else
			return false;
	}

	void _InOrder(Node* root)
	{
		if (root == NULL)
			return;

		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}

protected:
	Node* _root;
};




void Test1()
{
	SBTree<int, int> s;
	s.Insert_R(5, 1);
	s.Insert_R(4, 1);
	s.Insert_R(3, 1);
	s.Insert_R(6, 1);
	s.Insert_R(1, 1);
	s.Insert_R(2, 1);
	s.Insert_R(9, 1);
	s.Insert_R(8, 1);
	s.Insert_R(7, 1);
	s.Insert_R(0, 1);
	s.InOrder();
	/*cout << s.Find_R(9)->_key << endl;*/
	s.Remove(4);
	s.Remove(7);
	s.Remove(8);
	
	s.Remove(6);
	s.Remove(3);
	s.Remove(1);
	s.Remove(2);
	s.Remove(0);
	s.Remove(5);
	s.Remove(9);
	s.InOrder();


}

#endif //__SBTREE_H__




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值