Pat(Advanced Level)Practice--1066(Root of AVL Tree)

来源:互联网 发布:linux文件类型有哪些 编辑:程序博客网 时间:2024/06/05 04:40

Pat1066代码

题目描述:
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree in one line.

Sample Input 1:
588 70 61 96 120
Sample Output 1:
70
Sample Input 2:
788 70 61 96 120 90 65
Sample Output 2:
88

AC代码:
/*首先平衡二叉树是一个二叉排序树;其基本思想是:在构建二叉排序树的过程中,当每插入一个节点时,先检查是否因为插入而破坏了树的平衡性,若是,找出最小不平衡树,进行适应的旋转,使之成为新的平衡二叉树。*/#include<cstdio>#include<cstdlib>#define LH 1#define EH 0#define RH -1#define MAX 25using namespace std;typedef struct BTNode{int data;int BF;//平衡因子(balance factor)struct BTNode *lchild,*rchild;}BTNode,*BTree;void R_Rotate(BTree *p)//以p为根节点的二叉排序树进行右旋转{BTree L;L=(*p)->lchild;(*p)->lchild=L->rchild;L->rchild=(*p);*p=L;//p指向新的根节点}void L_Rotate(BTree *p)//以p为根节点的二叉排序树进行左旋转{BTree R;R=(*p)->rchild;(*p)->rchild=R->lchild;R->lchild=(*p);*p=R;}void LeftBalance(BTree *T){BTree L,Lr;L=(*T)->lchild;switch(L->BF){//检查T的左子树平衡度,并作相应的平衡处理case LH://新节点插入在T的左孩子的左子树上,做单右旋处理(*T)->BF=L->BF=EH;R_Rotate(T);break;case RH://新插入节点在T的左孩子的右子树上,做双旋处理Lr=L->rchild;switch(Lr->BF){case LH:(*T)->BF=RH;L->BF=EH;break;case EH:(*T)->BF=L->BF=EH;break;case RH:(*T)->BF=EH;L->BF=LH;break;}Lr->BF=EH;L_Rotate(&(*T)->lchild);R_Rotate(T);}}void RightBalance(BTree *T){BTree R,Rl;R=(*T)->rchild;switch(R->BF){case RH://新节点插在T的右孩子的右子树上,要做单左旋处理(*T)->BF=R->BF=EH;L_Rotate(T);break;case LH://新节点插在T的右孩子的左子树上,要做双旋处理Rl=R->lchild;switch(Rl->BF){case LH:(*T)->BF=EH;R->BF=RH;break;case EH:(*T)->BF=R->BF=EH;break;case RH:(*T)->BF=LH;R->BF=EH;break;}Rl->BF=EH;R_Rotate(&(*T)->rchild);L_Rotate(T);}}bool InsertAVL(BTree *T,int e,bool *taller)//变量taller反应T长高与否{if(!*T){*T=(BTree)malloc(sizeof(BTNode));(*T)->data=e;(*T)->lchild=(*T)->rchild=NULL;(*T)->BF=EH;*taller=true;}else{if(e==(*T)->data)//不插入{*taller=false;return false; }if(e<(*T)->data){if(!InsertAVL(&(*T)->lchild,e,taller))//未插入return false;if(*taller)//以插入左子树,且左子树变高{switch((*T)->BF){case LH://原本左子树比右子树高,需要做左平衡处理LeftBalance(T);*taller=false;break;case EH://原本左右子树等高,现因左子树增高而树增高(*T)->BF=LH;*taller=true;break;case RH://原本右子树比左子树高,现在左右子树等高(*T)->BF=EH;*taller=false;break;}}}else{//应在T的右子树中搜寻if(!InsertAVL(&(*T)->rchild,e,taller))return false;if(*taller)//插入右子树,且右子树长高{switch((*T)->BF){case LH://原本左子树比右子树高,现在左右子树等高(*T)->BF=EH;*taller=false;break;case EH://原本左右子树等高,现在右子树变高(*T)->BF=RH;*taller=true;break;case RH://原本右子树比左子树高,现在需做右平衡处理RightBalance(T);*taller=false;break;}}}}return true;}int main(int argc,char *argv[]){int i,n;int A[MAX];BTree T=NULL;bool taller;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&A[i]);for(i=0;i<n;i++)InsertAVL(&T,A[i],&taller);printf("%d\n",T->data);return 0;}

参照了数据结构中平衡二叉树的实现。。。
2 0
原创粉丝点击