PAT A1123. Is It a Complete AVL Tree (30)
来源:互联网 发布:excel2007修复软件 编辑:程序博客网 时间:2024/06/04 19:24
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 output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<= 20). 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, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print "YES" if the tree is complete, or "NO" if not.
Sample Input 1:588 70 61 63 65Sample Output 1:
70 63 88 61 65YESSample Input 2:
888 70 61 96 120 90 65 68Sample Output 2:
88 65 96 61 70 90 120 68NO
#include <cstdio>#include<algorithm>#include<queue>#include<vector>using namespace std;const int Max = 40;int n;struct node{int data,height;node *lchild,*rchild;}*root;node * creat(int x){node* p= new node;p->data=x;p->height=1;p->lchild=p->rchild=NULL;return p;}int getHeight(node* &root){if(root==NULL) return 0; return root->height;}void getBalance(node* &root){root->height = max(getHeight(root->lchild),getHeight(root->rchild))+1;}intgetSet(node* &root){return getHeight(root->lchild)-getHeight(root->rchild);}void L (node* &root){node *k = root->rchild;root->rchild=k->lchild;k->lchild=root;getBalance(root);getBalance(k);root=k;}void R(node* &root){node* k=root->lchild;root->lchild=k->rchild;k->rchild=root;getBalance(root);getBalance(k);root=k;}void Insert(node* &root,int x){if(root==NULL){root=creat(x);return ;}if(root->data>x){Insert(root->lchild,x);getBalance(root);if(getSet(root)==2){if(getSet(root->lchild)==1){R(root);}else if(getSet(root->lchild)==-1){L(root->lchild);R(root);}}}else{Insert(root->rchild,x);getBalance(root);if(getSet(root)==-2){if(getSet(root->rchild)==-1){L(root);}else if(getSet(root->rchild)==1){R(root->rchild);L(root);}}}}int main(){int ans;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&ans);Insert(root,ans);}queue<node*> q;q.push(root);int u=1;while(!q.empty()){node* p = new node;p=q.front();q.pop();if(p->lchild==NULL&&p->rchild!=NULL ) u=0;if(getSet(p)<0) u=0;printf("%d",p->data);if(p->lchild!=NULL) q.push(p->lchild);if(p->rchild!=NULL) q.push(p->rchild);if(!q.empty()){printf(" ");}}printf("\n%s\n",u?"YES":"NO");system("pause");return 0;}
- PAT A1123. Is It a Complete AVL Tree (30)
- PAT A1123. Is It a Complete AVL Tree (30)
- PAT A1123. Is It a Complete AVL Tree (30)
- PAT A1123. Is It a Complete AVL Tree
- PAT 1123. Is It a Complete AVL Tree (30)
- PAT--1123. Is It a Complete AVL Tree (30)
- pat 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30) PAT 甲级
- PAT 1123. Is It a Complete AVL Tree (30)
- PAT 甲级 1123. Is It a Complete AVL Tree (30)
- PAT(A)-1123. Is It a Complete AVL Tree (30)(AVL+bfs)
- PAT甲1123Is It a Complete AVL Tree (30) --AVL
- 【PAT】1123. Is It a Complete AVL Tree
- 1123. Is It a Complete AVL Tree (30)-PAT甲级真题
- PAT (Advanced Level) Practise 1123Is It a Complete AVL Tree (30)
- PAT (Advanced Level) Practise 1123. Is It a Complete AVL Tree (30)
- PAT (Advanced Level) 1123. Is It a Complete AVL Tree (30)
- PAT (Advanced Level) Practise 1123 Is It a Complete AVL Tree (30)
- win10+caffe+CUDA8.0安装配置
- 关于数字根(LeetCode Add Digits)
- C# .NET更智能的数据库操作的封装完整版(重构)
- Java中 基本数据类型与封装类、字符串的相互转换 方法汇总
- LeetCode 506. Relative Ranks
- PAT A1123. Is It a Complete AVL Tree (30)
- 播放器:从RTP包提取TS
- 202. Happy Number
- #673 – 将鼠标位置和颜色进行映射(Mapping Mouse Position to Color)
- 16进制数值转换成16进制字符串,用于显示或对OllyDbg中的特征码进行整理
- 解决Minimum supported Gradle version is 3.3. Current version is 2.14.1
- 17-02-26
- PAT_A 1127. ZigZagging on a Tree (30)
- linux时间修改和同步