0
点赞
收藏
分享

微信扫一扫

PAT 1123 Is It a Complete AVL Tree (30分)(完全二叉平衡树)


1123 Is It a Complete AVL Tree (30分)

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.

PAT 1123 Is It a Complete AVL Tree (30分)(完全二叉平衡树)_完全二叉树

PAT 1123 Is It a Complete AVL Tree (30分)(完全二叉平衡树)_子树_02

PAT 1123 Is It a Complete AVL Tree (30分)(完全二叉平衡树)_完全二叉树_03

PAT 1123 Is It a Complete AVL Tree (30分)(完全二叉平衡树)_结点_04

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:

5
88 70 61 63 65

Sample Output 1:

70 63 88 61 65
YES

Sample Input 2:

8
88 70 61 96 120 90 65 68

Sample Output 2:

88 65 96 61 70 90 120 68
NO

 题目大意:判断是否是完全二叉平衡树(完全二叉树+AVL树)

1、判断是不是完全二叉树,就看出现一个孩子为空的结点之后是否还会出现孩子为空的情况,是的话就不是完全二叉树,否则是

2、AVL树,建树,发现树不平衡然后根据旋转规则来旋转

A发现树不平衡的情况有四种:
新来的结点插入到A的左子树的左子树,解决:右旋
新来的结点插入到A的左子树的右子树,解决:左右旋
新来的结点插入到A的右子树的左子树,解决:右左旋
新来的结点插入到A的右子树的右子树,解决:左旋

(真是个好题,学到很多)

//给你插入的序列,让你输出二叉平衡树的层序序列,然后判断是不是完全二叉树 
#include <bits/stdc++.h>
using namespace std;
struct node{
int val;
node *left, *right;
node(int v):val(v), left(NULL), right(NULL){}
};
int n, isComplete, after;
vector<int> ans;

//左旋
node* leftRotate(node *rt) {
node* temp = rt->right;
rt->right = temp->left;
temp->left = rt;
return temp;
}
//右旋
node* rightRotate(node *rt) {
node* temp = rt->left;
rt->left = temp->right;
temp->right = rt;
return temp;
}
//左右旋
node* leftRightRotate(node *rt) {
rt->left = leftRotate(rt->left);
return rightRotate(rt);
}
//右左旋
node* rightLeftRotate(node *rt) {
rt->right = rightRotate(rt->right);
return leftRotate(rt);
}
int getHeight(node *rt) {
if(rt == NULL) return 0;
int l = getHeight(rt->left);
int r = getHeight(rt->right);
return max(l, r) + 1;
}
node* insert(node *rt, int val) {
if(rt == NULL) {
rt = new node(val);
}else if(val < rt->val) { //左边插
rt->left = insert(rt->left, val);
int l = getHeight(rt->left);
int r = getHeight(rt->right);
if(l - r >= 2) {
if( val < rt->left->val) { //左左情况
rt = rightRotate(rt); //右旋
}else { //左右情况
rt = leftRightRotate(rt); //左右旋
}
}
}else {
rt->right = insert(rt->right, val);
int l = getHeight(rt->left);
int r = getHeight(rt->right);
if(r - l >= 2) {
if(val > rt->right->val ){ //右右情况
rt = leftRotate(rt); //左旋
}else {
rt = rightLeftRotate(rt); //右左情况 右左旋
}
}
}
return rt;
}
void levelOrder(node* rt){
queue<node *> q;
q.push(rt);
while(!q.empty()) {
node *temp = q.front();
q.pop();
ans.push_back(temp->val);
if(temp->left != NULL) {
if(after) isComplete = 0;
q.push(temp->left);
} else {
after = 1;
}
if(temp->right != NULL) {
if(after) isComplete = 0;
q.push(temp->right);
}else after = 1;
}
}
int main() {
int temp;
node *root = NULL;
ans.clear(); isComplete = 1, after = 0;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> temp;
root = insert(root, temp);
}
levelOrder(root);
for(int i = 0; i <ans.size(); i++) {
cout << ans[i];
if(i != ans.size() - 1) cout << " ";
else cout << endl;
}
if(isComplete) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}

 

举报

相关推荐

0 条评论