04-树5 Root of AVL Tree
来源:互联网 发布:sha256算法的基本流程 编辑:程序博客网 时间:2024/06/06 03:15
04-树5 Root of AVL Tree(25 分)
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.
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 the 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
#include <iostream>#include<stack>using namespace std;struct AVL{ int data; int height; struct AVL *left; struct AVL *right;};int getHeight(AVL *T){ if(T) return max(getHeight(T->left),getHeight(T->right))+1; else return 0;}AVL *LL(AVL *A){ AVL *B=new AVL; B=A->left; A->left=B->right; B->right=A; A->height=max(getHeight(A->left),getHeight(A->right))+1; B->height=max(getHeight(B->left),A->height)+1; return B;}AVL *RR(AVL *A){ AVL *B=new AVL; B=A->right; A->right=B->left; B->left=A; A->height=max(getHeight(A->left),getHeight(A->right))+1; B->height=max(A->height,getHeight(B->right))+1; return B;}AVL *LR(AVL *A){ A->left=RR(A->left); return LL(A);}AVL *RL(AVL *A){ A->right=LL(A->right); return RR(A);}AVL* Insert(int x,AVL *T){ if(!T){ T=new AVL; T->data=x; T->height=0; T->left=T->right=NULL; } else if(x<T->data){ T->left=Insert(x,T->left); if(getHeight(T->left)-getHeight(T->right)==2){ if(x<T->left->data) T=LL(T); else T=LR(T); } } else if(x>T->data){ T->right=Insert(x,T->right); if(getHeight(T->left)-getHeight(T->right)==-2){ if(x>T->right->data) T=RR(T); else T=RL(T); } } T->height=max(getHeight(T->left),getHeight(T->right))+1; return T;}void deleteTree(AVL *T){ if(T->left) deleteTree(T->left); if(T->right) deleteTree(T->right); delete T;}int main() { int n,num; AVL *T=NULL; cin>>n; for(int i=0;i<n;i++){ cin>>num; T=Insert(num,T); } cout<<T->data<<endl; deleteTree T; return 0;}
阅读全文
0 0
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5- Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树5 Root of AVL Tree
- 04-树4. Root of AVL Tree
- 04-树3. Root of AVL Tree
- 04-树3. Root of AVL Tree
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree
- CentOS6.5 yum方式安装mysql 5.6
- 节奏游戏开发指南
- HDU
- python-用户交互程序
- StringMvc环境搭建以及小demo,例子
- 04-树5 Root of AVL Tree
- Spark On YARN内存和CPU分配
- 用Python学《微积分B》(重积分)
- [BZOJ]4430: [Nwerc2015]Guessing Camels赌骆驼 树状数组(思路好题)
- 如何转载别人的CSDN文章
- sql server 建表时collate Chinese_PRC_CI_AS 是什么意思
- Android 7.0 去除GoogleVR弹出的警告框
- 机器学习与深度学习
- 信用小白贷款难,什么姿势才能提高贷款通过率?