二叉树代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    char data;
    struct node *leftchild,*rightchild;
};
struct node *creat(char first[],char mid[],int length)
{
    if(length<=0)
        return NULL;
    struct node *root;
    root=(struct node *)malloc(sizeof(struct node));
    root->data=first[0];
    int i;
    for(i=0;i<length;i++)
    {
        if(mid[i]==first[0])
            break;
    }
    root->leftchild=creat(first+1,mid,i)
    root->rightchild=creat(first+1+i,mid+i+1,length-i-1);
    return root;
};
struct node *creat()
{
    struct node *root;
    char ch;
    ch=str[tem++];
    if(ch==',')
        return NULL;
    else
    {
        root=(struct node *)malloc(sizeof(struct node));
        root->data=ch;
        root->leftchild=creat();
        root->righchild=creat();
    }
    return root;
};
void leveltraversal(struct node *root)//层次遍历
{
    struct node *q[55];//定义一个结构体数组,模拟队列
    int front=0,rear=0;
    q[rear++]=root;
    while(front<rear)
    {
        if(q[front])
        {
           // printf("%c",q[front]->data);//输出层次遍历的值
           if(q[front]->leftchild==NULL&&q[front]->rightchild==NULL)//输出叶子结点的值
                printf("%c",q[front]->data);
            q[rear++]=q[front]->leftchild;//根结点的左右孩子分别入队
            q[rear++]=q[front]->rightchild;
        }
        front++;//根结点出队列
    }
}
viod countleaf(struct node *root)//统计叶子数
{
    if(root)
    {
        if(root->leftchild==NULL&&root->rightchild==NULL)//结点左右为空,叶子增加1
            leaf++;
        countleaf(root->leftchild);
        countleaf(root->rightchild);
    }
}
int depth(struct node *root)//计算树的深度
{
    lnt leftdepth,rightdepth;
    if(root==NULL)
        return 0;
    leftdepth=depth(root->leftchild)+1;//左子树的深度
    rightdepth=depth(root->rightchild)+1;//右子树的深度
    return leftdeth>rightdepth?leftdepth:rightdepth;//返回左右两边深度大的那个
}

void mid(struct node *root)//中序遍历
{
    if(root)
    {
        mid(root->leftchild);
        printf("%c",root->data);
        mid(root->rightchild);

    }
}
void last(struct node *root)//后序遍历
{
    if(root)
    {
        last(root->leftchild);
        last(root->rightchild);
        printf("%c",root->data);
    }
}
void first(struct node *root)//先序遍历
{
    if(root=NULL)
        return ;
    printf("%c",root->data);
    first(root->leftchild);
    first(root->rightchild);

}
int y,n;//定义一个临时的变量,来统计输出的个数,当满足条件输出\n;
void println(struct node *root)//摆脱数据+空格的模式,最后一个不输出空格,用这种方法
{
    if(root==NULL)
        return ;
    println(root->leftchild);
    println(root->rightchild);
    if(y==n-1)//因为y是从0开始,所以最后 一个是n-1
        printf("%d\n",root->data);
    else
    {
        y++;
        printf("%d ",root->data);
    }
}
//排序二叉树
struct node *creat(struct node *root,int  key)
{
    if(root==NULL)
    {
        root=(struct node *)malloc(sizeof(struct node));
        root->data=key;
        root->leftchild=root->rightchild=NULL;
    }
    else
    {
        if(key<root->data)
            root->leftchild=creat(root->leftchild,key);
        else
            root->rightchild=creat(root->rightchild,key);
    }
    return root;
};

int judge(struct node *root,struct node *root2)
{
    if(root==NULL&&root2==NULL)
        return 1;
    else if(root!=NULL&&root2!=NULL)
    {
        if(root->data!=root2->data)
            return 0;
        else if(judge(root->leftchild,root2->leftchild)&&judge(root->rightchild,root2->rightchild))
            return 1;//就是在根节点值相等的条件下,来判断左右子树是否相等
        else  return 0;
    }
    else//一个有值,一个没值。
        return 0;
}



平衡二叉树
typedef int elemtype;
struct node
{
    elemtype data;
    int depth;
    struct node *leftchild,*rightchild;
};
int max(int x,int y)
{
    return x>y?x:y;
}
int treedepth(struct node *root)
{
    if(root==NULL)
        return -1;
    else
        return root->depth;
}


struct node *LL(struct node *root)//他现在是一条向左的线,所以向右旋转,
{
    struct node *p=root->leftchild;//用个临时变量,来指向根节点的左孩子,
    root->leftchild=p->rightchild;
    p->rightchild=root;
    p->depth=max(treedepth(p->leftchild),treedepth(p->rightchild))+1;
    root->depth=max(treedepth(root->leftchild),treedepth(root->rightchild))+1;
    return p;//此时p,已经成为了,比root节点高的节点,根节点,下同
};
struct node *RR(struct node *root)
{
    struct node *p=root->rightchild;
    root->rightchild=p->leftchild;
    p->leftchild=root;
    p->depth=max(treedepth(p->leftchild),treedepth(p->rightchild))+1;
    root->depth=max(treedepth(root->leftchild),treedepth(root->rightchild))+1;
    return p;
};
struct node *LR(struct node *root)//树形为先左后右。
{//先从根节点的左孩子开始,让他这一层,进行左旋就是RR.然后,让root,进行LL右旋。左左
    root->leftchild=RR(root->leftchild);
    return LL(root);//最后返回根节点
};

struct node *RL(struct node *root)
{
    root->rightchild=LL(root->rightchild);
    return RR(root);
};
struct node *creat(struct node *root,int x)
{
    if(root==NULL)
    {
        root=(struct node *)malloc(sizeof(struct node));
        root->data=x;
        root->leftchild=root->rightchild=NULL;
    }
    else if(root->data>x)
    {
        root->leftchild=creat(root->leftchild,x);
        if(treedepth(root->leftchild)-treedepth(root->rightchild)>1)//是否满足,左右子树深度差1;
        {//满足,进来
            if(root->leftchild->data>x)//根据它孩子和数据的大小,来判断是LL。。等等,什么形状
                root=LL(root);//根节点更换成了p,p传来的就是根节点
            else
                root=LR(root);
        }
    }
    else
    {
        root->rightchild=creat(root->rightchild,x);
        if(treedepth(root->rightchild)-treedepth(root->leftchild)>1)
        {
            if(root->rightchild->data<x)
                root=RR(root);
            else
                root=RL(root);
        }
    }
    root->depth=max(treedepth(root->leftchild),treedepth(root->rightchild))+1;
    return root;
};


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值