何为二叉搜索树
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树: 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值 它的左右子树也分别为二叉搜索树
//二叉搜索树的节点
template<class T>
struct BSTNode
{
BSTNode<T>* _left;
BSTNode<T>* _right;
T _key;
BSTNode(const T& key)
:_left(nullptr)
,_right(nullptr)
,_key(key)
{}
};
二叉搜索树的查找
a、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
b、最多查找高度次,走到到空,还没找到,这个值不存在。
二叉搜索树的插入 插入的具体过程如下:
a. 树为空,则直接新增节点,赋值给root指针
b. 树不空,按二叉搜索树性质查找插入位置,插入新节点
💖二叉搜索树的删除首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情 况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点
c. 要删除的结点只有右孩子结点
d. 要删除的结点有左、右孩子结点
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程 如下:
情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除
情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除
情况d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点 中,再来处理该结点的删除问题--替换法删除
完整代码
#pragma once
#include<iostream>
using namespace std;
//二叉搜索树的节点
template<class T>
struct BSTNode
{
BSTNode<T>* _left;
BSTNode<T>* _right;
T _key;
BSTNode(const T& key)
:_left(nullptr)
,_right(nullptr)
,_key(key)
{}
};
//二叉搜索树的类
template<class T>
class BSTree
{
typedef BSTNode<T> Node;
private:
void DestroyTree(Node* root)
{
if (root == nullptr)
return;
DestroyTree(root->_left);
DestroyTree(root->_right);
delete root;
}
Node* CopyTree(Node* root)
{
if (root == nullptr)
return nullptr;
Node* NewNode = new Node(root->_key);
NewNode->_left=CopyTree(root->_left);
NewNode->_right=CopyTree(root->_right);
return NewNode;
}
public:
BSTree()=default;//强制编译器自己生成构造
BSTree(const BSTree<T>& t)
{
_root = CopyTree(t._root);
}
BSTree<T>& operator=(const BSTree<T> t)
{
swap(_root, t._root);
return *this;
}
~BSTree()
{
DestroyTree(_root);
_root = nullptr;
}
//中序遍历
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//插入
bool Insert(const T& key)
{
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* parent = nullptr;
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
{
return false;
}
}
cur = new Node(key);
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
//查找
bool Find(const T& key)
{
Node* cur = _root;
while (cur)
{
if (key > cur->_key)
{
cur = cur->_right;
}
else if (key < cur->_key)
{
cur = cur->_left;
}
else
{
return true;
}
}
return false;
}
//删除
bool Erase(const T& key)
{
Node* parent = nullptr;
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//找到了
{
//cur左子树为空
if (cur->_left == nullptr)
{
if (cur == _root)
{
_root = cur->_right;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
}
//cur右子树为空
else if (cur->_right == nullptr)
{
if (cur == _root)
{
_root = cur->_left;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
}
//cur左右子树都不为空
//找cur右子树中最小的节点
else
{
Node* MinRight = cur->_right;
Node* MRParent = cur;
while (MinRight->_left)
{
MRParent = MinRight;
MinRight = MinRight->_left;
}
swap(cur->_key, MinRight->_key);
if (MRParent->_left == MinRight)
{
MRParent->_left = MinRight->_right;
}
else
{
MRParent->_right = MinRight->_right;
}
delete MinRight;
}
return true;
}
}
return false;
}
//递归写法
bool FindR(const T& key)
{
return _FindR(_root, key);
}
bool InsertR(const T& key)
{
return _InsertR(_root, key);
}
bool EraseR(const T& key)
{
return _EraseR(_root, key);
}
private:
bool _FindR(const Node* root, const T& key)
{
if (root == nullptr)
{
return false;
}
if (key > root->_key)
{
return _FindR(root->_right, key);
}
else if (key < root->_key)
{
return _FindR(root->_left, key);
}
else
{
return true;
}
}
bool _InsertR(Node*& root, const T& key)
{
if (root == nullptr)
{
root = new Node(key);
return true;
}
if (key > root->_key)
{
return _InsertR(root->_right, key);
}
else if (key < root->_key)
{
return _InsertR(root->_left, key);
}
else
{
return false;
}
}
bool _EraseR(Node*& root, const T& key)
{
if (root == nullptr)
{
return false;
}
if (key > root->_key)
{
return _EraseR(root->_right, key);
}
else if (key < root->_key)
{
return _EraseR(root->_left, key);
}
else
{
Node* del = root;
if (root->_left == nullptr)
{
root = root->_right;
}
else if (root->_right ==nullptr)
{
root = root->_left;
}
else
{
Node* MinRight = root->_right;
while (MinRight->_left)
{
MinRight = MinRight->_left;
}
swap(root->_key, MinRight->_key);
return _EraseR(root->_right, key);
}
delete del;
return true;
}
}
void _InOrder(const Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
private:
Node* _root=nullptr;
};