C++二叉搜索树

目录

一、基本介绍

二、二叉搜索树增删查的代码实现(_key-_value型的二叉搜索树)


一、基本介绍

二叉搜索树是一棵空树,或者是具有以下性质的二叉树:
 1、若左子树不为空,则左子树上所有节点的值都小于等于根节点的值
 2、若右子树不为空,则右子树上所有节点的值都大于等于根节点的值
 3、他的左右子树也分别为二叉搜索树
 ——和堆还不一样,它不区分左右子树,要么都小,要么都大,或者相等
 
 
 二叉搜索树的结构决定了查找一个值时,只要根的值比查找的值大,就往左走;
 只要根的值比查找的值小,就往右走;直至最后走到空,若还没有找到,就结束,没有这个值
 
 
 时间复杂度是(N)——最坏情况,形成了一个类似于链表的结构
 (logN)——最优情况
 
 
 为了应对最坏情况,就有了平衡搜索树、红黑树、B树系列


二、二叉搜索树增删查的代码实现(_key-_value型的二叉搜索树)

以下为一个树节点

template <class K, class V>
struct BSTNode
{
	K _key;
	V _value;//与增删查的实现无关
	BSTNode<K, V>* _left;
	BSTNode<K, V>* _right;

	BSTNode(const K& key, const V& value)
		: _key(key)
		, _value(value)
		, _left(nullptr)
		, _right(nullptr)
	{}
};

以下为增删查的代码实现

template <class K, class V>
class BSTree
{
	using Node = BSTNode<K, V>;
public:
	BSTree() = default;

	BSTree(const BSTree& t)
	{
		_root = Copy(t._root);
	}

	BSTree& operator=(BSTree tmp)
	{
		swap(_root, tmp._root);
		return *this;
	}

	~BSTree()
	{
		_Destroy(_root);
		_root = nullptr;
	}

	//二叉搜索树的插入
	bool Insert(const K& key, const V& value)
	{
		if (!_root)
		{
			_root = new Node(key, value);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
				//这里可以根据实际情况来确定是否允许插入相同的值,这里以及其他功能是实现的不允许的
			{
				return false;
			}
		}

		Node* newnode = new Node(key, value);
		//这里已经是parent是叶子结点了
		if (parent->_key < key)
		{
			parent->_right = newnode;
		}
		else
		{
			parent->_left = newnode;
		}

		return true;
	}

	//二叉搜索树的中序遍历以及private成员函数的调用方法
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	//二叉搜索树的查找
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key == key)
			{
				return cur;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
		}
		return nullptr;
	}

	//二叉搜索树的删除
	bool Erase(const K& key)
	{
		if (_root == nullptr)
		{
			return false;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)//先找到要删除的节点
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				//删除
				//先是左右两边一边为空的情况
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_right;
					}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_right;
						}
						else
						{
							parent->_right = cur->_right;
						}
					}
					delete cur;
					return true;
				}
				else if(cur->_right == nullptr)
				{
					if (_root == cur)
					{
						_root = cur->_left;
					}
					else
					{
						if (parent->_left == cur)
						{
							parent->_left = cur->_left;
						}
						else
						{
							parent->_right = cur->_left;
						}
					}
					delete cur;
					return true;
				}
				//这里是两边都有孩子的情况
				else
				{
					//替代法,找右子树的最小值,就满足了根节点比左子树都大,比右子树都小
					Node* replace = cur->_right;
					Node* replace_parent = cur;
					while (replace->_left)
					{
						replace_parent = replace;
						replace = replace->_left;
					}

					cur->_key = replace->_key;
					if (replace_parent->_left == replace)
						replace_parent->_left = replace->_right;
					else
						replace_parent->_right = replace->_right;

					delete replace;
					return true;
				}

			}
		}

		return false;
	}

private:
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

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

	void _Destroy(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_Destroy(root->_left);
		_Destroy(root->_right);
		delete root;
	}

	Node* Copy(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}

		Node* newRoot = new Node(root->_key, root->_value);
		newRoot->_left = Copy(root->_left);
		newRoot->_right = Copy(root->_right);

		return newRoot;
	}

	Node* _root = nullptr;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值