二叉搜索树概念 :
二叉搜索树又称二叉排序树,它或者是一棵空树
,或者是具有以下性质的二叉树
:
若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
它的左右子树也分别为二叉搜索树
int a [] = {5,3,4,1,7,8,2,6,0,9};
二叉搜索树操作 :
1.
二叉搜索树的查找
![](https://img-blog.csdnimg.cn/9e0b1716a5924ad5ad0e1d32df53593d.png)
2.
二叉搜索树的插入
插入的具体过程如下:
a.
树为空,则直接插入
b.
树不空,按二叉搜索树性质查找插入位置,插入新节点
3.
二叉搜索树的删除
首先查找元素是否在二叉搜索树中,如果不存在,则返回
,
否则要删除的结点可能分下面四种情况:
a.
要删除的结点无孩子结点
b.
要删除的结点只有左孩子结点
c.
要删除的结点只有右孩子结点
d.
要删除的结点有左、右孩子结点
看起来有待删除节点有
4
中情况,实际情况
a
可以与情况
b
或者
c
合并起来,因此真正的删除过程如下:
情况
b
:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点
情况
c
:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点
情况
d
:在它的右子树中寻找中序下的第一个结点
(
关键码最小
)
,用它的值填补到被删除节点中,
再来处理该结点的删除问题
二叉搜索树的性能分析 :
插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
对有
n
个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。
但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:
最优情况下,二叉搜索树为完全二叉树,其平均比较次数为:log2N
最差情况下,二叉搜索树退化为单支树,其平均比较次数为:N/2
问题:如果退化成单支树,二叉搜索树的性能就失去了。那能否进行改进,不论按照什么次序插入关键码
都可以是二叉搜索树的性能最佳---高度平衡的二叉搜索树(AVL树)。
二叉搜索树递归实现和各种操作:
#include<iostream>
#include<vector>
using namespace std;
template<class _Ty>
class BSTree;
//BST二叉排序树
template<class _Ty>
class BSTNode
{
friend class BSTree<_Ty>;
public:
BSTNode() :data(_Ty()), leftChild(nullptr), rightChild(nullptr)
{}
BSTNode(_Ty val, BSTNode* left = nullptr, BSTNode* right = nullptr):data(val),leftChild(left),rightChild(right)
{}
~BSTNode() {}
private:
_Ty data;
BSTNode* leftChild;
BSTNode* rightChild;
};
template<class _Ty>
class BSTree
{
public:
BSTree() :root(nullptr) {}
BSTree(vector<_Ty>& nums):root(nullptr)
{
for (const auto& e : nums)
{
Insert(e);
}
}
bool Insert(const _Ty x)
{
return Insert(root, x);
}
BSTNode<_Ty>* Min() const
{
return Min(root);
}
BSTNode<_Ty>* Max() const
{
return Max(root);
}
BSTNode<_Ty>* Search(const _Ty& key)
{
return Search(root, key);
}
void Order() const
{
Order(root);
}
bool Remove(const _Ty& key)
{
return Remove(root, key);
}
protected:
bool Insert(BSTNode<_Ty>*& t, const _Ty x)
{
if (t == nullptr)
{
t = new BSTNode<_Ty>(x);
return true;
}
if (x < t->data)
return Insert(t->leftChild, x);
else if (x > t->data)
return Insert(t->rightChild, x);
return false;
}
BSTNode<_Ty>* Min(BSTNode<_Ty>* t)const
{
while (t && t->leftChild!=nullptr)
{
t = t->leftChild;
}
return t;
}
BSTNode<_Ty>* Max(BSTNode<_Ty>* t)const
{
while (t && t->rightChild != nullptr)
{
t = t->rightChild;
}
return t;
}
BSTNode<_Ty>* Search(BSTNode<_Ty>* t, const _Ty& key)
{
if (t == nullptr)
return t;
if (key < t->data)
Search(t->leftChild, key);
else if (key > t->data)
Search(t->rightChild, key);
else
return t;
}
void Order(BSTNode<_Ty>* t) const
{
if (t != nullptr)
{
Order(t->leftChild);
cout << t->data << " ";
Order(t->rightChild);
}
}
bool Remove(BSTNode<_Ty>*& t, const _Ty& key)
{
if (t == nullptr)
return false;
if (key < t->data)
return Remove(t->leftChild, key);
else if (key > t->data)
return Remove(t->rightChild, key);
else
{
//找到了,删除
BSTNode<_Ty>* p = nullptr;
if (t->leftChild != nullptr && t->rightChild != nullptr)
{
p = t->leftChild;
while (p->rightChild != nullptr)
p = p->rightChild;
t->data = p->data;
Remove(t->leftChild, p->data);
}
else
{
p = t;
if (t->leftChild != nullptr)
t = t->leftChild;
else
t = t->rightChild;
delete p;
}
return true;
//if (t->leftChild == nullptr && t->rightChild == nullptr)
//{
// delete t;
// t = nullptr;
//}
//else if (t->leftChild != nullptr && t->rightChild == nullptr)
//{
// p = t;
// t = t->leftChild;
// delete p;
//}
//else if (t->leftChild == nullptr && t->rightChild != nullptr)
//{
// p = t;
// t = t->rightChild;
// delete p;
//}
//else
//{
// p = t->leftChild;
// while (p->rightChild != nullptr)
// p = p->rightChild;
// t->data = p->data;
// Remove(t->leftChild, p->data);
//}
}
}
private:
BSTNode<_Ty>* root;
};
void test01()
{
vector<int> iv{ 5,3,4,1,7,8,2,6,0,9 };
BSTree<int>bst;
for (int i = 0; i < iv.size(); ++i)
{
bst.Insert(iv[i]);
}
}
void test02()
{
vector<int> iv{ 5,3,4,1,7,8,2,6,0,9 };
BSTree<int>bst(iv);
BSTNode<int>* p = bst.Min();
BSTNode<int>* q = bst.Max();
BSTNode<int>* ret = bst.Search(60);
bst.Order();
//cout << "创建完毕" << endl;
bst.Remove(6);
cout << "创建完毕" << endl;
}
void test03()
{
vector<int> iv{ 50,30,40,10,70,80,2,60,90 };
BSTree<int> bst(iv);
bst.Remove(10);
bst.Remove(50);
cout << "删除完毕" << endl;
}
void main()
{
test03();
system("pause");
}
Remove非递归实现
#include<iostream>
#include<vector>
using namespace std;
template<class _Ty>
class BSTree;
//BST二叉排序树
template<class _Ty>
class BSTNode
{
friend class BSTree<_Ty>;
public:
BSTNode() :data(_Ty()), leftChild(nullptr), rightChild(nullptr)
{}
BSTNode(_Ty val, BSTNode* left = nullptr, BSTNode* right = nullptr) :data(val), leftChild(left), rightChild(right)
{}
~BSTNode() {}
private:
_Ty data;
BSTNode* leftChild;
BSTNode* rightChild;
};
template<class _Ty>
class BSTree
{
public:
BSTree() :root(nullptr) {}
BSTree(vector<_Ty>& nums) :root(nullptr)
{
for (const auto& e : nums)
{
Insert(e);
}
}
bool Insert(const _Ty x)
{
return Insert(root, x);
}
BSTNode<_Ty>* Min() const
{
return Min(root);
}
BSTNode<_Ty>* Max() const
{
return Max(root);
}
BSTNode<_Ty>* Search(const _Ty& key)
{
return Search(root, key);
}
void Order() const
{
Order(root);
}
bool Remove(const _Ty& key)
{
return Remove(root, key);
}
BSTNode<_Ty>* Parent(BSTNode<_Ty>* key)const
{
return Parent(root, key);
}
protected:
bool Insert(BSTNode<_Ty>*& t, const _Ty x)
{
if (t == nullptr)
{
t = new BSTNode<_Ty>(x);
return true;
}
if (x < t->data)
return Insert(t->leftChild, x);
else if (x > t->data)
return Insert(t->rightChild, x);
return false;
}
BSTNode<_Ty>* Min(BSTNode<_Ty>* t)const
{
while (t && t->leftChild != nullptr)
{
t = t->leftChild;
}
return t;
}
BSTNode<_Ty>* Max(BSTNode<_Ty>* t)const
{
while (t && t->rightChild != nullptr)
{
t = t->rightChild;
}
return t;
}
BSTNode<_Ty>* Search(BSTNode<_Ty>* t, const _Ty& key)
{
if (t == nullptr)
return t;
if (key < t->data)
Search(t->leftChild, key);
else if (key > t->data)
Search(t->rightChild, key);
else
return t;
}
void Order(BSTNode<_Ty>* t) const
{
if (t != nullptr)
{
Order(t->leftChild);
cout << t->data << " ";
Order(t->rightChild);
}
}
bool Remove(BSTNode<_Ty>*& t, const _Ty& key)
{
if (t == nullptr)
return false;
BSTNode<_Ty>* p = Search(t, key);
if (p == nullptr)
return false;
if (p->leftChild != nullptr && p->rightChild != nullptr)
{
BSTNode<_Ty>* q = p->leftChild;
while (q->rightChild != nullptr)
q = q->rightChild;
p->data = q->data;
p = q;
}
BSTNode<_Ty>* pr = Parent(t, p);
if (pr == nullptr)//要删除根节点
{
if (p->leftChild != nullptr)
t = p->leftChild;
else
t = p->rightChild;
}
else
{
if (pr->leftChild == p)
{
if (p->leftChild != nullptr)
pr->leftChild = p->leftChild;
else
pr->leftChild = p->rightChild;
}
else
{
if (p->leftChild != nullptr)
pr->rightChild = p->leftChild;
else
pr->rightChild = p->rightChild;
}
}
delete p;
}
BSTNode<_Ty>* Parent(BSTNode<_Ty>* t, const _Ty& key)const
{
if (t == nullptr || t->data == key)
return nullptr;
if (t->leftChild && key == t->leftChild->data || t->rightChild && key == t->rightChild->data)
return t;
if (key < t->data)
return Parent(t->leftChild, key);
else if (key > t->data)
return Parent(t->rightChild, key);
}
BSTNode<_Ty>* Parent(BSTNode<_Ty>* t,BSTNode<_Ty>* key)const
{
if (t == nullptr || t == key)
return nullptr;
if (t->leftChild && key == t->leftChild || t->rightChild && key == t->rightChild)
return t;
if (key->data < t->data)
return Parent(t->leftChild, key);
else if (key->data > t->data)
return Parent(t->rightChild, key);
}
private:
BSTNode<_Ty>* root;
};
void test01()
{
vector<int> iv{ 5,3,4,1,7,8,2,6,0,9 };
BSTree<int>bst;
for (int i = 0; i < iv.size(); ++i)
{
bst.Insert(iv[i]);
}
}
void test02()
{
vector<int> iv{ 5,3,4,1,7,8,2,6,0,9 };
BSTree<int>bst(iv);
BSTNode<int>* p = bst.Min();
BSTNode<int>* q = bst.Max();
BSTNode<int>* ret = bst.Search(60);
bst.Order();
//cout << "创建完毕" << endl;
bst.Remove(6);
cout << "创建完毕" << endl;
}
void test03()
{
//vector<int> iv{ 50,30,40,10,70,80,2,60,90 };
vector<int> iv{ 50,30,40};
BSTree<int> bst(iv);
bst.Remove(50);
//bst.Remove(50);
/*BSTNode<int>* p1= bst.Parent(50);
BSTNode<int>* p2 = bst.Parent(90);
BSTNode<int>* p3 = bst.Parent(10);*/
cout << "删除完毕" << endl;
}
void test04()
{
vector<int> iv{ 50,30,40,10,70,80,2,60,90 };
BSTree<int> bst(iv);
//bst.Remove(50);
BSTNode<int>* rt = bst.Search(10);
BSTNode<int>* p1= bst.Parent(rt);
cout << "删除完毕" << endl;
}
void main()
{
test03();
system("pause");
}
Remove写法3
#include<iostream>
#include<vector>
using namespace std;
template<class _Ty>
class BSTree;
//BST二叉排序树
template<class _Ty>
class BSTNode
{
friend class BSTree<_Ty>;
public:
BSTNode() :data(_Ty()), leftChild(nullptr), rightChild(nullptr)
{}
BSTNode(_Ty val, BSTNode* left = nullptr, BSTNode* right = nullptr) :data(val), leftChild(left), rightChild(right)
{}
~BSTNode() {}
private:
_Ty data;
BSTNode* leftChild;
BSTNode* rightChild;
};
template<class _Ty>
class BSTree
{
public:
BSTree() :root(nullptr) {}
BSTree(vector<_Ty>& nums) :root(nullptr)
{
for (const auto& e : nums)
{
Insert(e);
}
}
bool Insert(const _Ty x)
{
return Insert(root, x);
}
BSTNode<_Ty>* Min() const
{
return Min(root);
}
BSTNode<_Ty>* Max() const
{
return Max(root);
}
BSTNode<_Ty>* Search(const _Ty& key)
{
return Search(root, key);
}
void Order() const
{
Order(root);
}
bool Remove(const _Ty& key)
{
return Remove(root, key);
}
BSTNode<_Ty>* Parent(BSTNode<_Ty>* key)const
{
return Parent(root, key);
}
protected:
bool Insert(BSTNode<_Ty>*& t, const _Ty x)
{
if (t == nullptr)
{
t = new BSTNode<_Ty>(x);
return true;
}
if (x < t->data)
return Insert(t->leftChild, x);
else if (x > t->data)
return Insert(t->rightChild, x);
return false;
}
BSTNode<_Ty>* Min(BSTNode<_Ty>* t)const
{
while (t && t->leftChild != nullptr)
{
t = t->leftChild;
}
return t;
}
BSTNode<_Ty>* Max(BSTNode<_Ty>* t)const
{
while (t && t->rightChild != nullptr)
{
t = t->rightChild;
}
return t;
}
BSTNode<_Ty>* Search(BSTNode<_Ty>* t, const _Ty& key)
{
if (t == nullptr)
return t;
if (key < t->data)
Search(t->leftChild, key);
else if (key > t->data)
Search(t->rightChild, key);
else
return t;
}
void Order(BSTNode<_Ty>* t) const
{
if (t != nullptr)
{
Order(t->leftChild);
cout << t->data << " ";
Order(t->rightChild);
}
}
bool Remove(BSTNode<_Ty>*& t, const _Ty& key)
{
if (t == nullptr)
return false;
BSTNode<_Ty>* p = t, *pr = nullptr;
while (p != nullptr)
{
if (key == p->data)
break;
pr = p;
if (key < p->data)
p = p->leftChild;
else
p = p->rightChild;
}
//
if (p->leftChild != nullptr && p->rightChild != nullptr)
{
BSTNode<_Ty>* q = p->leftChild;
while (q->rightChild != nullptr)
{
pr = q;
q = q->rightChild;
}
p->data = q->data;
p = q;
}
if (pr == nullptr)
{
if (p->leftChild != nullptr)
t = p->leftChild;
else
t = p->rightChild;
}
else
{
if (pr->leftChild == p)
{
if (p->leftChild != nullptr)
pr->leftChild = p->leftChild;
else
pr->leftChild = p->rightChild;
}
else
{
if (p->leftChild != nullptr)
pr->rightChild = p->leftChild;
else
pr->rightChild = p->rightChild;
}
}
}
BSTNode<_Ty>* Parent(BSTNode<_Ty>* t, const _Ty& key)const
{
if (t == nullptr || t->data == key)
return nullptr;
if (t->leftChild && key == t->leftChild->data || t->rightChild && key == t->rightChild->data)
return t;
if (key < t->data)
return Parent(t->leftChild, key);
else if (key > t->data)
return Parent(t->rightChild, key);
}
BSTNode<_Ty>* Parent(BSTNode<_Ty>* t, BSTNode<_Ty>* key)const
{
if (t == nullptr || t == key)
return nullptr;
if (t->leftChild && key == t->leftChild || t->rightChild && key == t->rightChild)
return t;
if (key->data < t->data)
return Parent(t->leftChild, key);
else if (key->data > t->data)
return Parent(t->rightChild, key);
}
private:
BSTNode<_Ty>* root;
};
void test01()
{
vector<int> iv{ 5,3,4,1,7,8,2,6,0,9 };
BSTree<int>bst;
for (int i = 0; i < iv.size(); ++i)
{
bst.Insert(iv[i]);
}
}
void test02()
{
vector<int> iv{ 5,3,4,1,7,8,2,6,0,9 };
BSTree<int>bst(iv);
BSTNode<int>* p = bst.Min();
BSTNode<int>* q = bst.Max();
BSTNode<int>* ret = bst.Search(60);
bst.Order();
//cout << "创建完毕" << endl;
bst.Remove(6);
cout << "创建完毕" << endl;
}
void test03()
{
//vector<int> iv{ 50,30,40,10,70,80,2,60,90 };
vector<int> iv{ 50,30,40 };
BSTree<int> bst(iv);
bst.Remove(50);
//bst.Remove(50);
/*BSTNode<int>* p1= bst.Parent(50);
BSTNode<int>* p2 = bst.Parent(90);
BSTNode<int>* p3 = bst.Parent(10);*/
cout << "删除完毕" << endl;
}
void test04()
{
vector<int> iv{ 50,30,40,10,70,80,2,60,90 };
BSTree<int> bst(iv);
//bst.Remove(50);
BSTNode<int>* rt = bst.Search(10);
BSTNode<int>* p1 = bst.Parent(rt);
cout << "删除完毕" << endl;
}
void main()
{
test03();
system("pause");
}