c++之二叉搜索树

二叉搜索树及其基本操作

1.二叉搜索树的概念

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

  • 若它的左子树不为空,则其左子树上所有节点的值都小于其根结点的值
  • 若它的右子树不为空,则其右子树上所有节点的值都大于其根结点的值
  • 它的左右子树也都是二叉搜索树
    2.二叉搜索树的基本操作

2.1二叉搜索树的查找
原理:
若根结点不为空:
如果根结点key==查找key 返回true
如果根结点key>查找key 在其左子树查找
如果根结点key<查找key 在其右子树查找
代码实现:

Node* Find(const T& data) {
		if (_pRoot == nullptr) {
			return NULL;
		}
		Node* pCur = _pRoot;
		while (pCur) {
			if (data == pCur->_data) {
				return pCur;
			}
			else if (data < pCur->_data) {
				pCur = pCur->_pLeft;
			}
			else {
				pCur = pCur->_pRight;
			}
		}
		return nullptr;
	}

2.2二叉搜索树的插入
插入过程:
1. 树为空,直接插入
2. 树不空,按二叉搜索树性质找到插入位置,插入结点
代码如下:

	bool Insert(const T& data) {
		if (_pRoot == nullptr) {
			_pRoot = new Node(data);
			return true;
		}
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur) {
			pParent = pCur;
			if (data < pCur->_data) {
				pCur = pCur->_pLeft;
			}
			else if (data > pCur->_data) {
				pCur = pCur->_pRight;
			}
			else {
				return false;
			}
		}
		pCur = new Node(data);
		if (data < pParent->_data) {
			pParent->_pLeft = pCur;
		}
		else {
			pParent->_pRight = pCur;
		}
		return true;
	}

2.3 二叉搜索树的删除

首先查找元素是否在二叉搜索树中,如果不存在,则返回

bool Delete(const T& data) {
		if (_pRoot == nullptr) {
			return false;
		}
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur) {
			if (pCur->_data == data) {
				break;
			}
			else if (data < pCur->_data) {
				pParent = pCur;
				pCur = pCur->_pLeft;
			}
			else {
				pParent = pCur;
				pCur = pCur->_pRight;
			}
		}
		if (pCur == nullptr) {
			return false;
		}

否则要删除的结点可能分为下面三种情况

要删除的结点只有左孩子

	else if (nullptr == pCur->_pRight) {
			if (nullptr==pParent) {
				_pRoot = pCur->_pLeft;
			}
			else {
				if (pCur == pParent->_pLeft) {
					pParent->_pLeft = pCur->_pLeft;
				}
				else {
					pParent->_pRight = pCur->_pLeft;
				}
			}
		}

要删除的结点只有右孩子

		if (nullptr == pCur->_pLeft) {
			if (nullptr==pParent) {
				_pRoot= pCur->_pRight;
			}
			else {
				if (pCur = pParent->_pLeft) {
					pParent->_pLeft = pCur->_pRight;
				}
				else {
					pParent->_pRight = pCur->_pRight;
				}
			}
		}

要删除的孩子左右孩子都有

	else {
			Node* pDelNode = pCur->_pRight;
			pParent = pCur;
			while (pDelNode->_pLeft) {
				pParent = pDelNode;
				pDelNode = pDelNode->_pLeft;
			}
			pCur->_data = pDelNode->_data;
			if (pDelNode == pParent->_pLeft) {
				pParent->_pLeft = pDelNode->_pRight;
			}
			else {
				pParent->_pRight = pDelNode->_pRight;
			}
			pCur = pDelNode;
		}

具体删除代码如下:

	bool Delete(const T& data) {
		if (_pRoot == nullptr) {
			return false;
		}
		Node* pCur = _pRoot;
		Node* pParent = nullptr;
		while (pCur) {
			if (pCur->_data == data) {
				break;
			}
			else if (data < pCur->_data) {
				pParent = pCur;
				pCur = pCur->_pLeft;
			}
			else {
				pParent = pCur;
				pCur = pCur->_pRight;
			}
		}
		if (pCur == nullptr) {
			return false;
		}
		if (nullptr == pCur->_pLeft) {
			if (nullptr==pParent) {
				_pRoot= pCur->_pRight;
			}
			else {
				if (pCur = pParent->_pLeft) {
					pParent->_pLeft = pCur->_pRight;
				}
				else {
					pParent->_pRight = pCur->_pRight;
				}
			}
		}
		else if (nullptr == pCur->_pRight) {
			if (nullptr==pParent) {
				_pRoot = pCur->_pLeft;
			}
			else {
				if (pCur == pParent->_pLeft) {
					pParent->_pLeft = pCur->_pLeft;
				}
				else {
					pParent->_pRight = pCur->_pLeft;
				}
			}
		}
		else {
			Node* pDelNode = pCur->_pRight;
			pParent = pCur;
			while (pDelNode->_pLeft) {
				pParent = pDelNode;
				pDelNode = pDelNode->_pLeft;
			}
			pCur->_data = pDelNode->_data;
			if (pDelNode == pParent->_pLeft) {
				pParent->_pLeft = pDelNode->_pRight;
			}
			else {
				pParent->_pRight = pDelNode->_pRight;
			}
			pCur = pDelNode;
		}
		delete pCur;
		return true;
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL中没有直接提供二叉搜索树的实现,但STL中有一些关于树的容器,比如set和map,它们底层的实现就是基于红黑树(一种平衡二叉搜索树)的。你可以使用这些容器来实现二叉搜索树的功能。关于二叉搜索树的一些知识,比如二叉树的遍历、迭代、线索二叉树、堆、Huffman编码、AVL树等都可以在STL中找到相应的实现。 二叉搜索树的查找可以通过比较根节点的值和目标值的大小来判断是往左子树还是往右子树查找,并重复这个过程直到找到目标值或者遍历到叶子节点为止。常规实现使用循环来实现查找,递归实现使用递归函数来查找。 二叉搜索树的插入操作也可以通过递归或循环来实现,根据目标值和当前节点的值的大小关系来决定是往左子树还是往右子树插入新节点。 STL中的二叉搜索树容器如set和map提供了插入、删除和查找等功能,并且保持了二叉搜索树的性质。你可以使用这些容器来处理二叉搜索树相关的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++ STL 数据结构 树](https://download.csdn.net/download/xinxipan/3008948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【C++ STL】-- 二叉搜索树](https://blog.csdn.net/weixin_64609308/article/details/128018280)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值