【C++搜索二叉树】1.二叉搜索树概念2.二叉搜索树非递归实现

目录

1.二叉搜索树概念

2.二叉搜索树非递归实现

2.3搜索二叉树的删除(最重要也是最有难度的接口)

 3.(key)二叉搜索树递归实现 

 4.(KV)二叉搜索树递归实现 


1.二叉搜索树概念

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

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树(任何一个节点都满足左子树都小于头节点,右子树都大于头节点)

下面都大部分例子都由下面这可搜索二叉树来距离:

2.1 二叉搜索数的查找一个节点时间复杂度O(N),应是最坏

2.二叉搜索树非递归实现

2.1二叉搜索树的插入

思路: 

bool Insert(const K& key)
	{
		//空树直接插入
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}
		Node* cur = _root;
		//保留要插入位置的父节点
		Node* parent = nullptr;
		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;
			return true;
		}
		else
		{
			parent->_right = cur;
			return true;
		}
	}

2.2搜索二叉树找

思路和插入差不多不赘述

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;
	}

2.3搜索二叉树的删除(最重要也是最有难度的接口)

处理带两个节点方法:替换法,选择key节点左子树的最大或者右子树最小的节点,把值给key节点,删除的就是带一个节点的了;

bool Erase(const K& key)//删除
	{
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			//寻找key
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				//找到key,如果只有一个子节点
				if (cur->_left == nullptr)
				{
					if (cur=_root)
					{
						//没有左子树cur还是头节点
						_root = cur->_right;
						delete cur;
					} 
					else
					{
						//cur是parent是左还是右不明确
						if (parent->_left == cur) {
							parent->_left = cur->_right;
						}
						else {
							parent->_right = cur->_right;
						}
						delete cur;
					}
				}
				else if (cur->_right == nullptr)
				{
					//没有左子树cur还是头节点
					if (cur = _root)
					{
						_root = cur->_left;
						delete cur;
					}
					else
					{
						if (parent->_left == cur) {
							parent->_left = cur->_left;
						}
						else {
							parent->_right = cur->_left;
						}
						delete cur;
					}
				}
				else
				{
					parent = cur;//保存key节点
					Node* curparent = cur;//保存cur的父节点
					cur = cur->_right;
					//右子树最小或者左子树最大
					while (cur->_left != nullptr)
					{
						curparent = cur;
						cur = cur->_left;
					}
					parent->_key = cur->_key;// 右子树最小替换
					if (cur->_left == nullptr)
					{
						//cur是parent是左还是右不明确
						if (curparent->_left == cur) {
							curparent->_left = cur->_right;
						}
						else {
							curparent->_right = cur->_right;
						}
					}
					else if (cur->_right == nullptr)
					{
						if (curparent->_left == cur) {
							curparent->_left = cur->_left;
						}
						else {
							curparent->_right = cur->_left;
						}
					}
					delete cur;
				}
				return true;
			}
		}
		return false;
	}

2.4二叉搜索数全部代码 

#pragma once
#include <iostream>
using namespace std;

template<class K>
struct BSTreeNode
{
	BSTreeNode(const K& key)
		:_left(nullptr)
		,_right(nullptr)
		,_key(key)
	{}

	BSTreeNode<K>* _left;
	BSTreeNode<K>* _right;
	K _key;
};

template<class K>
class BSTree
{
	typedef BSTreeNode<K> Node;
public:
	BSTree()
		:_root(nullptr)
	{}
	bool Insert(const K& key)
	{
		//空树直接插入
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}
		Node* cur = _root;
		//保留要插入位置的父节点
		Node* parent = nullptr;
		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;
			return true;
		}
		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)
		{
			//寻找key
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				//找到key,如果只有一个子节点
				if (cur->_left == nullptr)
				{
					if (cur=_root)
					{
						//没有左子树cur还是头节点
						_root = cur->_right;
						delete cur;
					} 
					else
					{
						//cur是parent是左还是右不明确
						if (parent->_left == cur) {
							parent->_left = cur->_right;
						}
						else {
							parent->_right = cur->_right;
						}
						delete cur;
					}
				}
				else if (cur->_right == nullptr)
				{
					//没有左子树cur还是头节点
					if (cur = _root)
					{
						_root = cur->_left;
						delete cur;
					}
					else
					{
						if (parent->_left == cur) {
							parent->_left = cur->_left;
						}
						else {
							parent->_right = cur->_left;
						}
						delete cur;
					}
				}
				//else ///2、左右都不为空,替换法删除
				//{
				//	Node* minRight = cur->_right;
				//	while (minRight->_left)
				//	{
				//		minRight = minRight->_left;
				//	}
				//	K min = minRight->_key;

				//	// 递归调用自己去删除替换节点,一定会走到左为空的情况处理
				//	this->Erase(min);

				//	cur->_key = min;
				//}
				else
				{
					parent = cur;//保存key节点
					Node* curparent = cur;//保存cur的父节点
					cur = cur->_right;
					//右子树最小或者左子树最大
					while (cur->_left != nullptr)
					{
						curparent = cur;
						cur = cur->_left;
					}
					parent->_key = cur->_key;// 右子树最小替换
					if (cur->_left == nullptr)
					{
						//cur是parent是左还是右不明确
						if (curparent->_left == cur) {
							curparent->_left = cur->_right;
						}
						else {
							curparent->_right = cur->_right;
						}
					}
					else if (cur->_right == nullptr)
					{
						if (curparent->_left == cur) {
							curparent->_left = cur->_left;
						}
						else {
							curparent->_right = cur->_left;
						}
					}
					delete cur;
				}
				return true;
			}
		}
		return false;
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}
	void InOrder()
	{
		_InOrder(_root);
		cout<<endl;
	}
private:
	Node* _root;
};

 3.(key)二叉搜索树递归实现 

key的搜索场景(适用于在不在的问题)

  1. 宿舍楼门禁:把学生的学号存在key搜索二叉树内,查询是否这个学号
  2. 小区车库扫描抬杠系统,将车牌号录入系统
namespace key
{
	template<class K>
	struct BinarySearchNode
	{
		BinarySearchNode(const K& key)
			:_left(nullptr)
			, _right(nullptr)
			, _key(key)
		{}
		BinarySearchNode<K>* _left;
		BinarySearchNode<K>* _right;
		K _key;
	};
	template<class K>
	class BSTree
	{
		typedef BinarySearchNode<K> Node;
	public:
		BSTree()
			:_root(nullptr)
		{}
		Node* FindR(const K& key)
		{
			return _FindR(_root, key);
		}
		bool InsertR(const K& key)
		{
			return _InsertR(_root, key);
		}
		bool EraseR(const K& key)
		{
			return _EraseR(_root, key);
		}
		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}
		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}
		~BSTree()
		{
			_Destory(_root);
			_root = nullptr;
		}
		BSTree(const BSTree<K>& t)
		{
			_root = Copy(t._root);
		}
		BSTree<K>& operator=(BSTree<K> t)
		{
			swap(_root, t._root);
			return *this;
		}
	private:
		Node* _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
			{
				return nullptr;
			}
			if (root->_key < key)
			{
				return _FindR(root->left, key);
			}
			else if (root->_key > key)
			{
				return _FindR(root->right, key);
			}
			else
			{
				return root;
			}
		}
		bool _InsertR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				root = new Node(key);
				return true;
			}
			if (root->_key < key)
			{
				return _InsertR(root->_right, key);//引用别名,不需要多建一个父节点变量
			}
			else if (root->_key > key)
			{
				return _InsertR(root->_left, key);

			}
			else
			{
				return false;
			}
		}
		bool _EraseR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				return false;
			}
			if (root->_key < key)
			{
				return _EraseR(root->_right, key);
			}
			else if (root->_key > key)
			{
				return _EraseR(root->_left, key);

			}
			else
			{
				if (root->_left == nullptr)
				{
					Node* del = root;
					root = root->_right;
					delete del;
				}
				else if (root->_right == nullptr)
				{
					Node* del = root;
					root = root->_left;
					delete del;
				}
				else
				{
					Node* minRight = root->_right;
					while (minRight->_left)
					{
						minRight = minRight->_left;
					}
					K min = minRight->_key;

					// 转换成在root的右子树删除min
					_EraseR(root->_right, min);
					root->_key = min;
				}
				return true;
			}
		}
		void _Destory(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_Destory(root->_left);
			_Destory(root->_right);
			delete root;
		}
		Node* Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}
			Node* copyNode = new Node(root->_key);
			copyNode->_left = Copy(root->_left);
			copyNode->_right = Copy(root->_right);
			return copyNode;
		}

		Node* _root;
	};
}

 4.(KV)二叉搜索树递归实现 

搜索场景

  1. 买票进站,可以依靠你的身份证,查到你的信息
  2. 简单的中英翻译字典

 3.统计一个文本出现次数

namespace KV
{
	template<class K, class V>
	struct BSTreeNode
	{
		BSTreeNode<K, V>* _left;
		BSTreeNode<K, V>* _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;
	private:
		// 如果树中已经存在key,返回false
		bool _InsertR(Node*& root, const K& key, const V& value)
		{
			if (root == NULL)  // 插入
			{
				root = new Node(key, value);
				return true;
			}

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

		Node* _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

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

		// 如果树中不存在key,返回false
		// 存在,删除后,返回true
		bool _EraseR(Node*& root, const K& key)
		{
			if (root == NULL)
			{
				return false;
			}

			if (root->_key < key)
			{
				return _EraseR(root->_right, key);
			}
			else if (root->_key > key)
			{
				return _EraseR(root->_left, key);
			}
			else
			{
				// 找到了,root就是要删除的节点
				if (root->_left == nullptr)
				{
					Node* del = root;
					root = root->_right;
					delete del;
				}
				else if (root->_right == nullptr)
				{
					Node* del = root;
					root = root->_left;
					delete del;
				}
				else
				{
					Node* minRight = root->_right;
					while (minRight->_left)
					{
						minRight = minRight->_left;
					}
					K kmin = minRight->_key;
					V vMin = minRight->_value;

					// 转换成在root的右子树删除min
					_EraseR(root->_right, kmin);
					root->_key = kmin;
					root->_value = vMin;
				}

				return true;
			}
		}

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

			_Destory(root->_left);
			_Destory(root->_right);
			delete root;
		}

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

			Node* copyNode = new Node(root->_key, root->_value);
			copyNode->_left = _Copy(root->_left);
			copyNode->_right = _Copy(root->_right);

			return copyNode;
		}

	public:
		BSTree()
			:_root(nullptr)
		{}

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

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

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

		// 涉及深浅拷贝,需要实现拷贝构造 operator=等

		bool InsertR(const K& key, const V& value)
		{
			return _InsertR(_root, key, value);
		}

		Node* FindR(const K& key)
		{
			return _FindR(_root, key);
		}

		bool EraseR(const K& key)
		{
			return _EraseR(_root, key);
		}

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

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

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

	private:
		Node* _root;
	};
}

  • 16
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值