1123 Is It a Complete AVL Tree (30point(s)) - C語言 PAT 甲級

1123 Is It a Complete AVL Tree (30point(s))

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.
frgurefigure2figrue3figure4
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:

5
88 70 61 63 65

Sample Output:

70 63 88 61 65
YES

Sample Input:

8
88 70 61 96 120 90 65 68

Sample Output:

88 65 96 61 70 90 120 68
NO

題目大意:

輸入 N 個節點插入 AVL 樹中,

輸出 AVL 樹的層序遍歷,並判斷是否爲完全二叉樹

設計思路:
  • 建樹直接上 AVL 代碼
  • 層序遍歷用隊列遍歷
  • 判斷完全二叉樹
    • 若一個節點沒有孩子節點,則這個節點後的所有節點也沒有孩子節點,即爲完全二叉樹
    • 否則不是完全二叉樹
編譯器:C (gcc)
#include <stdio.h>
#include <stdlib.h>

struct node {
        int d;
        struct node *left, *right;
};

int max(int a, int b)
{
        return a > b ? a : b;
}

int get_height(struct node *root)
{
        if (root == NULL)
                return 0;
        return max(get_height(root->left), get_height(root->right)) + 1;
}

struct node *left_rotate(struct node *root)
{
        struct node *t = root->right;
        root->right = t->left;
        t->left = root;
        return t;
}

struct node *right_rotate(struct node *root)
{
        struct node *t = root->left;
        root->left = t->right;
        t->right = root;
        return t;
}

struct node *right_left_rotate(struct node *root)
{
        root->right = right_rotate(root->right);
        return left_rotate(root);
}

struct node *left_right_rotate(struct node *root)
{
        root->left = left_rotate(root->left);
        return right_rotate(root);
}

struct node *insert(struct node *root, int d)
{
        if (root == NULL) {
                root = (struct node *)malloc(sizeof(struct node));
                root->d = d;
                root->left = NULL;
                root->right = NULL;
        } else if (d < root->d) {
                root->left = insert(root->left, d);
                if (get_height(root->left) - get_height(root->right) == 2)
                                root = d < root->left->d ? right_rotate(root) : left_right_rotate(root);
        } else {
                root->right = insert(root->right, d);
                if (get_height(root->left) - get_height(root->right) == -2)
                        root = d > root->right->d ? left_rotate(root) : right_left_rotate(root);
        }
        return root;
}

struct node *insert(struct node *root, int d)
{
        if (root == NULL) {
                root = (struct node *)malloc(sizeof(struct node));
                root->d = d;
                root->left = NULL;
                root->right = NULL;
        } else if (d < root->d) {
                root->left = insert(root->left, d);
                if (get_height(root->left) - get_height(root->right) == 2)
                                root = d < root->left->d ? right_rotate(root) : left_right_rotate(root);
        } else {
                root->right = insert(root->right, d);
                if (get_height(root->left) - get_height(root->right) == -2)
                        root = d > root->right->d ? left_rotate(root) : right_left_rotate(root);
        }
        return root;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章