国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

二叉樹大全

2019-11-10 20:00:55
字體:
供稿:網(wǎng)友
#include <stdio.h>#include <string.h>#include <stdlib.h>struct tree{    int data;    struct tree *lchild, *rchild;};int i, flag;void BinarySortTreeCreat(struct tree *&t,int a);void BInarySortTreeCompare(struct tree *t1, struct tree *t2);void PReCreat(struct tree *&t,char *pre, int len);///need i = 0void PreinCreat(struct tree *&t,char *pre, char *in, int len);void InpostCreat(struct tree *&t,char *in, char *post, int len);void CengciOrder(struct tree *t);void PreOrder(struct tree *t);void InOrder(struct tree *t);void PostOrder(struct tree *t);int LeafCount(struct tree *t);void LeafOrder(struct tree *t);///up to down,left to rightint TreeHeight(struct tree *t);int main(){    int T, len, cnt, m, num[101];    struct tree *t;    char pre[51], in[51], post[51];    while(~scanf("%d",&T))    {        t = NULL;        for(int j = 0; j < T; j++)        {            scanf("%d",&num[j]);            BinarySortTreeCreat(t, num[j]);        }        flag = 0;        PostOrder(t);        printf("/n");    }    return 0;}void BinarySortTreeCreat(struct tree *&t,int a){    if(t == NULL)    {        t = (struct tree *)malloc(sizeof(struct tree));        t->data = a;        t->lchild = NULL;        t->rchild = NULL;    }    else    {        if(a > t->data)            BinarySortTreeCreat(t->rchild, a);        else            BinarySortTreeCreat(t->lchild, a);    }}void BInarySortTreeCompare(struct tree *t1, struct tree *t2){    if(t1 == NULL&&t2 == NULL)        return ;    if(t1||t2)    {        if(t1->data!=t2->data)        {            flag = 1;            return ;        }        BInarySortTreeCompare(t1->lchild, t2->lchild);        BInarySortTreeCompare(t1->rchild, t2->rchild);    }}void PreCreat(struct tree *&t,char *pre, int len){    if(len == 0)        return ;    if(pre[i] == ',')    {        t = NULL;        i++;    }    else    {        t = (struct tree *)malloc(sizeof(struct tree));        t->data = pre[i++];        PreCreat(t->lchild, pre, len);        PreCreat(t->rchild, pre, len);    }}void PreinCreat(struct tree *&t,char *pre, char *in, int len){    if(len <= 0)        t = NULL;    else    {        int a = strchr(in, pre[0]) - in;        t = (struct tree *)malloc(sizeof(struct tree));        t->data = pre[0];        PreinCreat(t->lchild, pre+1,in,a);        PreinCreat(t->rchild,pre+a+1,in+a+1,len-a-1);    }}void InpostCreat(struct tree *&t,char *in, char *post, int len){    if(len <= 0)        t = NULL;    else    {        int a = strchr(in, post[len-1]) - in;        t = (struct tree *)malloc(sizeof(struct tree));        t->data = post[len-1];        InpostCreat(t->lchild,in,post,a);        InpostCreat(t->rchild,in+a+1,post+a,len-1-a);    }}void CengciOrder(struct tree *t){    struct tree *q[55], *p;    int head=0, tail=0;    q[tail++]=t;    if(!t)        return ;    while(head < tail)    {        p=q[head++];        printf("%c",p->data);        if(p->lchild)            q[tail++]=p->lchild;        if(p->rchild)            q[tail++]=p->rchild;    }}void PreOrder(struct tree *t){    if(t!=NULL)    {        printf("%c",t->data);        PreOrder(t->lchild);        PreOrder(t->rchild);    }}void InOrder(struct tree *t){    if(t!=NULL)    {        InOrder(t->lchild);        printf(flag==0?"%d":" %d",t->data);        flag++;        InOrder(t->rchild);    }}void PostOrder(struct tree *t){    if(t!=NULL)    {        PostOrder(t->lchild);        PostOrder(t->rchild);        printf(flag==0?"%d":" %d",t->data);        flag++;    }}int LeafCount(struct tree *t){    if(t == NULL)        return 0;    if(t->lchild==NULL&&t->rchild==NULL)        return 1;    return LeafCount(t->lchild)+LeafCount(t->rchild);}void LeafOrder(struct tree *t){    struct tree *q[55], *p;    int head=0, tail=0;    q[tail++]=t;    if(!t)        return ;    while(head < tail)    {        p=q[head++];        if(p->lchild==NULL&&p->rchild==NULL)            printf("%c",p->data);        if(p->lchild)            q[tail++]=p->lchild;        if(p->rchild)            q[tail++]=p->rchild;    }}int TreeHeight(struct tree *t){    int lh=0,rh=0;    if(t==NULL)        return 0;    if(t->lchild!=NULL)        lh=TreeHeight(t->lchild);    else        lh=0;    if(t->rchild!=NULL)        rh=TreeHeight(t->rchild);    else        rh=0;    return (rh>lh)?rh+1:lh+1;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 博乐市| 连城县| 兴仁县| 合川市| 滨海县| 榆中县| 四平市| 南江县| 韩城市| 桑植县| 西华县| 南漳县| 天等县| 廉江市| 宾川县| 上犹县| 四川省| 洮南市| 昭觉县| 鄱阳县| 常德市| 嵊州市| 汝阳县| 峨眉山市| 武平县| 宜春市| 肇州县| 开江县| 中西区| 吴堡县| 鞍山市| 邯郸县| 香港 | 鹤岗市| 永安市| 怀宁县| 许昌市| 察雅县| 松滋市| 盐津县| 车险|