【数据结构】二叉搜索树

目录

一. 二叉搜索树

 1.1 二叉搜索树概念

 1.2 二叉搜索树操作

 1.3 二叉搜索树的实现

 1.4 二叉搜索树的应用

 1.5 二叉搜索树的性能分析


一. 二叉搜索树

 1.1 二叉搜索树概念

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

  1. 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  2. 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  3. 它的左右子树也分别为二叉搜索

 1.2 二叉搜索树操作

 【1】. 二叉搜索树的查找

原理

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

【2】. 二叉搜索树的插入
 

插入的具体过程如下:

  • a. 树为空,则直接新增节点,赋值给root指针
  • b. 树不空,按二叉搜索树性质查找插入位置,插入新节点

【3】.二叉搜索树的删除

        首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:

        a. 要删除的结点无孩子结点
        b. 要删除的结点只有左孩子结点
        c. 要删除的结点只有右孩子结点
        d. 要删除的结点有左、右孩子结点

看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程
如下:
情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点  --  直接删除
情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点  --  直接删除


情况d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点
中,再来处理该结点的删除问题--替换法删除

 

 1.3 二叉搜索树的实现

namespace key
{
	template <class K>
	class BSTreeNode
	{
	public:
		K _key;//存的值
		BSTreeNode* _left;//左节点
		BSTreeNode* _right;//右节点

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

	};

	template<class K>
	class BSTree
	{
		typedef BSTreeNode<K> Node;
	public:
		BSTree() {}
		

		BSTree(const BSTree<K>& t)
		{
			_root = _Copy(t._root);
		}

		~BSTree()
		{
			_Destory(_root);
		}

        //插入
		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}

			Node* parent = nullptr;
			Node* tmp = _root;

			while (tmp)
			{
				if (key < tmp->_key)
				{
					parent = tmp;
					tmp = tmp->_left;
				}
				else if (key > tmp->_key)
				{
					parent = tmp;
					tmp = tmp->_right;
				}
				else
				{
					return false;
				}
			}
			tmp = new Node(key);

			if (tmp->_key < parent->_key)
			{
				parent->_left = tmp;
			}
			else
			{
				parent->_right = tmp;
			}
			return true;
		}
        
        //查找
		bool Find(const K& key)
		{
			Node* root = _root;
			while (root)
			{
				if (key > root->_key)
				{
					root = root->_right;
				}
				else if (key < root->_key)
				{
					root = root->_left;
				}
				else
				{
					return true;
				}
			}
			return false;
		}
        
        //删除
		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (key < cur->_key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					//开始删除

					//左为空
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = _root->_right;
						}
						else
						{
							if (cur == parent->_left)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}
						delete cur;
						cur = nullptr;
					}
					//右为空
					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;
						cur = nullptr;
					}
					//左右都不为空
					else
					{
						//替换法删除
						Node* rightMin = cur->_right;
						Node* minParent = cur;
						//找右字数最小节点
						while (rightMin->_left)
						{
							minParent = rightMin;
							rightMin = rightMin->_left;
						}

						swap(rightMin->_key, cur->_key);
						if (minParent->_left == rightMin)
						{
							minParent->_left = rightMin->_right;
						}
						else
						{
							minParent->_right = rightMin->_right;
						}
						delete rightMin;
						rightMin = nullptr;
					}
					return true;
				}
			}
			return false;
		}


		void InOrder()
		{
			_InOrder(_root);

			cout << endl;
		}

		//t2 = t1
		BSTree<K>& operator=(BSTree<K> t)
		{
			swap(_root, t._root);
			return *this;
		}
	private:

		Node* _Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}
			Node* copyRoot = new Node(root->_key);
			copyRoot->_left = _Copy(root->_left);
			copyRoot->_right = _Copy(root->_right);

			return copyRoot;
		}

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

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

		}

	private:
		Node* _root = nullptr;

	};
}

递归版实现

using namespace std;

namespace key
{
	template <class K>
	class BSTreeNode
	{
	public:
		K _key;
		BSTreeNode* _left;
		BSTreeNode* _right;

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

	};

	template<class K>
	class BSTree
	{
		typedef BSTreeNode<K> Node;
	public:
		BSTree() {}
		//c++11:强制编译器生成默认构造
		//BSTree() = default;
		BSTree(const BSTree<K>& t)
		{
			_root = _Copy(t._root);
		}

		~BSTree()
		{
			_Destory(_root);
		}


		void InOrder()
		{
			_InOrder(_root);

			cout << endl;
		}

		//递归版
	//////////////////////////////////////////////////////////
		bool FindR(const K& key)
		{
			return _Find(_root, key);
		}
		bool InsertR(const K& key)
		{
			return _InsertR(_root, key);
		}
		bool EraseR(const K& key)
		{
			return _EraseR(_root, key);
		}
		/////////////////////////////////////////////////////////
			//t2 = t1
		BSTree<K>& operator=(BSTree<K> t)
		{
			swap(_root, t._root);
			return *this;
		}
	private:

		Node* _Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}
			Node* copyRoot = new Node(root->_key);
			copyRoot->_left = _Copy(root->_left);
			copyRoot->_right = _Copy(root->_right);

			return copyRoot;
		}

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

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

		}

		//递归版查找
		bool _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
			{
				return false;
			}
			if (key < root->_key)
			{
				_FindR(root->_left, key);
			}
			else if (key > root->_key)
			{
				_FindR(root->_right, key);
			}
			else
			{
				return true;
			}
		}

        //递归版插入
		bool _InsertR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				root = new Node(key);
				return true;
			}
			if (key < root->_key)
			{
				return _InsertR(root->_left, key);
			}
			else if (key > root->_key)
			{
				return _InsertR(root->_right, key);
			}
			else
			{
				return false;
			}
		}

        //递归版删除
		bool _EraseR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				return false;
			}
			if (key < root->_key)
			{
				_EraseR(root->_left, key);
			}
			else if (key > root->_key)
			{
				_EraseR(root->_right, key);
			}
			else
			{
				//root是上一层的左指针或者右指针的别名,就可以直接写
				Node* del = root;
				if (root->_left == nullptr)
				{
					root = root->_right;
				}
				else if (root->_right == nullptr)
				{
					root = root->_left;
				}
				else
				{
					//找右树的最小节点
					Node* min = root->_right;
					while (min->_left)
					{
						min = min->_left;
					}
					swap(root->_key, min->_key);
					//删除,虽然整个树在交换后结构改变,不是二叉搜索树了
					//但是在这个子树里面依旧是一个二叉搜索树
					return _EraseR(root->_right, key);
				}
				delete del;
				return true;
			}
		}

	private:
		Node* _root = nullptr;

	};
}

 1.4 二叉搜索树的应用

 1. K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到
的值。

比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:

  • 以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
  • 在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误

2. KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方
式在现实生活中非常常见:

  • 比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对;
  • 再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是<word, count>就构成一种键值对

改造二叉搜索树成<Key, Value>结构

using namespace std;
//	Key/Value的模型 -- 通过Key去找Value
namespace KeyValue
{
	template <class K, class V>
	class BSTreeNode
	{
	public:
		K _key;
		V _value;
		BSTreeNode<K, V>* _left;
		BSTreeNode<K, V>* _right;

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

	};

	template<class K, class V>
	class BSTree
	{
		typedef BSTreeNode<K, V> Node;
	public:
		BSTree() {}
		//c++11:强制编译器生成默认构造
		//BSTree() = default;
		BSTree(const BSTree<K, V>& t)
		{
			_root = _Copy(t._root);
		}

		~BSTree()
		{
			_Destory(_root);
		}

		bool Insert(const K& key,const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key, value);
				return true;
			}
			Node* parent = nullptr;
			Node* tmp = _root;
			while (tmp)
			{
				if (key < tmp->_key)
				{
					parent = tmp;
					tmp = tmp->_left;
				}
				else if (key > tmp->_key)
				{
					parent = tmp;
					tmp = tmp->_right;
				}
				else
				{
					return false;
				}
			}
			tmp = new Node(key, value);

			if (tmp->_key < parent->_key)
			{
				parent->_left = tmp;
			}
			else
			{
				parent->_right = tmp;
			}
			return true;
		}

		Node* Find(const K& key)
		{
			Node* root = _root;
			while (root)
			{
				if (key > root->_key)
				{
					root = root->_right;
				}
				else if (key < root->_key)
				{
					root = root->_left;
				}
				else
				{
					return root;
				}
			}
			return nullptr;
		}



		void InOrder()
		{
			_InOrder(_root);

			cout << endl;
		}

	private:

		Node* _Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}
			Node* copyRoot = new Node(root->_key);
			copyRoot->_left = _Copy(root->_left);
			copyRoot->_right = _Copy(root->_right);

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

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

		}

	private:
		Node* _root = nullptr;
	};
}

1.5 二叉搜索树的性能分析


插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的
深度的函数,即结点越深,则比较次数越多。
但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:

最优情况下,二叉搜索树为完全二叉树,其平均比较次数为:log2^N
最差情况下,二叉搜索树退化为单支树,其平均比较次数为:N/2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值