平衡二叉树模板/PAT1123 Is It a Complete AVL Tree

该博客介绍了PAT1123题目的解决方案,涉及AVL树这一平衡二叉搜索树的概念。题目要求根据给定序列插入元素并保持AVL树性质,输出最终树的层序遍历序列,并判断是否为完全二叉树。

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

我的心愿是世界和平!

平衡二叉树:不想写定义~~(可以写,但是没必要)~~

#include<cstdio>
#include<iostream>
using namespace std;

typedef struct node{
    int data;
    int height;
    struct node *left;
    struct node *right;
}*tree,Tree;

int Height(tree &T,int len)
{
    if(T == NULL) return len;
    if(T->left == NULL && T->right == NULL) return T->height;
    return max(Height(T->left,T->height),Height(T->right,T->height));
}

void change(tree &T,int len){
    if(T == NULL) return ;
    T->height = len;
    change(T->left,len+1);
    change(T->right,len+1);
}

//单旋转右旋
void singleRotateWithRight(tree &T)
{
    int t = T->height;
    tree L = T;
    T = L->left;
    L->left = T->right;
    T->right = L;
    T->height = t;
    change(T->left,t+1);
    change(T->right,t+1);
}

//单旋转左旋
void singleRotateWithLeft(tree &T)
{
    int t = T->height;
    tree R = T;
    T = R->right;
    R->right = T->left;
    T->left = R;
    T->height = t;
    change(T->left,t+1);
    change(T->right,t+1);
}

//双旋转,先左后右
void doubleRotateWithLeft(tree &T)
{
    singleRotateWithLeft(T->left);
    singleRotateWithRight(T);
}

//双旋转,先右后左
void doubleRotateWithRight(tree &T)
{
    singleRotateWithRight(T->right);
    singleRotateWithLeft(T);
}

void addNode(tree &T,int x,int len)
{
    if(T==NULL){
        T=new Tree;
        T->data = x;
        T->left = NULL;
        T->right = NULL;
        T->height = len;
    }else if(x < T->data){
        addNode(T->left,x,len+1);
        if(Height(T->left,T->height) - Height(T->right,T->height) == 2){//获得深度判断左右节点深度差
            if(x < T->left->data){
                singleRotateWithRight(T);
            }else{
                doubleRotateWithLeft(T);
            }
        }
    }else if(x > T->data){
        addNode(T->right,x,len+1);
        if(Height(T->right,T->height) - Height(T->left,T->height) == 2){
            if(x > T->right->data){
                singleRotateWithLeft(T);
            }else{
                doubleRotateWithRight(T);
            }
        }
    }
}

int main(){
    tree T = NULL;
    int m,a;
    scanf("%d",&m);
    for(int i = 0;i < m;i++){
        scanf("%d",&a);
        addNode(T,a,0);// T 这是我的树,a 这是树的节点值, 0 这是树的该节点的深度
    }
    return 0;
}

题目描述: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.
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.
输入: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.
输出:or 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.

题目在这里~

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<map>
using namespace std;

typedef struct node{
    int data;
    int height;
    struct node *left;
    struct node *right;
}*tree,Tree;
int m,a;

int Height(tree &T,int len)
{
    if(T == NULL) return len;
    if(T->left == NULL && T->right == NULL) return T->height;
    return max(Height(T->left,T->height),Height(T->right,T->height));
}

void change(tree &T,int len){
    if(T == NULL) return ;
    T->height = len;
    change(T->left,len+1);
    change(T->right,len+1);
}

void singleRotateWithRight(tree &T)
{
    int t = T->height;
    tree L = T;
    T = L->left;
    L->left = T->right;
    T->right = L;
    T->height = t;
    change(T->left,t+1);
    change(T->right,t+1);
}

void singleRotateWithLeft(tree &T)
{
    int t = T->height;
    tree R = T;
    T = R->right;
    R->right = T->left;
    T->left = R;
    T->height = t;
    change(T->left,t+1);
    change(T->right,t+1);
}

void doubleRotateWithLeft(tree &T)
{
    singleRotateWithLeft(T->left);
    singleRotateWithRight(T);
}

void doubleRotateWithRight(tree &T)
{
    singleRotateWithRight(T->right);
    singleRotateWithLeft(T);
}


void addNode(tree &T,int x,int len)
{
    if(T==NULL){
        T=new Tree;
        T->data = x;
        T->left = NULL;
        T->right = NULL;
        T->height = len;
    }else if(x < T->data){
        addNode(T->left,x,len+1);
        if(Height(T->left,T->height) - Height(T->right,T->height) == 2){
            if(x < T->left->data){
                singleRotateWithRight(T);
            }else{
                doubleRotateWithLeft(T);
            }
        }
    }else if(x > T->data){
        addNode(T->right,x,len+1);
        if(Height(T->right,T->height) - Height(T->left,T->height) == 2){
            if(x > T->right->data){
                singleRotateWithLeft(T);
            }else{
                doubleRotateWithRight(T);
            }
        }
    }
}

void levelOrder(tree &T){
    queue<int>q;
    queue<tree>que;
    tree t;
    que.push(T);
    int flag = 1,sign = 1;
    while(!que.empty()){
        t = new Tree;
        t = que.front();
        que.pop();
        q.push(t->data);
        if(t->left != NULL){
            que.push(t->left);
            if(flag == 0){
                sign = 0;
            }
        }else{
            flag = 0;
        }
        if(t->right!=NULL){
            que.push(t->right);
            if(flag == 0){
                sign = 0;
            }
        }else{
            flag = 0;
        }
    }
    cout<<q.front();
    q.pop();
    while(!q.empty()){
        cout<<" "<<q.front();
        q.pop();
    }
    if(sign) printf("\nYES");
    else printf("\nNO");
}

int main(){
    tree T = NULL;
    scanf("%d",&m);
    for(int i = 0;i < m;i++){
        scanf("%d",&a);
        addNode(T,a,0);
    }
    levelOrder(T);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值