c++二叉搜索树配套代码

148 篇文章 1 订阅
129 篇文章 1 订阅
本文介绍了使用C++编写的二叉搜索树模板类,包含插入、删除、查找等基本操作,并通过示例展示了如何创建树并执行操作。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<cstring>
#include <vector>
#include<assert.h>
using namespace std;
template<class T>
struct BSTnode
{
	BSTnode<T>* left;
	BSTnode<T>* right;
	T _val;

	BSTnode(const T& val = T()):_val(val),left(nullptr),right(nullptr)
	{}


	
};
template <class T>
class bstree
{

	typedef BSTnode<T> Node;
public:
	bool Insert(const T&key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_val < key)
			{
				parent = cur;
				cur = cur->right;
			}
			else if (cur->_val > key)
			{
				parent = cur;
				cur = cur->left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(key);
		if (parent->_val < key)
		{
			parent->right = cur;
		}
		else
		{
			parent->left = cur;
		}
	}
	void inorder(Node*root=_root)
	{
		if (root == nullptr)
		{
			return;
		}
		inorder(root->left);
		cout << root->_val << ' ';
		inorder(root->right);
	
	}
	void text()
	{
		inorder(_root);
	}
	bool Find(const T& val)
	{
		Node* node = _root;
		while (node)
		{
			if (node->_val == val)
				return true;
			else if (node->_val > val)
				node = node->_left;
			else
				node = node->_right;
		}
		return false;
	}
	bool erase(const T& key)
	{
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_val < key)
			{
				parent = cur;
				cur = cur->right;
			}
			else if (cur->_val > key)
			{
				parent = cur;
				cur = cur->left;
			}
			else
			{
				if (cur->left== nullptr)
				{
					if(cur==_root)
					{
						_root = cur->right;
					}
					else
					{
						if (parent->left == cur)
						{
							parent->left = cur->right;
						}
						else
						{
							parent->right = cur->right;
						}
					}
				}
				else if (cur->right == nullptr)
				{
					if (_root == cur)
					{
						_root = cur->left;
					}
					else
					{
						if (parent->left == cur)
						{
							parent->left = cur->left;
						}
						else
						{
							parent->right = cur->left;
						}
					}
				}
				else
				{
					Node* pminright = cur;
					Node* minright = cur->right;
					while (minright->left)
					{
						pminright = minright;
						minright = minright->left;
					}
					cur->_val = minright->_val;
					if (pminright->left == minright)
					{
						if (minright->right)
						{
							pminright->left = minright->right;
						}
						else
						{
							pminright->left = nullptr;
						}
					}

					else
					{
						if (minright->left)
						{
							pminright->right = minright->right;
						}
						else
						{
							pminright->right = nullptr;
						}
					}
				}
				return true;
			}
			
		}
		return false;
	}
	void findp(Node* root, const T& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_val ==key)
		{
			return true;
		}
		if (root->_val < key)
		{
			return findp(root->right, key);
		}
		else
		{
			return findp(root->left, key);
		}
	}
	bool findd(const T& key)
	{
		return findp(_root, key);
	}
	bool insertd(Node* root, const T& key)
	{
		if (root == nullptr)
		{
			root = new Node(key);
			return true;
		}
		if (root->_val < key)
		{
			return insertd(root->right, key);
		}
		else if (root->_val > key)
		{
			return insertd(root->left, key);
		}
		else
		{
			return false;
		}
	}
	bool insertdz(const T& key)
	{
		insertd(_root, key);
	}
	bool erasedp(Node* root, const T& key)
	{
		if (root == nullptr)
		{
			return false;
		}
		if (root->_val < key)
		{
			return erasedp(root->right, key);
		}
		else if (root->_val > key)
		{
			return erasedp(root->left, key);
		}
		else
		{
			if (root->left == nullptr)
			{
				Node* point = root->right;
				delete root;
				root = point;
			}
			else if (root->right == nullptr)
			{
				Node* point = root->left;
				delete root;
				root = point;
			}
			else
			{
				Node* point = root->left;
				while (point->right)
				{
					point = point->right;
				}
				swap(root->_val, point->val);
		
				return erasedp(root->left, key);
			}
			return true;
		}
	}
	bool erased(const T& key)
	{
		return erasedp(_root, key);
	}
private:
	Node* _root=nullptr;

};

int main()
{
	bstree<int>t1;
	int arr[] = { 8,3,1,10,6,4,7,14,13 };
	for (auto d : arr)
	{
		t1.Insert(d);
	}
	t1.text();
	t1.erase(8);
	t1.text();
	return 0;
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值