BinTree.h
#pragma once
#include<iostream>
#include<vector>
using namespace std;
/*
二叉排序树,对二叉排序树进行中序遍历,可以得到一个递增的有序序列
*/
template<class T> class BinNode
{
public:
T key;
BinNode* lchild, * rchild;
};
template<class T> class BinTree
{
public:
BinTree();//默认构造
BinTree(T* arr, int len);//数组构造
BinTree(vector<T> vec);//容器构造
bool Insert(T& key);//插入节点
void InOrder();//中序遍历外部接口
bool Search(T key);//查找结点
bool Delete(T& key);//删除结点
bool isEmpty();//判断树是否为空树
private:
BinNode<T>* TNode;
void _InOrder(BinNode<T>* T);//中序遍历内部实现
void _visit(BinNode<T>* T);//访问结点内部实现
BinNode<T>* _Search(BinNode<T>* t, T key);//查找结点内部实现
bool _isLeaf(BinNode<T>* t);//判断一个结点是不是叶子节点
bool _isNodeWithTwoChild(BinNode<T>* t);//判断一个结点是否有左右两棵子树
BinNode<T>* _NodeMin(BinNode<T>* t, BinNode<T>*& parent);//找到当前节点为根的树中的最小值
BinNode<T>* _Delete(BinNode<T>*& t);//删除根结点 并返回新的根节点,
//针对只有一颗左或右子树的结点,删除结点时用到
};
template<class T> BinTree<T>::BinTree()//默认构造
{
TNode = NULL;
}
template<class T> BinTree<T>::BinTree(T* arr, int len)//数组构造
{
TNode = NULL;
for (int i = 0; i < len; ++i)
{
Insert(*(arr + i));
}
}
template<class T> BinTree<T>::BinTree(vector<T> vec)//容器构造
{
TNode = NULL;
for (int i = 0; i < vec.size(); ++i)
{
Insert(vec[i]);
}
}
template<class T> bool BinTree<T>::Insert(T& key)//插入节点
{
BinNode<T>* p = new BinNode<T>();//临时结点
p->key = key;
p->lchild = p->rchild = NULL;
BinNode<T>* parent = new BinNode<T>();
parent = NULL;
if (TNode == NULL)//如果原树为空,则创建新树,插入的结点作为根节点
{
TNode = p;
return true;
}
else
{
BinNode<T>* cur = TNode;
while (cur)//插入的结点肯定是叶子结点,寻找叶子节点(parent)
{
if (cur->key == key)
return false;
else if (cur->key > key)
{
parent = cur;
cur = cur->lchild;
}
else if (cur->key < key)
{
parent = cur;
cur = cur->rchild;
}
}
if (p->key < parent->key)
{
parent->lchild = p;
return true;
}
else
{
parent->rchild = p;
return true;
}
}
}
template<class T> void BinTree<T>::_visit(BinNode<T>* T)//访问结点
{
cout << T->key << endl;
}
template<class T> void BinTree<T>::_InOrder(BinNode<T>* T)//递归中序遍历内部函数
{
if (T != NULL)
{
_InOrder(T->lchild);
_visit(T);
_InOrder(T->rchild);
}
}
template<class T> void BinTree<T>::InOrder()//中序遍历外部接口
{
_InOrder(TNode);
}
template<class T> BinNode<T>* BinTree<T>::_Search(BinNode<T>* t, T key)//递归查找结点内部函数
{
if (t == NULL)
return NULL;
else
{
if (t->key == key)
return t;
else if (t->key > key)
{
return _Search(t->lchild, key);
}
else if (t->key < key)
{
return _Search(t->rchild, key);
}
}
}
template<class T> bool BinTree<T>::Search(T key)//查找结点外部接口
{
return _Search(TNode, key) == NULL ? false : true;;
}
template<class T> bool BinTree<T>::isEmpty()//判断树是否为空
{
return TNode == NULL;
}
template<class T> bool BinTree<T>::_isLeaf(BinNode<T>* t)//判断一个结点是否是叶子结点
{
if (!isEmpty())
{
if (t->lchild == NULL && t->rchild == NULL)
return true;
else
return false;
}
else
return false;
}
template<class T> bool BinTree<T>::_isNodeWithTwoChild(BinNode<T>* t)//判断一个结点是否有左右两棵子树
{
if (!isEmpty())
{
if (t->lchild != NULL && t->rchild != NULL)
return true;
else
return false;
}
else
return false;
}
//找到当前节点为根的树中的最小值
template<class T> BinNode<T>* BinTree<T>::_NodeMin(BinNode<T>* t, BinNode<T>*& parent)
{
BinNode<T>* cur = t;
while (cur->lchild != NULL)
{
parent = cur;
cur = cur->lchild;
}
return cur;
}
template<class T> BinNode<T>* BinTree<T>::_Delete(BinNode<T>*& t)//一棵树仅有左孩子或者有孩子
{
if (t->lchild != NULL)
return t->lchild;
else
return t->rchild;
}
//删除结点
template<class T> bool BinTree<T>::Delete(T& key)
{
/*
先搜索找到目标结点z
删除节点分为三种情况:
1.若被删除节点z是叶节点,则直接删除,不会破坏二叉排序树的性质
2.若被删除节点z只有一棵左子树或右子树,则让z的子树替代z的位置
3.若被删除结点z有左右两棵子树,则令z的直接后继(或直接前驱)替代z,
然后从二叉排序树中删除这个直接后继(或直接前驱),这样就转换成了第一或第二种情况
*/
if (isEmpty())
{
cout << "空树 无法删除" << endl;
return false;
}
else
{
bool find = false;
BinNode<T>* parent=TNode;
//BinNode<T>* t = _Search(TNode, key,parent);
BinNode<T>* cur = TNode;
while (cur)
{
if (key < cur->key)
{
parent = cur;
cur = cur->lchild;
}
else if (key > cur->key)
{
parent = cur;
cur = cur->rchild;
}
else if (key == cur->key)
{
find = true;
break;
}
}
if (!find)
{
cout << "该结点未找到" << endl;
return false;
}
if (_isLeaf(cur))
{
if (parent->lchild->key == key)
parent->lchild = NULL;
else
parent->rchild = NULL;
delete cur;
return true;
}
else if (_isNodeWithTwoChild(cur))
{//该结点右子树的最小值即为该节点的直接后继
BinNode<T>* parent = cur;
BinNode<T>* temp = _NodeMin(cur->rchild,parent);
cur->key= temp->key;
if (parent->rchild == temp)
parent->rchild = _Delete(temp);
else if (parent->lchild == temp)
parent->lchild = _Delete(temp);
delete temp;
return true;
}
else
{
if (cur->lchild != NULL)
{
parent->lchild = _Delete(cur);
delete cur;
return true;
}
else
{
parent->rchild = _Delete(cur);
delete cur;
return true;
}
}
}
}
源.cpp
#include <iostream>
#include "BinTree.h"
using namespace std;
void test01()
{
int arr[] = { 19,13,11,8,50,26,21,30,66,60,70,63,61,65,71 };
BinTree<int> T(arr,15);
T.InOrder();
cout << "-------" << endl;
cout << T.Search(7) << endl;
cout << T.Search(70) << endl;
int a = 70;
cout << "-------1" << endl;
cout << T.Delete(a) << endl;
cout << "-------------2" << endl;
T.InOrder();
}
int main()
{
test01();
return 0;
}