第十四周專案1平衡二叉樹
/*Copyright (c) 2015, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:H1.cpp
* 作者:辛志勐
* 完成日期:2015年12月5日
* 版本號:VC6.0
* 問題描述:平衡二叉樹
* 輸入描述:無
* 程式輸出:平衡二叉樹的建立和刪除
*/
#include <stdio.h>
#include <malloc.h>
typedef int KeyType; //定義關鍵字型別
typedef char InfoType;
typedef struct node //記錄型別
{
KeyType key; //關鍵字項
int bf; //平衡因子
InfoType data; //其他資料域
struct node *lchild,*rchild; //左右孩子指標
} BSTNode;
void LeftProcess(BSTNode *&p,int &taller)
//對以指標p所指結點為根的二叉樹作左平衡旋轉處理,本演算法結束時,指標p指向新的根結點
{
BSTNode *p1,*p2;
if (p->bf==0) //原本左、右子樹等高,現因左子樹增高而使樹增高
{
p->bf=1;
taller=1;
}
else if (p->bf==-1) //原本右子樹比左子樹高,現左、右子樹等高
{
p->bf=0;
taller=0;
}
else //原本左子樹比右子樹高,需作左子樹的平衡處理
{
p1=p->lchild; //p指向*p的左子樹根結點
if (p1->bf==1) //新結點插入在*b的左孩子的左子樹上,要作LL調整
{
p->lchild=p1->rchild;
p1->rchild=p;
p->bf=p1->bf=0;
p=p1;
}
else if (p1->bf==-1) //新結點插入在*b的左孩子的右子樹上,要作LR調整
{
p2=p1->rchild;
p1->rchild=p2->lchild;
p2->lchild=p1;
p->lchild=p2->rchild;
p2->rchild=p;
if (p2->bf==0) //新結點插在*p2處作為葉子結點的情況
p->bf=p1->bf=0;
else if (p2->bf==1) //新結點插在*p2的左子樹上的情況
{
p1->bf=0;
p->bf=-1;
}
else //新結點插在*p2的右子樹上的情況
{
p1->bf=1;
p->bf=0;
}
p=p2;
p->bf=0; //仍將p指向新的根結點,並置其bf值為0
}
taller=0;
}
}
void RightProcess(BSTNode *&p,int &taller)
//對以指標p所指結點為根的二叉樹作右平衡旋轉處理,本演算法結束時,指標p指向新的根結點
{
BSTNode *p1,*p2;
if (p->bf==0) //原本左、右子樹等高,現因右子樹增高而使樹增高
{
p->bf=-1;
taller=1;
}
else if (p->bf==1) //原本左子樹比右子樹高,現左、右子樹等高
{
p->bf=0;
taller=0;
}
else //原本右子樹比左子樹高,需作右子樹的平衡處理
{
p1=p->rchild; //p指向*p的右子樹根結點
if (p1->bf==-1) //新結點插入在*b的右孩子的右子樹上,要作RR調整
{
p->rchild=p1->lchild;
p1->lchild=p;
p->bf=p1->bf=0;
p=p1;
}
else if (p1->bf==1) //新結點插入在*p的右孩子的左子樹上,要作RL調整
{
p2=p1->lchild;
p1->lchild=p2->rchild;
p2->rchild=p1;
p->rchild=p2->lchild;
p2->lchild=p;
if (p2->bf==0) //新結點插在*p2處作為葉子結點的情況
p->bf=p1->bf=0;
else if (p2->bf==-1) //新結點插在*p2的右子樹上的情況
{
p1->bf=0;
p->bf=1;
}
else //新結點插在*p2的左子樹上的情況
{
p1->bf=-1;
p->bf=0;
}
p=p2;
p->bf=0; //仍將p指向新的根結點,並置其bf值為0
}
taller=0;
}
}
int InsertAVL(BSTNode *&b,KeyType e,int &taller)
/*若在平衡的二叉排序樹b中不存在和e有相同關鍵字的結點,則插入一個
資料元素為e的新結點,並返回1,否則返回0。若因插入而使二叉排序樹
失去平衡,則作平衡旋轉處理,布林變數taller反映b長高與否*/
{
if(b==NULL) //原為空樹,插入新結點,樹“長高”,置taller為1
{
b=(BSTNode *)malloc(sizeof(BSTNode));
b->key=e;
b->lchild=b->rchild=NULL;
b->bf=0;
taller=1;
}
else
{
if (e==b->key) //樹中已存在和e有相同關鍵字的結點則不再插入
{
taller=0;
return 0;
}
if (e<b->key) //應繼續在*b的左子樹中進行搜尋
{
if ((InsertAVL(b->lchild,e,taller))==0) //未插入
return 0;
if (taller==1) //已插入到*b的左子樹中且左子樹“長高”
LeftProcess(b,taller);
}
else //應繼續在*b的右子樹中進行搜尋
{
if ((InsertAVL(b->rchild,e,taller))==0) //未插入
return 0;
if (taller==1) //已插入到b的右子樹且右子樹“長高”
RightProcess(b,taller);
}
}
return 1;
}
void DispBSTree(BSTNode *b) //以括號表示法輸出AVL
{
if (b!=NULL)
{
printf("%d",b->key);
if (b->lchild!=NULL || b->rchild!=NULL)
{
printf("(");
DispBSTree(b->lchild);
if (b->rchild!=NULL) printf(",");
DispBSTree(b->rchild);
printf(")");
}
}
}
void LeftProcess1(BSTNode *&p,int &taller) //在刪除結點時進行左處理
{
BSTNode *p1,*p2;
if (p->bf==1)
{
p->bf=0;
taller=1;
}
else if (p->bf==0)
{
p->bf=-1;
taller=0;
}
else //p->bf=-1
{
p1=p->rchild;
if (p1->bf==0) //需作RR調整
{
p->rchild=p1->lchild;
p1->lchild=p;
p1->bf=1;
p->bf=-1;
p=p1;
taller=0;
}
else if (p1->bf==-1) //需作RR調整
{
p->rchild=p1->lchild;
p1->lchild=p;
p->bf=p1->bf=0;
p=p1;
taller=1;
}
else //需作RL調整
{
p2=p1->lchild;
p1->lchild=p2->rchild;
p2->rchild=p1;
p->rchild=p2->lchild;
p2->lchild=p;
if (p2->bf==0)
{
p->bf=0;
p1->bf=0;
}
else if (p2->bf==-1)
{
p->bf=1;
p1->bf=0;
}
else
{
p->bf=0;
p1->bf=-1;
}
p2->bf=0;
p=p2;
taller=1;
}
}
}
void RightProcess1(BSTNode *&p,int &taller) //在刪除結點時進行右處理
{
BSTNode *p1,*p2;
if (p->bf==-1)
{
p->bf=0;
taller=-1;
}
else if (p->bf==0)
{
p->bf=1;
taller=0;
}
else //p->bf=1
{
p1=p->lchild;
if (p1->bf==0) //需作LL調整
{
p->lchild=p1->rchild;
p1->rchild=p;
p1->bf=-1;
p->bf=1;
p=p1;
taller=0;
}
else if (p1->bf==1) //需作LL調整
{
p->lchild=p1->rchild;
p1->rchild=p;
p->bf=p1->bf=0;
p=p1;
taller=1;
}
else //需作LR調整
{
p2=p1->rchild;
p1->rchild=p2->lchild;
p2->lchild=p1;
p->lchild=p2->rchild;
p2->rchild=p;
if (p2->bf==0)
{
p->bf=0;
p1->bf=0;
}
else if (p2->bf==1)
{
p->bf=-1;
p1->bf=0;
}
else
{
p->bf=0;
p1->bf=1;
}
p2->bf=0;
p=p2;
taller=1;
}
}
}
void Delete2(BSTNode *q,BSTNode *&r,int &taller)
//由DeleteAVL()呼叫,用於處理被刪結點左右子樹均不空的情況
{
if (r->rchild==NULL)
{
q->key=r->key;
q=r;
r=r->lchild;
free(q);
taller=1;
}
else
{
Delete2(q,r->rchild,taller);
if (taller==1)
RightProcess1(r,taller);
}
}
int DeleteAVL(BSTNode *&p,KeyType x,int &taller) //在AVL樹p中刪除關鍵字為x的結點
{
int k;
BSTNode *q;
if (p==NULL)
return 0;
else if (x<p->key)
{
k=DeleteAVL(p->lchild,x,taller);
if (taller==1)
LeftProcess1(p,taller);
return k;
}
else if (x>p->key)
{
k=DeleteAVL(p->rchild,x,taller);
if (taller==1)
RightProcess1(p,taller);
return k;
}
else //找到了關鍵字為x的結點,由p指向它
{
q=p;
if (p->rchild==NULL) //被刪結點右子樹為空
{
p=p->lchild;
free(q);
taller=1;
}
else if (p->lchild==NULL) //被刪結點左子樹為空
{
p=p->rchild;
free(q);
taller=1;
}
else //被刪結點左右子樹均不空
{
Delete2(q,q->lchild,taller);
if (taller==1)
LeftProcess1(q,taller);
p=q;
}
return 1;
}
}
int main()
{
BSTNode *b=NULL;
int i,j,k;
KeyType a[]= {16,3,7,11,9,26,18,14,15},n=9; //例10.5
printf(" 建立一棵AVL樹:\n");
for(i=0; i<n; i++)
{
printf(" 第%d步,插入%d元素:",i+1,a[i]);
InsertAVL(b,a[i],j);
DispBSTree(b);
printf("\n");
}
printf(" AVL:");
DispBSTree(b);
printf("\n");
printf(" 刪除結點:\n"); //例10.6
k=11;
printf(" 刪除結點%d:",k);
DeleteAVL(b,k,j);
printf(" AVL:");
DispBSTree(b);
printf("\n");
k=9;
printf(" 刪除結點%d:",k);
DeleteAVL(b,k,j);
printf(" AVL:");
DispBSTree(b);
printf("\n");
k=15;
printf(" 刪除結點%d:",k);
DeleteAVL(b,k,j);
printf(" AVL:");
DispBSTree(b);
printf("\n\n");
return 0;
}
學習心得:平衡二叉樹的原理懂了,雖然博文發了,但是程式碼還得慢慢琢磨呀。