给一个vector向量,包含{8,4,6,9,10,3,5}元素
其二叉树的图为
假设一道光照下
元素会按照从小到大的顺序排序,二叉树用中序遍历排序也是这个顺序
二叉搜索树的插入规则时:
当插入值比节点值小时,插入左子树,反之插入右子树
二叉树的定义
每个左子树不会大于节点,每个右子树不会小于节点
C++实现代码:
每个功能都有一个接口放在public域,对象通关接口调用在protected域重载过的函数
为了BST类能访问BSTNode的成员,将BST设为BSTNode的友元类,在此之前要声明BST类,才能将设为友元函数,不然BSTNode找不到类BST类
二叉搜索树插入的实现分析:
采用的递归方法
若插入值等于节点值直接返回
若树为空,则直接创建节点
当t不为空指针时,循环操作
如果插入值小于节点值,则将用左子树作为参数放入Insert函数中
反之则将用右子树作为参数放入Insert函数中
当t为空时,创建节点并返回
bool Insert(BSTNode<Type>*& t, const Type& v)
{
if (t == nullptr)
{
t = new BSTNode<Type>(v);
return true;
}
if (v == t->data)
return false;
if (v < t->data)
Insert(t->leftChild, v);
else if (v > t->data, v)
Insert(t->rightChild, v);
return true;
}
二叉搜索树的删除
删除可以分成四种情况或者两种情况
四种情况:1、叶子节点 2、只有左子树 3、只有右子树 4、左右子树都有
1、直接删除,delete后指针赋空
2、结点等于左子树
3、结点等于右子树
4、替换,将左子树最大的元素赋给结点,并删除节点(若用右子树则是最小的元素)
if (t->leftChild == nullptr && t->rightChild == nullptr)
{
//叶子节点
delete t;
t = nullptr;
}
else if (t->leftChild != nullptr && t->rightChild == nullptr)
{
//有左子树,无右子树
BSTNode<Type>* p = t;
t = t->leftChild;
delete p;
}
else if (t->leftChild == nullptr && t->rightChild != nullptr)
{
//无左子数,有右子树
BSTNode<Type>* p = t;
t = t->rightChild;
delete p;
}
else if (t->leftChild != nullptr && t->rightChild != nullptr)
{
BSTNode<Type>* p = t->leftChild;
while (p->rightChild != nullptr)
{
p = p->rightChild;
}
t->data = p->data;
Remove(p,p->data);
}
两种情况:1、至多有一个子树 2、有两个子树
1、左子树不为空,则t等于左子树,不然等于右子树
2、替换,将左子树最大的元素赋给结点,并删除节点(若用右子树则是最小的元素)
if (t->leftChild != nullptr && t->rightChild != nullptr)
{
p = t->leftChild;
while (p->rightChild != nullptr)
{
p = p->rightChild;
}
t->data = p->data;
Remove(p, p->data);
}
else
{
p = t;
if (t->leftChild != nullptr)
t = t->leftChild;
else
t = t->rightChild;
delete p;
}
#include <iostream>
#include <vector>
using namespace std;
template<class Type>
class BSTNode;
template<class Type>
class BST;
template<class Type>
class BSTNode
{
friend class BST<Type>;
public:
BSTNode(Type d = Type(), BSTNode<Type>* left = nullptr, BSTNode<Type>* right = nullptr) :data(d), leftChild(left), rightChild(right)
{
}
~BSTNode()
{}
private:
Type data;
BSTNode* leftChild;
BSTNode* rightChild;
};
template<class Type>
class BST
{
public:
BST() :root(nullptr)
{}
public:
bool Insert(const Type& v)
{
return Insert(root, v);
}
void Inorder()
{
return Inorder(root);
}
BSTNode<Type>* Find(const Type& key)
{
return Find(root, key);
}
Type Max()const
{
return Max(root);
}
Type Min()const
{
return Min(root);
}
bool Remove(const Type& key)
{
return Remove(root,key);
}
protected:
bool Insert(BSTNode<Type>*& t, const Type& v)
{
if (t == nullptr)
{
t = new BSTNode<Type>(v);
return true;
}
if (v == t->data)
return false;
if (v < t->data)
Insert(t->leftChild, v);
else if (v > t->data, v)
Insert(t->rightChild, v);
return true;
}
//中序遍历
void Inorder(BSTNode<Type>* t)
{
if (t != nullptr)
{
Inorder(t->leftChild);
cout << t->data << endl;;
Inorder(t->rightChild);
}
}
BSTNode<Type>* Find(BSTNode<Type>* t, const Type& key)
{
if (t == nullptr || key == t->data)
return t;
if (key < t->data)
{
Find(t->leftChild, key);
}
if (key > t->data)
{
Find(t->rightChild, key);
}
}
Type Max(BSTNode<Type>* t)const
{
BSTNode<Type>* p = t;
while (p->rightChild != nullptr)
{
p = p->rightChild;
}
return p->data;
}
Type Min(BSTNode<Type>* t)const
{
BSTNode<Type>* p = t;
while (p->leftChild != nullptr)
{
p = p->leftChild;
}
return p->data;
}
bool Remove(BSTNode<Type>*& t, const Type& key)
{
//查找结点
if (t == nullptr)
{
return false;
}
if (key < t->data)
{
Remove(t->leftChild, key);
}
else if (key > t->data)
{
Remove(t->rightChild, key);
}
else
{
BSTNode<Type>* p;
if (t->leftChild != nullptr && t->rightChild != nullptr)
{
p = t->leftChild;
while (p->rightChild != nullptr)
{
p = p->rightChild;
}
t->data = p->data;
Remove(p, p->data);
}
else
{
p = t;
if (t->leftChild != nullptr)
t = t->leftChild;
else
t = t->rightChild;
delete p;
}
/*if (t->leftChild == nullptr && t->rightChild == nullptr)
{
//叶子节点
delete t;
t = nullptr;
}
else if (t->leftChild != nullptr && t->rightChild == nullptr)
{
//有左子树,无右子树
BSTNode<Type>* p = t;
t = t->leftChild;
delete p;
}
else if (t->leftChild == nullptr && t->rightChild != nullptr)
{
//无左子数,有右子树
BSTNode<Type>* p = t;
t = t->rightChild;
delete p;
}
else if (t->leftChild != nullptr && t->rightChild != nullptr)
{
BSTNode<Type>* p = t->leftChild;
while (p->rightChild != nullptr)
{
p = p->rightChild;
}
t->data = p->data;
Remove(p,p->data);
}*/
}
return true;
}
private:
BSTNode<Type>* root;
};
int main()
{
vector<int> iv = { 2,3,0,6,4,7,5,9,1,40 };
BST<int> bst;
for (auto& e : iv)
{
bst.Insert(e);
}
bst.Inorder();
BSTNode<int>* pd = bst.Find(10);
if (pd == nullptr)
cout << "nullptr" << endl;
cout << "Min = " << bst.Min() << endl;
cout << "Max = " << bst.Max() << endl;
cout << bst.Remove(2) << endl;
//cout << bst.Remove(9) << endl;
return 0;
}