平衡二叉树传统的旋转方法不易理解,通过分析二叉排序树的基本原理,[张标汉. 平衡二叉树调整教学探讨[J]. 计算机教育, 2009(10):53-54.]提出一种易于理解的平衡二叉树调整方法。
以下是基于此方法的C++实现。
//平衡二叉树
/*
1.输入一个数列L,生成一棵平衡二叉树T
2.对平衡二叉树T作递增、递减输出
3. 计算平衡二叉树T的平均查找长度,输出结果
//4.在二叉排序树T上删除指定结点
5.输出平衡二叉树T中指定结点的平衡因子
*/
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 100
struct BiNode
{
int data;
BiNode * lchild, * rchild;
};
class BiSortTree
{
public:
BiSortTree(int r[],int n);
BiSortTree();
~BiSortTree(){}
void Creat(int r[],int n);
void InsertBST(BiNode * &root,BiNode * &s);
void DeleteBST(BiNode * p,BiNode * f);
BiNode * SearchBST(BiNode * root,int k);
BiNode * SearchBSTFarther(BiNode * root,BiNode * son);
void IncreasingTraverse(BiNode * root);
void DecreasingTraverse(BiNode * root);
BiNode * GetRoot();
int GetLeafNumCount();
int GetDeep(BiNode * root,int k);
int height(BiNode * root);
int GetBalanceFactor(BiNode * tNode);
BiNode * FindMinSubTree(BiNode * &s);
void Adjust(BiNode *minroot,BiNode * &s);
private:
BiNode * root;
void AddLeafNum(int num);
int LeafNum[N];
int LeafNumCount;
};
BiNode * BiSortTree::GetRoot()
{
return root;
}
int BiSortTree::height(BiNode * root)
{
if(root==NULL) return 0;
else
{
int hl=height(root->lchild);
int hr=height(root->rchild);
return hl>hr?hl+1:hr+1;
}
}
//构造二叉排序树
BiSortTree::BiSortTree(int r[],int n)
{
Creat(r,n);
}
BiSortTree::BiSortTree()
{
LeafNumCount=0;
}
void BiSortTree::Creat(int r[],int n)
{
int i;
root=NULL;
LeafNumCount=0;
for(i=0;i
BiNode * s=new BiNode;
s->lchild=s->rchild=NULL;
s->data=r[i];
InsertBST(root,s);
//cout<<"Creat Traverse"<
}
BiNode * BiSortTree::FindMinSubTree(BiNode * &s){
BiNode *t=root,*bnd[N];
int i=0;
while(t!=s){
if(abs(GetBalanceFactor(t))>1)
bnd[i++]=t;
if(s->data
else t=t->rchild;
}
if(i>0)return bnd[i-1];else return NULL;
}
void BiSortTree::Adjust(BiNode *minroot,BiNode * &s){
BiNode *A=minroot,*B,*C;
if(s->data
else B=A->rchild;
if(s->data
else C=B->rchild;
BiNode *F=SearchBSTFarther(root,A);
if(C->data
{
cout<<"LL"<
B->rchild=A;
if(F!=NULL)if(F->lchild==A)F->lchild=B;
else F->rchild=B;
else root=B;
}
if(A->data
{cout<<"RR"<
B->lchild=A;
if(F!=NULL)if(F->lchild==A)F->lchild=B;
else F->rchild=B;
else root=B;
}
if(B->data
{cout<<"LR"<
C->lchild=B;C->rchild=A;
if(F!=NULL)if(F->lchild==A)F->lchild=C;
else F->rchild=C;
else root=C;
}
if(B->data>C->data&&C->data>A->data)//RL
{cout<<"RL"<
C->lchild=A;C->rchild=B;
if(F!=NULL)if(F->lchild==A)F->lchild=C;
else F->rchild=C;
else root=C;
}
}
//插入
void BiSortTree::InsertBST(BiNode * &root,BiNode * &s)
{ BiNode *minroot;
//cout<<"InsertBST"<
{
root=s;
minroot=FindMinSubTree(s);
if(minroot!=NULL){
cout<<"FindMinSubTree"<
}
}
else if(s->data
{
InsertBST(root->lchild,s);
}
else
{
InsertBST(root->rchild,s);
}
}
void BiSortTree::DeleteBST(BiNode * p,BiNode *f)
{
if((p->lchild==NULL)&&(p->rchild==NULL)) // delete leaf node
{
if(f->lchild==p) f->lchild=NULL;else f->rchild=NULL;
delete p;
}
else if(p->rchild==NULL) //delete node only has lchild
{
if(f->lchild==p) f->lchild=p->lchild;else f->rchild=p->lchild;
delete p;
}
else if(p->lchild==NULL) //delete node only has rchild
{
if(f->lchild==p) f->lchild=p->rchild;else f->rchild=p->rchild;
delete p;
}
else //delete node has both rchild and lchild
{
BiNode * par=p;
BiNode * s=p->rchild;
while(s->lchild!=NULL)
{
par=s;
s=s->lchild;
}
p->data=s->data;
if(par==p) par->rchild=s->rchild;
else par->lchild=s->rchild;
delete s;
}
}
//查找
BiNode * BiSortTree::SearchBST(BiNode * root,int k)
{
if(root==NULL)
{
return NULL;
}
else if((root->data)==k)
{
return root;
}
else if((root->data)
return SearchBST(root->rchild,k);
}
else
{
return SearchBST(root->lchild,k);
}
}
int BiSortTree::GetDeep(BiNode * root,int k)
{
if(root==NULL)
{
return 0;
}
else if((root->data)==k)
{
return 1;
}
else if((root->data)
return GetDeep(root->rchild,k)+1;
}
else
{
return GetDeep(root->lchild,k)+1;
}
}
BiNode * BiSortTree::SearchBSTFarther(BiNode * root,BiNode * son)
{
if(root==NULL)
{
return NULL;
}
else if((root->data)
{
if( ( (root->lchild)==son) || ( (root->rchild)==son) )
{
return root;
}
else
{
return SearchBSTFarther(root->rchild,son);
}
}
else
{
if( ( (root->lchild)==son) || ( (root->rchild)==son) )
{
return root;
}
else
{
return SearchBSTFarther(root->lchild,son);
}
}
}
//遍历
void BiSortTree::IncreasingTraverse(BiNode * root)
{
if(root==NULL)
{
return ;
}
IncreasingTraverse(root->lchild);
cout<
IncreasingTraverse(root->rchild);
}
void BiSortTree::DecreasingTraverse(BiNode * root)
{
if(root==NULL)
{
return ;
}
DecreasingTraverse(root->rchild);
cout<
DecreasingTraverse(root->lchild);
}
int BiSortTree::GetBalanceFactor(BiNode * tNode)
{
return height(tNode->lchild)-height(tNode->rchild);
}
int main()
{
//cout<<"ok"<
BiSortTree T;
int i,n=8,x;
int SumDeep=0;
// cout<<"Input the numbers(End by -1):"<
for(i=0;i
cout<<"数列L中有"<
cout<<"对二叉排序树T作递增输出:"< for(i=0;i /*cin>>x; return 0;
cout<
cout<
SumDeep+=T.GetDeep(T.GetRoot(),L[i]);
}
cout<<"二叉排序树T的平均查找长度: "<<(SumDeep*1.0/n)<
cout<<"在二叉排序树T上删除值为"<
if(DelNode!=NULL)
{
BiNode * DelNodeFarther=T.SearchBSTFarther(T.GetRoot(),DelNode);
T.DeleteBST(DelNode,DelNodeFarther);
}
else cout<<"值为"<
cout<
cout<
for(i=0;i
if(tNode!=NULL)cout<<"值为"<
}