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.
AC代码:
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 120Sample Output 1:
70Sample Input 2:
788 70 61 96 120 90 65Sample Output 2:
88
/*首先平衡二叉树是一个二叉排序树;其基本思想是:在构建二叉排序树的过程中,当每插入一个节点时,先检查是否因为插入而破坏了树的平衡性,若是,找出最小不平衡树,进行适应的旋转,使之成为新的平衡二叉树。*/#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
- Pat(Advanced Level)Practice--1066(Root of AVL Tree)
- PAT (Advanced Level) Practise 1066 Root of AVL Tree (25)
- PAT (Advanced Level) Practise 1066 Root of AVL Tree (25)
- 【PAT】【Advanced Level】1066. Root of AVL Tree (25)
- PAT (Advanced Level) 1066. Root of AVL Tree (25) AVL树的插入建树
- 1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1021(Deepest Root)
- PAT 1066 Root of AVL Tree
- Pat(Advanced Level)Practice--1020(Tree Traversals)
- PAT-1066 Root of AVL Tree(解题报告)
- 【PAT】1066. Root of AVL Tree (25)
- 【PAT】1066. Root of AVL Tree (25)
- PAT 1066. Root of AVL Tree
- PAT 1066. Root of AVL Tree
- pat(A) 1066. Root of AVL Tree
- PAT 1066. Root of AVL Tree (25)
- PAT 1066. Root of AVL Tree (25)
- pat 1066. Root of AVL Tree (25)
- 常用awk命令(转)
- WINCE6.0+S3C6410的触摸屏驱动
- 【Qt编程】Qt学习之Window and Dialog Widgets
- hdu 1421搬寝室(简单dp)
- node.js 之事件驱动
- Pat(Advanced Level)Practice--1066(Root of AVL Tree)
- 标准c++中string类介绍
- android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context re
- SEO基础知识完美教程
- 题目1056:最大公约数
- apache2.4.1源码安装过程
- Ubuntu上Invalid command 'RewriteEngine'解决办法
- SVNSubversion 用户权限管理
- SVN 状态图标不显示的解决办法