1123. Is It a Complete AVL Tree (30)

来源:互联网 发布:CSI网络犯罪调查百度云 编辑:程序博客网 时间:2024/05/17 08:38

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

#include <bits/stdc++.h>using namespace std;typedef struct AVLNode *Position;typedef struct AVLNode *AVLTree;typedef int ElementType;struct AVLNode {    ElementType Data;    AVLTree Left;    AVLTree Right;    AVLTree Parent;    int Height;//左子树减去右子树的差值};int GetHeight(Position P){    if (P == NULL) return -1;    else return P->Height;}AVLTree SingleLeftRotation(AVLTree A){    AVLTree 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), GetHeight(B->Right)) + 1;    return B;}AVLTree SingleRightRotation(AVLTree A){    AVLTree B = A->Right;    A->Right = B->Left;    B->Left = A;    A->Height = max(GetHeight(A->Left), GetHeight(A->Right)) + 1;    B->Height = max(GetHeight(B->Left), GetHeight(B->Right)) + 1;    return B;}AVLTree DoubleLeftRightRotation(AVLTree A){    A->Left = SingleRightRotation(A->Left);    return SingleLeftRotation(A);}AVLTree DoubleRightLeftRotation(AVLTree A){    A->Right = SingleLeftRotation(A->Right);    return SingleRightRotation(A);}AVLTree Insert(AVLTree T, ElementType X){    if (T == NULL)    {        T = (AVLTree)malloc(sizeof(struct AVLNode));        T->Data = X;        T->Height = 0;//        T->Left = T->Right = NULL;    }    else if (X < T->Data)    {        T->Left = Insert(T->Left, X);        //插入后可能需要左旋        if (GetHeight(T->Left)-GetHeight(T->Right) == 2)        {            if (X < T->Left->Data)                T = SingleLeftRotation(T);            else                T = DoubleLeftRightRotation(T);        }    }    else if (X > T->Data)    {        T->Right = Insert(T->Right, X);        if (GetHeight(T->Left)-GetHeight(T->Right) == -2)        {            if (X > T->Right->Data)                T = SingleRightRotation(T);            else                T = DoubleRightLeftRotation(T);        }    }    //最后更新树高    T->Height = max(GetHeight(T->Left), GetHeight(T->Right)) + 1;    return T;}Position FindMin(AVLTree T){    if (T == NULL) return NULL;    else if (T->Left == NULL) return T;    else return FindMin(T->Left);}Position FindMax(AVLTree T){    if (T == NULL) return NULL;    else if (T->Right == NULL) return T;    else return FindMax(T->Right);}bool LevelTraversal(AVLTree T){    queue<AVLTree> q;    if (T == NULL) return true;    q.push(T);    bool flagFirst = false;    bool IsComFlag = true, IsComEnd = false;    while(q.size() != 0) {        AVLTree AVL = q.front();        if (flagFirst == false) { cout << AVL->Data; flagFirst = true; }        else cout << " " << AVL->Data;        q.pop();        if (AVL->Left != NULL) q.push(AVL->Left);        if (AVL->Right != NULL) q.push(AVL->Right);        if (IsComFlag == true) {            if (!(AVL->Left) && (AVL->Right)) {                IsComFlag = false;            }            else if (!IsComEnd && (!(AVL->Left) || !(AVL->Right))) {                IsComEnd = true;            }            else if (IsComEnd && ((AVL->Left) || (AVL->Right))) {                IsComFlag = false;            }        }    }    return IsComFlag;}int main(){    AVLTree T = NULL;    int n;    scanf("%d", &n);    int k;    for (int i = 0; i < n; i++)    {        scanf("%d", &k);        T = Insert(T, k);    }    bool flag = LevelTraversal(T);    puts(flag ? "\nYES": "\nNO");    return 0;}
原创粉丝点击