树的孩子表示法(指针形式)

寒假版本:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define m 3
typedef char type;
typedef struct node
{
    type info;
    struct node *child[m];
}node,*tree;
tree root;
//按照前序顺序输入一棵树
tree createtree()
{
    int i;
    char ch;
    tree t;
    if((ch=getchar())=='#')t=NULL;
    else
    {
        t=(tree)malloc(sizeof(node));
        t->info=ch;
        for(i=0;i<m;i++)
            t->child[i]=createtree();
    }
    return t;
}
//前序遍历输出树
void preorder(tree p)
{
    int i;
    if(p!=NULL)
    {
        printf("%c ",p->info);
        for(i=0;i<m;i++)
            preorder(p->child[i]);
    }
}
//后序遍历输出树
void postorder(tree p)
{
    int i;
    if(p!=NULL)
    {
        for(i=0;i<m;i++)
            postorder(p->child[i]);
        printf("%c ",p->info);
    }
}
//层次遍历输出树
void levelorder(tree t)
{
    tree queue[100];
    int f,r,i;
    tree p;
    f=0;r=1;queue[0]=t;
    while(f<r)
    {
        p=queue[f];//出队,并输出
        f++;
        printf("%c ",p->info);
        for(i=0;i<m;i++)
        {
            if(p->child[i])
            {
                queue[r]=p->child[i];
                r++;
            }
        }
    }
}
void destory(tree t)
{
    tree queue[100];
    int f,r,i;
    tree p;
    f=0;r=1;queue[0]=t;
    while(f<r)
    {
        p=queue[f];
        f++;
        for(i=0;i<m;i++)
        {
            if(p->child[i])
            {
                queue[r]=p->child[i];
                r++;
            }
        }
        if(p)
        {
             free(p);
        printf("del
");
        }
    }
}
int main()
{
    tree A=createtree();
    preorder(A);
    printf("
");
    //AB###CE###FH#####G###D###
    postorder(A);
    printf("
");
    levelorder(A);
    printf("
");
    destory(A);
    return 0;
}


课余版本:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<time.h>
#define MAX 2
typedef int type;
typedef struct node
{
    type data;
    struct node*child[MAX];
}tnode,*tree;
tree creat()
{
    int x,i;
    tree t;
    scanf("%d",&x);
    if(x==0)
        t=NULL;
    else
    {
        t=(tnode*)malloc(sizeof(tnode));
        t->data=x;
        for(i=0; i<MAX; i++)
            t->child[i]=creat();
    }

    return t;
}
void preorder(tree t)
{
    if(t)
    {
        int i;
        printf("%d ",t->data);
        for(i=0;i<MAX;i++)
            preorder(t->child[i]);
    }
}
void postorder(tree t)
{
    if(t)
    {   int i;
        for(i=0;i<MAX;i++)
            postorder(t->child[i]);
        printf("%d ",t->data);
    }
}
void levelorder(tree t)
{
    tree queue[100];
    int front=0,rear=0,i;
    if(t)queue[rear++]=t;
    while(front<rear)
    {
        tree p=queue[front++];
        printf("%d ",p->data);
        for(i=0;i<MAX;i++)
            if(p->child[i])
            queue[rear++]=p->child[i];
    }
}
void destory(tree t)
{
    tree queue[100];
    int front=0,rear=0,i;
    if(t)queue[rear++]=t;
    while(front<rear)
    {
        tree p=queue[front++];
        for(i=0;i<MAX;i++)
            if(p->child[i])
            queue[rear++]=p->child[i];
        //printf("del :%d
",p->data);
        free(p);
    }
}
void test()
{
    tree t=creat();
    printf("
preorder:	");   preorder(t);
    printf("
postorder:	");  postorder(t);
    printf("
levelorder:	"); levelorder(t);
    destory(t);
}
int main()
{
    //1 2 4 0 7 0 0 5 0 0 3 0 6 0 0
    test();
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/Thereisnospon/p/4768514.html