#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;
}
c++二叉搜索树配套代码
最新推荐文章于 2024-10-08 06:04:53 发布
本文介绍了使用C++编写的二叉搜索树模板类,包含插入、删除、查找等基本操作,并通过示例展示了如何创建树并执行操作。
摘要由CSDN通过智能技术生成