M的编程备忘录之C++——搜索二叉树

目录

1、搜索二叉树概念

2、搜索二叉树操作

2.1、搜索二叉树的搜索

2.2、二叉搜索树的插入

2.3、二叉搜索树的删除

3、二叉搜索树实现


1、搜索二叉树概念

        二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
                若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
                若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
                它的左右子树也分别为二叉搜索树

2、搜索二叉树操作

2.1、搜索二叉树的搜索

        1、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
        2、最多查找高度次,走到到空,还没找到,这个值不存在。

2.2、二叉搜索树的插入

        插入的具体过程如下:         
                1、树为空,则直接新增节点,赋值给root指针
                2、树不空,按二叉搜索树性质查找插入位置,插入新节点

2.3、二叉搜索树的删除

        首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
        a. 要删除的结点无孩子结点
        b. 要删除的结点只有左孩子结点,删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除
        c. 要删除的结点只有右孩子结点,删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除
        d. 要删除的结点有左、右孩子结点,在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题--替换法删除

3、二叉搜索树实现

template<class K, class V >
struct BSTreeNode
{
	BSTreeNode<K, V>* _left;
	BSTreeNode<K, V>* _right;
	K _key;
	V _vaule;

	BSTreeNode(const K& key, const V& vaule = key)
		:_left(nullptr)
		,_right(nullptr)
		,_key(key)
		,_vaule(vaule)
	{}
};

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

	BSTree(const BSTree<K, V>& t)
	{
		_root = CopyTree(t._root);
	}

	BSTree<K, V>& operator=(BSTree<K, V> t)
	{
		swap(_root, t._root);
		return *this;
	}

	~BSTree()
	{
		DestoryTree(_root);
		_root == nullptr;
	}
	//迭代实现
	bool Insert(const K& key, const V& value = key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key, value);
			return true;
		}

		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
			{
				return false;
			}
		}
		cur = new Node(key, value);
		if (parent->_key > key)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		return true;
	}
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	bool Erase(const K& key)
	{
		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 (cur == parent->_left)
						{
							parent->_left = cur->_right;
						}
						else
						{
							parent->_right = cur->_right;
						}
					}
					delete cur;
				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_left;
					}
					else
					{
						if (cur == parent->_left)
						{
							parent->_left = cur->_left;
						}
						else
						{
							parent->_right = cur->_left;
						}
					}
					delete cur;
				}
				else
				{
					Node* minparent = cur;
					Node* minRight = cur->_right;
					while (minRight->_left)
					{
						minparent = minRight;
						minRight = minRight->_left;
					}

					swap(cur->_key, minRight->_key);

					if (minparent->_left == minRight)
					{
						minparent->_left = minRight->_right;
					}
					else
					{
						minparent->_right = minRight->_right;
					}
					delete minRight;
				}
				return true;
			}
		}
		return false;
	}
	void InOrder()
	{
		_InOrder(_root);
	}

	//递归实现
	bool FindR(const K& key, const V& value)
	{
		return _FindR(_root, key, value);
	}
	bool EraseR(const K& key)
	{
		return _EraseR(_root, key);
	}
	bool InsertR(const K& key, const V& value)
	{
		return _InsertR(_root, key, value);
	}
private:
	bool _FindR(Node* root, const K& key, const V& value)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_key > key)
		{
			return _FindR(_root, key, value);
		}
		else if (root->_key < key)
		{
			return _FindR(_root, key, value);
		}
		else
		{
			return true;
		}
	}
	bool _EraseR(Node*& root, const K& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_key > key)
		{
			return _EraseR(root->_left, key);
		}
		else if (root->_key < key)
		{
			return _EraseR(root->_right, key);
		}
		else
		{
			Node* del = root;
			if (root->_left == nullptr)
			{
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* minRight = root->_right;
				while (minRight->_left)
				{
					minRight = minRight->_left;
				}
				swap(root->_key, minRight->_key);

				return _EraseR(root->_right, key);
			}
			delete del;
			return true;
		}
	}
	bool _InsertR(Node*& root,const K& key, const V& value)
	{
		if (root == nullptr)
		{
			root = new Node(key, value);
			return true;
		}
		if (root->_key > key)
		{
			return _InsertR(root->_left, key, value);
		}
		else if (root->_key < key)
		{
			return _InsertR(root->_right, key, value);
		}
		else
		{
			return false;
		}
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_vaule << " ";
		_InOrder(root->_right);
	}	
	void DestoryTree(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		DestoryTree(root->_left);
		DestoryTree(root->_right);
		delete root;
	}
	Node* CopyTree(Node* root)
	{
		if (root == nullptr)
		{
			return nullptr;
		}
		Node* copyNode = new Node(root->_key, root->_vaule);
		copyNode->_left = CopyTree(root->_left);
		copyNode->_right = CopyTree(root->_right);

		return copyNode;
	}
private:
	Node* _root = nullptr;
};

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值