C++中如何实现搜索二叉树
创始人
2024-12-21 15:02:28
0次

    二叉查找树(英语:Binary Search Tree),也称二叉搜索树、有序二叉树(英语:ordered binary tree),排序二叉树(英语:sorted binary tree),是指一棵空树或者具有下列性质的二叉树:

  • 任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;

  • 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;

  • 任意节点的左、右子树也分别为二叉查找树;

  • 没有键值相等的节点。

#pragma once

template
struct BSTreeNode
{
	K _key;
	V _value;
	BSTreeNode* _left;
	BSTreeNode* _right;

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

template
class BSTree
{
	typedef BSTreeNode Node;
public:
	BSTree()
		:_root(NULL)
	{}

	bool Insert(const K& key, const V& value)
	{
		if (NULL == _root)//若为空树
		{
			_root = new Node(key, value);
			return true;
		}

		Node* parent = NULL;
		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//已经存在key
			{
				return false;
			}
		}

		//插入节点
		if (key > parent->_key)
			parent->_right = new Node(key, value);
		else
			parent->_left = new Node(key, value);
	}

	//Insert递归写法
	bool InsertR(const K& key, const V& value)
	{
		return _InsertR(_root, key, value);
	}

	bool _InsertR(Node*& root, const K& key, const V& value)
	{
		if (NULL == root)
		{
			root = new Node(key, value);
			return true;
		}

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

	Node* Find(const K& key)
	{
		Node* cur = _root;

		while (cur)
		{
			if (key > cur->_key)
				cur = cur->_right;
			else if (key < cur->_key)
				cur = cur->_left;
			else
				return cur;
		}

		return NULL;
	}

	//Find递归写法
	Node* FindR(const K& key)
	{
		return _FindR(_root, key);
	}

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

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

	bool Remove(const K& key)
	{
		Node* parent = NULL;
		Node* cur = _root;

		//确定删除节点的位置
		while (cur)
		{
			if (key > cur->_key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (key < cur->_key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				break;
			}
		}

		if (NULL == cur)//没有该节点
		{
			return false;
		}

		Node* del;
		if (NULL == cur->_left)//删除节点的左孩子为空
		{
			del = cur;

			//删除的节点为根节点
			if (NULL == parent)
			{
				_root = _root->_right;
			}
			else
			{
				if (cur == parent->_left)
					parent->_left = cur->_right;
				else
					parent->_right = cur->_right;
			}
		}
		else if (NULL == cur->_right)//删除节点的右孩子为空
		{
			del = cur;

			if (NULL == parent)
			{
				_root = _root->_left;
			}
			else
			{
				if (cur == parent->_left)
					parent->_left = cur->_right;
				else
					parent->_right = cur->_left;
			}
		}
		else//删除节点的左右孩子都不为空,找右子树最左节点代替该节点删除
		{
			parent = cur;

			Node* leftmost = cur->_right;
			while (leftmost->_left)
			{
				parent = leftmost;
				leftmost = leftmost->_left;
			}

			del = leftmost;

			cur->_key = leftmost->_key;
			cur->_value = leftmost->_value;

			if (leftmost == parent->_left)
				parent->_left = leftmost->_right;
			else
				parent->_right = leftmost->_right;
		}

		return true;
	}

	//Remove递归写法
	bool RemoveR(const K& key)
	{
		return _RemoveR(_root, key);
	}
	
	bool _RemoveR(Node*& root, const K& key)
	{
		if (NULL == root)
			return false;
		
		if (key > root->_key)
		{
			return _RemoveR(root->_right, key);
		}
		else if (key < root->_key)
		{
			return _RemoveR(root->_left, key);
		}
		else
		{
			Node* del = root;

			if (NULL == root->_left)
			{
				root = root->_right;
			}
			else if (NULL == root->_right)
			{
				root = root->_left;
			}
			else
			{
				Node* leftmost = root->_right;
				while (leftmost->_left)
				{
					leftmost = leftmost->_left;
				}

				swap(root->_key, leftmost->_key);
				swap(root->_value, leftmost->_value);

				return _RemoveR(root->_right, key);
			}

			delete del;
		}

		return true;
	}

	//中序遍历递归写法
	void InOrder()
	{
		_InOrder(_root);
	}

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

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

protected:
	Node* _root;
};


void Test()
{
	BSTree t;
	int a[] = {5, 3, 4, 1, 7, 8, 2, 6, 0, 9};
	for (size_t i = 0; i < sizeof(a)/sizeof(a[0]);++i)
	{
		t.InsertR(a[i], i);
	}

	cout<_key<_key<_key<

C++中如何实现搜索二叉树

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...