【C++搜索二叉树】

二叉搜索树

二叉搜索树的概念

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

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树
    在这里插入图片描述

二叉搜索树的操作

  1. 二叉搜索树的查找
    a、从根开始比较,比根大走右边,比根小走左边
    b、如果走到到空,还没找到,这个值不存在

  2. 二叉搜索树的插入
    插入的具体过程如下:
    a. 树为空,则直接新增节点,赋值给root指针
    b. 树不空,按二叉搜索树性质查找插入位置,插入新节点

  3. 二叉搜索树的删除
    首先查找是否在二叉搜索树中,如果不存在则返回,如果存在,则分三种情况:
    情况a:如果该节点只有左孩子节点,删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点–直接删除
    情况b:如果该节点只有右孩子节点,删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点–直接删除
    情况c:如果该节点右左,右孩子节点,在它的右子树中寻找中序下的第一个结点(,用它的值填补到被删除节点中,再来处理该结点的删除问题–替换法删除

搜索二叉树的具体实现

	template<class K>
	struct BSTNode
	{
		typedef BSTNode<K> Node;
		Node* _left;
		Node* _right;
		K _key;
		BSTNode(const K& key)
			:_left(nullptr)
			,_right(nullptr)
			,_key(key)
		{}
	};
	template<class K>
	class BSTree
	{
		typedef BSTNode<K> Node;
	public:
		BSTree() = default;

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

		BSTree<int> operator=(BSTree<K> t)
		{
			swap(_root, t._root);
			return *this;
		}

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

		bool Find(const K& key)
		{
			Node* curr = _root;
			while (curr)
			{
				if (curr->_key > key)
					curr = curr->_left;
				else if (curr->_key < key)
					curr = curr->_right;
				else
					return true;
			}
			return false;
		}
		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}
			Node* parent = nullptr;
			Node* curr = _root;
			while (curr)
			{
				if (curr->_key > key)
				{
					parent = curr;
					curr = curr->_left;
				}
				else if (curr->_key < key)
				{
					parent = curr;
					curr = curr->_right;
				}
				else
					return false;
			}
			Node* newnode = new Node(key);
			if (parent->_key < key)
				parent->_right = newnode;
			else
				parent->_left = newnode;
			return true;
		}
		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* curr = _root;
			while (curr)
			{
				if (curr->_key > key)
				{
					parent = curr;
					curr = curr->_left;
				}
				else if (curr->_key < key)
				{
					parent = curr;
					curr = curr->_right;
				}
				else	
				{
					//找到了
					//左子树为空
					if (curr->_left == nullptr)
					{
						//如果找到了这个节点且父亲为空,那么这个节点一定是root
						if (parent == nullptr)
						{
							delete _root;
							_root = nullptr;
						}
						else
						{
							if (parent->_left == curr)
								parent->_left = curr->_right;
							else
								parent->_right = curr->_right;
							delete curr;
						}
					}
					//右子树为空
					else if (curr->_right == nullptr)
					{
						//同上
						if (parent == nullptr)
						{
							delete _root;
							_root = nullptr;
						}
						else
						{
							if (parent->_left == curr)
								parent->_left = curr->_left;
							else
								parent->_right = curr->_left;
							delete curr;
						}
					}
					//两颗子树都不为空
					else
					{
						Node* minParent = curr;
						Node* minNode = curr->_right;
						while (minNode->_left)
						{
							minParent = minNode;
							minNode = minNode->_left;
						}

						swap(curr->_key, minNode->_key);

						if (minParent->_left == minNode)
							minParent->_left = minNode->_right;
						else
							minParent->_right = minNode->_right;
						delete minNode;
					}
					return true;
				}
			}
			return false;
		}
		void InOrder()
		{
			_InOrder(_root);
			cout << '\n';
		}
		bool 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);
		}
	private:
		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)
				{
					if (root == _root)
					{
						delete _root;
						_root = nullptr;
					}
					else
					{
						root = root->_right;
					}
				}
				else if (root->_right == nullptr)
				{
					if (root == _root)
					{
						delete _root;
						_root = nullptr;
					}
					else
					{
						root = root->_left;
					}
				}
				else
				{
					Node* minParent = root;
					Node* minNode = root->_right;
					while (minNode->_left)
					{
						minParent = minNode;
						minNode = minNode->_left;
					}
					swap(root->_key, minNode->_key);
					return _EraseR(root->_right, key);
				}
				return true;
			}
		}
		//因为传的是引用,所以root是上一栈帧中root的左/右节点指针的别名
		//修改root,就是修改上一栈帧中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 _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
				return false;

			if (root->_key > key)
				return _FindR(root->_left, key);
			else if (root->_key < key)
				return _FindR(root->_right, key);
			else
				return true;
		}
		void _Destroy(Node* root)
		{
			if (root == nullptr)
				return;
			_Destroy(root->_left);
			_Destroy(root->_right);
			delete root;
		}
		Node* _Copy(Node* root)
		{
			if (root == nullptr) return nullptr;
			Node* newnode = new Node(root->_key);
			newnode->_left = _Copy(root->_left);
			newnode->_right = _Copy(root->_right);
			return newnode;
		}
		void _InOrder(Node* root)
		{
			if (root == nullptr)
				return;
			_InOrder(root->_left);
			cout << root->_key << ' ';
			_InOrder(root->_right);
		}
		Node* _root = nullptr;
	};
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
打印搜索二叉树的值可以使用中序遍历(In-order traversal)算法,该算法会以从小到大的顺序遍历每个节点并打印节点的值。具体实现可以参考以下 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> // 定义搜索二叉树节点结构体 struct node { int value; struct node *left; struct node *right; }; // 中序遍历搜索二叉树并打印节点的值 void in_order_traversal(struct node *root) { if (root == NULL) { return; } in_order_traversal(root->left); printf("%d ", root->value); in_order_traversal(root->right); } int main() { // 构建一个搜索二叉树 struct node *root = (struct node*) malloc(sizeof(struct node)); root->value = 4; root->left = (struct node*) malloc(sizeof(struct node)); root->left->value = 2; root->left->left = (struct node*) malloc(sizeof(struct node)); root->left->left->value = 1; root->left->left->left = NULL; root->left->left->right = NULL; root->left->right = (struct node*) malloc(sizeof(struct node)); root->left->right->value = 3; root->left->right->left = NULL; root->left->right->right = NULL; root->right = (struct node*) malloc(sizeof(struct node)); root->right->value = 6; root->right->left = (struct node*) malloc(sizeof(struct node)); root->right->left->value = 5; root->right->left->left = NULL; root->right->left->right = NULL; root->right->right = (struct node*) malloc(sizeof(struct node)); root->right->right->value = 7; root->right->right->left = NULL; root->right->right->right = NULL; // 打印搜索二叉树的值 in_order_traversal(root); return 0; } ``` 上述代码中,我们先定义了一个搜索二叉树节点的结构体,包括节点的值、左子节点和右子节点。接着,我们实现了一个中序遍历算法 `in_order_traversal()`,该算法会递归遍历搜索二叉树的左子树、打印当前节点的值、再遍历右子树。最后,在 `main()` 函数中构建一个搜索二叉树,并调用 `in_order_traversal()` 打印搜索二叉树的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LRBORRR

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

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

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

打赏作者

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

抵扣说明:

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

余额充值