二叉树基础


struct node
{
    int data;
    struct node * l, *r;
};
char z[53];
int cn, count, high;
struct node * creat()//先序输入
{
    struct node * root;
    if(z[++cn] == ',')
    {
        root = NULL;
    }
    else
    {
        root = (struct node *)malloc(sizeof(struct node));
        root->data = z[cn];
        root->l = creat();
        root->r = creat();
    }
    return root;
}

void zhong(struct node * root)//中序输出
{
    if(root)
    {
        zhong(root->l);
        printf("%c",root->data);
        zhong(root->r);
    }
}
void hou(struct node * root)//后续输出
{
    if(root)
    {
        hou(root->l);
        hou(root->r);
        printf("%c",root->data);
    }
}
void cheng(struct node * root)//层序输出
{
    struct node * z[100];
    int in = 0, out = 0;
    z[in++] = root;
    while(in > out)
    {
        if(z[out])
        {
            printf("%c",z[out]->data);
            z[in++] = z[out]->l;
            z[in++] = z[out]->r;
        }
        out++;
    }
}
int check(struct node *root)//叶子结点数
{
    if(root)
    {
        if((!root->l) && (!root->r))
        {
            count++;
        }
        check(root->l);
        check(root->r);
    }
    return count;
}
int deep(struct node * root)//树深/树高
{
    if(root)
    {
        int l = deep(root->l) + 1;
        int r = deep(root->r) + 1;
        if(l > r) return l;
        else return r;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41031401/article/details/81481788
今日推荐