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

本文探讨了AVL树的性质及其在层序遍历下的表现,通过实例展示了如何判断AVL树是否同时构成完全二叉树,提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

int main(void)
{
        int n;
        int i, t;
        struct node *root = NULL;
        scanf("%d", &n);
        for (i = 0; i < n; i++) {
                scanf("%d", &t);
                root = insert(root, t);
        }

        struct node *p[20], *temp;
        int front, rear, cnt;
        int child_null = 0, complete = 1;
        p[0] = root;
        front = 0;
        rear = 1;
        cnt = 1;
        while (cnt) {
                temp = p[front++];
                cnt--;
                printf("%s%d", temp == root ? "" : " ", temp->d);
                if (temp->left != NULL) {
                        if (child_null)
                                complete = 0;
                        p[rear++] = temp->left;
                        cnt++;
                } else {
                        child_null = 1;
                }

                if (temp->right != NULL) {
                        if (child_null)
                                complete = 0;
                        p[rear++] = temp->right;
                        cnt++;
                } else {
                        child_null = 1;
                }
        }
        printf("\n%s", complete ? "YES" : "NO");

        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值