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.
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
 
题意:
本题是1066题和1110题的综合,通过给定的插入序列,首先需要正确构建AVL树;其次判断该树是否为完全二叉树。
 
思路:
略。看1066题和1110题的题解即可。
 
代码:
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

struct Node{
    int val;
    int height;//以该结点为根结点的子树高度
    Node *lchild,*rchild;
    Node(int v):val(v),height(1),lchild(nullptr),rchild(nullptr){}
};

int n;//结点个数

int getHeight(Node* pNode)
{
    if(pNode==nullptr) return 0;
    else return pNode->height;
}

int getBalancedFactor(Node* pNode)
{
    return getHeight(pNode->lchild)-getHeight(pNode->rchild);
}

void updateHeight(Node* pNode)
{
    pNode->height=max(getHeight(pNode->lchild),getHeight(pNode->rchild))+1;
}

void leftRotation(Node* &pNode)
{
    Node* temp=pNode->rchild;
    pNode->rchild=temp->lchild;
    temp->lchild=pNode;
    updateHeight(pNode);
    updateHeight(temp);
    pNode=temp;
}

void rightRotation(Node* &pNode)
{
    Node* temp=pNode->lchild;
    pNode->lchild=temp->rchild;
    temp->rchild=pNode;
    updateHeight(pNode);
    updateHeight(temp);
    pNode=temp;
}

void insert(Node* &root,int val)
{
    if(root==nullptr){
        root=new Node(val);
        return;
    }
    if(val < root->val){
        insert(root->lchild,val);
        updateHeight(root);
        if(getBalancedFactor(root)==2){
            if(getBalancedFactor(root->lchild)==1){//LL型
                rightRotation(root);
            }else if(getBalancedFactor(root->lchild)==-1){//LR型
                leftRotation(root->lchild);
                rightRotation(root);
            }
        }
    }else{
        insert(root->rchild,val);
        updateHeight(root);
        if(getBalancedFactor(root)==-2){
            if(getBalancedFactor(root->rchild)==-1){//RR型
                leftRotation(root);
            }else if(getBalancedFactor(root->rchild)==1){//RL型
                rightRotation(root->rchild);
                leftRotation(root);
            }
        }
    }
}

//层序遍历,并判断是否为完全二叉树
bool levelOrderTraversal(Node* root)
{
    int cnt=0;
    bool flag=true;
    queue<Node*> q;
    q.push(root);
    while(!q.empty()){
        Node* temp=q.front();
        q.pop();
        if(temp){
            printf("%d",temp->val);
            cnt++;
            if(cnt<n) printf(" ");
            q.push(temp->lchild);
            q.push(temp->rchild);
        }else{
            if(cnt<n) flag=false;
        }
    }
    return flag;
}

int main()
{
    int val;
    Node* root=nullptr;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&val);
        insert(root,val);
    }
    bool flag=levelOrderTraversal(root);
    printf("n%s",flag?"YES":"NO");
    return 0;
}

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!