您的位置:首页 > 其它

二叉树的建立与遍历

2014-12-04 23:00 246 查看
1.从键盘输入前序序列建立二叉树

#include<stdio.h>

#include<malloc.h>

typedef struct BiNode

{

    char data;

    struct BiNode *lchild,*rchild;

}BiNode;

 

BiNode *Create(BiNode *root)

{

    char ch;

    root=(BiNode *)malloc(sizeof(BiNode));

    printf("Input the data:");

    scanf("%c",&ch);

    getchar();

    root->data=ch;

    if(root->data=='#')  return root;

    else

    {

    root->lchild=NULL;

    root->rchild=NULL;

    root->lchild=Create(root->lchild);

    root->rchild=Create(root->rchild);

    }

    return root;

}

 

void Pre(BiNode *root)

{

    if(root->data=='#') return;

    else

    {

        printf("%c",root->data);

        Pre(root->lchild);

        Pre(root->rchild);

    }

}

 

void Pin(BiNode *root)

{

    if(root->data=='#') return;

    else

    {

        Pin(root->lchild);

        printf("%c",root->data);

        Pin(root->rchild);

    }

}

 

void Post(BiNode *root)

{

    if(root->data=='#') return;

    else

    {

        Post(root->lchild);

        Post(root->rchild);

        printf("%c",root->data);

    }

}

 

 

int main()

{

    BiNode *root;

    root=NULL;

    root=Create(root);

    printf("前序序列为:");

    Pre(root);

    printf("\n中序序列为:");

    Pin(root);

    printf("\n后序序列为:");

    Post(root);

    return 0;

}

2.已知前序和中序序列建立二叉树

#include<stdio.h>

#include<string.h>

#include<malloc.h>

char pre[]="ABCDEFG";   //前序序列

char pin[]="CBEDAFG";   //中序序列

typedef struct BiNode

{

    char data;

    struct BiNode *lchild,*rchild;

}BiNode;

int pos(char pre[],char pin[],int i1,int i2,int k);

BiNode *Creat(BiNode *root,int i1,int i2,int k);

void PreOrder(BiNode *root);

void PinOrder(BiNode *root);

void PostOrder(BiNode *root);

 

int main()

{

    int i1=0,i2=0;       //i1表示前序序列下标,i2表示中序序列下标

    int k=strlen(pre);  //k表示序列长度

    BiNode *root=NULL;

    root=Creat(root,i1,i2,k);

    printf("前序序列为:");

    PreOrder(root);

    printf("\n中序序列为:");

    PinOrder(root);

    printf("\n后序序列为:");

    PostOrder(root);

    return 0;

}

 

int pos(char pre[],char pin[],int i1,int i2,int k)

{

    int i;

    for(i=i2;i<k;i++)

    {

       if(pin[i]==pre[i1]) break;

    }

    return i;

}

 

 

BiNode *Creat(BiNode *root,int i1,int i2,int k)

{

    int m,leftlen,rightlen;  //leftlen表示左子树长度,rightlen表示右子树长度

    if(k<=0) root=NULL;      //递归结束条件

    else

    {

       root=(BiNode *)malloc(sizeof(BiNode));

    root->data=pre[i1];

    m=pos(pre,pin,i1,i2,k);    //查找根结点在中序序列的位置

    leftlen=m-i2;

    rightlen=k-(leftlen+1);

    root->lchild=Creat(root->lchild,i1+1,i1,leftlen);

    root->rchild=Creat(root->rchild,i1+leftlen+1,m+1,rightlen);

    }

    return root;

}

 

void PreOrder(BiNode *root)  //前序遍历

{

    if(root==NULL) return;

    else

    {

        printf("%c",root->data);

        PreOrder(root->lchild);

        PreOrder(root->rchild);

    }

}

 

void PinOrder(BiNode *root)  //中序遍历

{

    if(root==NULL) return;

    else

    {

        PinOrder(root->lchild);

        printf("%c",root->data);

        PinOrder(root->rchild);

    }

}

 

void PostOrder(BiNode *root)   //后序遍历

{

    if(root==NULL) return;

    else

    {

        PostOrder(root->lchild);

        PostOrder(root->rchild);

        printf("%c",root->data);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: