二叉搜索树C++实现

概述

二叉搜索树又称二叉排序树,具有以下性质:

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

注意:二叉搜索树中序遍历的结果是有序的

高度:从根到叶子的单一路径上,最大节点个数为高度,H=log(n)

在这里插入图片描述

class BST
	{
		struct BSTNode
		{
			BSTNode* left;
			BSTNode* right;
			BSTNode* parent;
			float value;

			BSTNode(float _value, BSTNode* _left = nullptr, BSTNode* _right = nullptr, BSTNode* _parent = nullptr)
				:left(_left), right(_right), parent(_parent), value(_value)
			{
			}
		};

		BSTNode* root = nullptr;
		BSTNode* find(BSTNode* _node, float _value);
		void transplant(BSTNode* found, BSTNode* replacement);

		BSTNode* predecessor(BSTNode*);
		BSTNode* successor(BSTNode*);

		bool isALeaf(BSTNode* _node);

		BSTNode* splitNode(float _min,float _max);

	public:
		BST() {}
		BST(std::vector<float> _values, const unsigned int _index = 0);

		void insert(float _value);
		bool find(float _value);
		//范围查询
		void find(const float _min, const float _max, std::vector<float>& _list);;
		bool remove(float _value);

		BSTNode* minimun(BSTNode* _node = nullptr);
		BSTNode* maximun(BSTNode* _node = nullptr);

		bool predecessor(float _value, float& _predecessor);
		bool successor(float _value, float& successor);
		//前中后序遍历
		void preOrderTraves(BSTNode*, std::vector<float>&);
		void inOrderTraves(BSTNode*, std::vector<float>&);
		void postOrderTraves(BSTNode*, std::vector<float>&);
	};

插入操作

在这里插入图片描述

void insert(float _value)
{
	if (!root)
	{
		root = new BSTNode(_value);
		return;
	}
	else
	{
		auto temp = root;
		while (temp)
		{
			if (_value < temp->value)
			{
				if (temp->left)
					temp = temp->left;
				else
				{

					temp->left = new BSTNode(_value);
					break;
				}
			}
			else
			{
				if (temp->right)
					temp = temp->right;
				else
				{
					temp->right = new BSTNode(_value);
					break;
				}
			}
		}
	}
}

前序、中序、后续遍历

void yhaida::BST::preOrderTraves(BSTNode* _node, std::vector<float>& _list)
{
	if (!_node) return;
	_list.push_back(_node->value);
	preOrderTraves(_node->left,_list);
	preOrderTraves(_node->right, _list);
}

void yhaida::BST::inOrderTraves(BSTNode* _node, std::vector<float>& _list)
{
	if (!_node) return;
	preOrderTraves(_node->left, _list);
	_list.push_back(_node->value);
	preOrderTraves(_node->right, _list);
}

void yhaida::BST::postOrderTraves(BSTNode* _node, std::vector<float>& _list)
{
	if (!_node) return;
	preOrderTraves(_node->left, _list);
	preOrderTraves(_node->right, _list);
	_list.push_back(_node->value);
}

搜寻

BST::BSTNode* BST::find(BSTNode* _node, float _value)
{
	auto current = _node;
	while (current && current->value!= _value)
	{
		if (current->value < _value)
			current = current->right;

		else
			current = current->right;
	}
	return current;
}

bool :BST::find(float _value)
{
	auto result = find(root, _value);
	if (!result)
		return false;
	return true;
}

最小值与最大值

float BST::minimun(BSTNode* _node)
{
	if (!_node)
		_node = root;

	auto temp = _node;

	while (temp->left)
		temp = temp->left;

	return temp->value;
}

float BST::maximun(BSTNode* _node)
{
	if (!_node)
		_node = root;

	auto temp = _node;

	while (temp->right)
		temp = temp->right;

	return temp->value;
}

前驱点、后驱点

private:
BST::BSTNode* BST::predecessor(BSTNode* _node)
{
	if (_node->left)
		return maximun(_node->left);
	else
	{
		auto temp = _node;
		while (temp->parent&& temp == temp->parent->left)
		{
			temp = temp->parent;
		}
		return temp->parent;
	}
}
BST::BSTNode* BST::successor(BSTNode* _node)
{
	if (_node->right)
		return minimun(_node->right);
	else
	{
		auto temp = _node;
		while (temp->parent && temp == temp->parent->right)
		{
			temp = temp->parent;
		}
		return temp->parent;
	}
	return nullptr;
}
public:
bool BST::predecessor(float _value, float& _predecessor)
{
	auto val_ptr = find(root, _value);
	if (!val_ptr)
		return false;
	auto ret = predecessor(val_ptr);
	if (!ret)
		return false;
	_predecessor = ret->value;
	return true;
}

bool BST::successor(float _value, float& _successor)
{
	auto val_ptr = find(root, _value);
	if (!val_ptr)
		return false;
	auto ret = successor(val_ptr);
	if (!ret)
		return false;
	_successor = ret->value;
	return true;
}

delete

bool BST::remove(float _value)
{
	BSTNode* current_node = find(root, _value);
	if (current_node)
	{
		BSTNode* current_left = current_node->left;
		BSTNode* current_right = current_node->right;

		if (isALeaf(current_node))
		{
			transplant(current_left, nullptr);
		}
		else if (!current_left)
		{
			transplant(current_left, current_right);
		}
		else if (!current_right)
		{
			transplant(current_left, current_left);
		}
		else
		{
			BSTNode* right_min = minimun(current_right);

			if (right_min->parent != current_node)
			{
				transplant(right_min, right_min->right);
				right_min->right = current_right;
				right_min->right->parent = right_min;
			}

			transplant(current_node, right_min);
			right_min->left = current_left->left;
			right_min->left->parent = right_min;
		}
	}
	return false;
}

范围查询

在这里插入图片描述

BST::BSTNode* BST::splitNode(float _min, float _max)
{
	auto v = root;
	while (!isALeaf(v) && (_max <= v->value||_min>=v->value))
	{
		if (_max <= v->value)
			v = v->left;
		else
			v = v->right;
	}
	return v;
}
//***************************************************************************
void BST::find(const float _min, const float _max, std::vector<float>& _list)
{
	auto v_split = splitNode(_min, _max);

	if (isALeaf(v_split))
	{
		if (v_split->value >= _min && v_split->value < _max)
			_list.push_back(v_split->value);
	}
	else
	{
		//左子树
		auto v = v_split->left;
		while (!isALeaf(v))
		{
			if (v->value >= _min)
			{
				inOrderTraves(v->right, _list);
				_list.push_back(v->value);
				v = v->left;
			}
			else
			{
				v = v->right;
			}
		}
		if (v->value >= _min)
			_list.push_back(v->value);

		//右子树
		auto v = v_split->right;
		while (!isALeaf(v))
		{
			if (v->value <= _max)
			{
				inOrderTraves(v->left, _list);
				_list.push_back(v->value);
				v = v->right;
			}
			else
			{
				v = v->left;
			}
		}
		if (v->value <= _max)
			_list.push_back(v->value);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yhaida

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

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

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

打赏作者

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

抵扣说明:

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

余额充值