code up问题 A: 算法9-9~9-12:平衡二叉树的基本操作

#include<cstdio>
#define maxn 102
#include<algorithm>
#include<vector>
#include<cmath>
#include<iostream>
#include<queue>
using namespace std;
//实现AVL树的基本操作,给一个插入序列,构建对应的AVL树
//结构体中加入以当前结点为根的树的高度
//在AVL树中插入一个结点使得插入后还是AVL树,插入一个结点可能会使某子树的平衡因子大于1或小于-1
//对于平衡因子为2的子树,对应结构有两种为LL和LR型,LL型树对其根结点使用右旋即可平衡
//LR型树对其根结点左子树使用左旋,变为LL型树然后进行LL型操作即可平衡
//对于平衡因子为-2的子树,对应结构有两种为RR和RL型
vector<int>origin,pre,post;
struct node
{
    
    
    int height;
    int data;
    node* lchild;
    node* rchild;
};
int getheight(node* root)
{
    
    
    if(root==NULL){
    
    
        return 0;
    }
    return root->height;
}
int getfactor(node* root)
{
    
    
    return getheight(root->lchild)-getheight(root->rchild);
}
void updateheight(node* root)
{
    
    
    if(root==NULL){
    
    
        return;
    }
    root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
}
void L(node*& root)
{
    
    
    node* temp=root->rchild;
    root->rchild=temp->lchild;
    temp->lchild=root;
    updateheight(root);
    updateheight(temp);
    root=temp;
}
void R(node*& root)
{
    
    
    node* temp=root->lchild;
    root->lchild=temp->rchild;
    temp->rchild=root;
    updateheight(root);
    updateheight(temp);
    root=temp;
}
node* newnode(int x)
{
    
    
    node* Node=new node;
    Node->data=x;
    Node->height=1;
    Node->lchild=NULL;
    Node->rchild=NULL;
    return Node;
}
void Insert(node*& root,int x)
{
    
    
    if(root==NULL){
    
    
        root=newnode(x);
        return;
    }
    if(x<root->data){
    
    
        Insert(root->lchild,x);
        updateheight(root);
        if(getfactor(root)==2){
    
    
            if(getfactor(root->lchild)==1){
    
    //LL型
                R(root);
            }
            else if(getfactor(root->lchild)==-1){
    
    //LR型
                L(root->lchild);
                R(root);
            }
        }
    }
    else{
    
    
        Insert(root->rchild,x);
        updateheight(root);
        if(getfactor(root)==-2){
    
    
            if(getfactor(root->rchild)==1){
    
    //RL型
                R(root->rchild);
                L(root);
            }
            else if(getfactor(root->rchild)==-1){
    
    //RR型
                L(root);
            }
        }
    }
}
node* buildAVL()
{
    
    
    node* root=NULL;
    for(int i=0;i<origin.size();i++){
    
    
        Insert(root,origin[i]);
    }
    return root;
}
bool Find(node* root,int x)
{
    
    
    if(root==NULL){
    
    
        return false;
    }
    if(root->data==x){
    
    
        return true;
    }
    if(x<root->data){
    
    
        Find(root->lchild,x);
    }
    else{
    
    
        Find(root->rchild,x);
    }
}
void Layerorder(node* root)
{
    
    
    if(root==NULL){
    
    
        return;
    }
    queue<node*>q1;
    q1.push(root);
    node* temp;
    while(!q1.empty()){
    
    
        temp=q1.front();
        q1.pop();
        printf("%d ",temp->data);
        if(temp->lchild!=NULL){
    
    
            q1.push(temp->lchild);
        }
        if(temp->rchild!=NULL){
    
    
            q1.push(temp->rchild);
        }
    }
    printf("\n");
}
int main()
{
    
    
    int n;
    scanf("%d",&n);
    int m;
    scanf("%d",&m);
    for(int i=0;i<n;i++){
    
    
        int temp;
        scanf("%d",&temp);
        origin.push_back(temp);
    }
    node* root;
    root=buildAVL();
    for(int i=0;i<m;i++){
    
    
        int temp;
        scanf("%d",&temp);
        if(Find(root,temp)){
    
    
            if(i!=m-1){
    
    
                printf("1 ");
            }
            else{
    
    
                printf("1\n");
            }
        }
        else{
    
    
            if(i!=m-1){
    
    
                printf("0 ");
            }
            else{
    
    
                printf("0\n");
            }
        }
    }
    return 0;
}

/输入
8
5 7 9 2 3 4 1 23
输出
5 3 9 2 4 7 23 1
已验证输出正确
/

猜你喜欢

转载自blog.csdn.net/weixin_45890608/article/details/111397098