二叉树进阶——手撕二叉搜索树

troop主页:troop

1.二叉搜索树的定义

二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:

  • 若它的左子树不为空,则左子树上的所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上的所有节点的值都大于根节点的值

在这里插入图片描述

2.实现(非递归)

补充结构

//struct BinarySearchTreeNode
template<class K>
struct BSTreeNode
{
	typedef BSTreeNode<K> Node;
	Node* _left;
	Node* _right;
	K _key;

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

/class BinarySearchTree
template<class K>
class BSTree
{
	typedef BSTreeNode<K> Node;
public:
	BSTree() = default;
	BSTree(const BSTree<K>& t)
	{
		_root = copy(t._root);
	}
	Node* copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;
		Node* newroot = new Node(root->_val);
		newroot->_left = copy(root->_left);
		newroot->_right = copy(root->_right);
		return newroot;
	}

	~BSTree()
	{
		Destroy();
	}
	void Destroy()
	{
		return _Destroy(_root);
	}
	void _Destroy(Node* root)
	{
		if (root == nullptr)
			return;
		_Destroy(root->_left);
		_Destroy(root->_right);
		delete root;
	}
private:
	Node* _root;
};

2.1查找

根据它的定义查找就是,跟节点比,比节点大就去它的右子树中寻找,比他小就去它的左子树中寻找

	bool 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 true;
			}
		}
		return false;
	}

2.2插入

在这里插入图片描述
插入也很简单,例如上图我们要插入16,我们就先找到要插入的位置,然后为了方便我们记录整个过程我们需要一个父节点。

	bool Insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			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;
			}
		}
		cur = new Node(key);
		if (parent->_key > key)
		{
			parent->_left=cur;
		}
		else
		{
			parent->_right=cur;
		}
		return true;
	}

现在可以进行测试代码的正确性。
在这里插入图片描述
注意,搜索二插入要有序它走的是中序遍历。

2.3删除(重要

删除比插入麻烦的多,我们删除值原则就是要保证它还是搜索二叉树,它的性质不可以改变。这就挺麻烦的,所以我们在删除这里用替换删除
替换删除:找到一个可以替换的节点,交换值,转换删除它。
可以替换的节点是:左子树的最大or右子树的最小。

下面我们来分析一下删除的各种情况

  1. 删除孩子节点
  2. 删除只有一个孩子的节点
  3. 删除两个孩子的节点
    这里总结下,情况1和2可以归为一类。情况3我们就要用到替换删除法

情况1(无孩子&&一个孩子)

在这里插入图片描述

//找到了开始删除
				//第一种
				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;
					return true;
				}
				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;
					return true;
				}

###情况二(两个孩子)
在这里插入图片描述
找到右子树的最小,把它的值复制给cur,就转换成了删除叶子节点

				else//第二种(俩孩子)替换删除法
				{
					Node* rightMinparent = cur;
					Node* rightMin = cur->_right;
					while (cur->_right)
					{
						rightMinparent = rightMin;
						rightMin = rightMin->_left;
					}
					cur->_key = rightMin->_key;
					if (rightMin == rightMinparent->_left)
					{
						rightMinparent->_left = rightMin->_right;
					}
					else
					{
						rightMinparent->_right = rightMin->_right;
					}
					delete rightMin;
					return true;
				}

3.二叉搜索树的应用

3.1K模型

K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到
的值。
比如:给一个单词word,判断该单词是否拼写正确。这个就是普通的二叉搜索树

3.2KV模型

KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。
通过key值快速查找另外一个值在不在。我们用二叉搜索树这个用的是比较多的
生活中的KV模型例如:商场车库,进去都可以进入(记录车牌(key)进入时间(value))
出:付费出(通过车牌(key),查询进入的时间(value))。
还有字典查询,高铁身份证进站等等。

3.2.1KV模型的实现

这个就是多加了一个对象,实现直接CV上面的代码

namespace key_value
{
	template<class K, class V>
	struct BSTreeNode
	{
		typedef BSTreeNode<K, V> Node;
		Node* _left;
		Node* _right;
		K _key;
		V _value;

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


	template<class K, class V>
	class BSTree
	{
		typedef BSTreeNode<K, V> Node;
	public:
		//插入
		bool Insert(const K& key, const V& value)
		{
			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->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return true;
				}
			}

			cur = new Node(key, value);
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = 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;
		}
		//中序遍历
		void _InOrder(Node* root)
		{
			if (root == nullptr)
				return;
			_InOrder(root->_left);
			std::cout << root->_val << " ";
			_InOrder(root->_right);
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}
		//3.删除
		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_val < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_val > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				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 (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else//我是父亲的右节点
							{
								parent->_right = cur->_left;
							}
						}
						delete cur;
						return true;
					}

					else//第二种(有两个孩子)替换删除法
					{
						Node* rightMinparent = cur;
						Node* rightMin = cur->_right;
						while (rightMin->_left)
						{
							rightMinparent = rightMin;
							rightMin = rightMin->_left;
						}
						cur->_val = rightMin->_val;
						if (rightMin == rightMinparent->_left)
							rightMinparent->_left = rightMin->_right;
						else
							rightMinparent->_right = rightMin->_right;
						delete rightMin;
						return true;
					}
				}
			}
			return false;
		}
		//void InOrder()
		//{
		//	_InOrder(_root);
		//	cout << endl;
		//}
	//private:
	//	void _InOrder(Node* root)
	//	{
	//		if (root == nullptr)
	//			return;

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

	private:
		Node* _root = nullptr;
	};
}

我们可以用哥=个例子来玩一玩这个KV模型

void TestBSTree()
{
	key_value::BSTree<string, string> dict;
	dict.Insert("insert", "插入");
	dict.Insert("erase", "删除");
	dict.Insert("left", "左边");
	dict.Insert("string", "字符串");

	string str;
	while (cin >> str)
	{
		auto ret = dict.Find(str);
		if (ret)
		{
			cout << str << ":" << ret->_value << endl;
		}
		else
		{
			cout << "单词拼写错误" << endl;
		}
	}
}

在这里插入图片描述

总结

总的来说二叉搜索树,比较难的地方就是删除部分,多画图多思考。
下篇我们就要深入二叉搜索树。

二叉搜索树源代码

#pragma once
#include<iostream>
using namespace std;
//struct BinarySearchTreeNode
template<class K>
struct BSTreeNode
{
	typedef BSTreeNode<K> Node;
	Node* _left;
	Node* _right;
	K _key;

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

//class BinarySearchTree
template<class K>
class BSTree
{
	typedef BSTreeNode<K> Node;
public:
	BSTree() = default;
	BSTree(const BSTree<K>& t)
	{
		_root = copy(t._root);
	}
	Node* copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;
		Node* newroot = new Node(root->_val);
		newroot->_left = copy(root->_left);
		newroot->_right = copy(root->_right);
		return newroot;
	}

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

	bool 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 true;
			}
		}
		return false;
	}

	bool Insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			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;
			}
		}
		cur = new Node(key);
		if (parent->_key > key)
		{
			parent->_left=cur;
		}
		else
		{
			parent->_right=cur;
		}
		return true;
	}
	bool Erase(const K& key)
	{
		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
			{
				//找到了开始删除
				//第一种
				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;
					return true;
				}
				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;
					return true;
				}
				else//第二种(俩孩子)替换删除法
				{
					Node* rightMinparent = cur;
					Node* rightMin = cur->_right;
					while (cur->_right)
					{
						rightMinparent = rightMin;
						rightMin = rightMin->_left;
					}
					if (rightMinparent->_left == rightMin)
					{
						rightMinparent->_left = rightMin->_right;
					}
					else
					{
						rightMinparent->_right = rightMin->_right;
					}
				}
			}
		}
		return false;
	}
	void Inorder()
	{
		_Inorder(_root);
		cout << endl;
	}
private:
	void _Inorder(Node* root)
	{
		if (root == nullptr)
			return;
		_Inorder(root->_left);
		cout << root->_key << " ";
		_Inorder(root->_right);
	}
private:
	Node* _root=nullptr;
};

  • 32
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tpoog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值