树的后序遍历方式源码

来源:互联网 发布:windows longhorn开机 编辑:程序博客网 时间:2024/06/06 18:52
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    char data;
    struct node *llink;
    struct node *rlink;
}NODE;
NODE*build()//
{
    NODE *tree;
    char c;
    scanf("%c",&c);
    if(c=='#') tree = NULL;
    else
    {
        tree=(NODE*)malloc(sizeof(NODE));
        tree->data=c;
        tree->llink=build();
        tree->rlink=build();
    }
    return tree;
}
void houxu(NODE*tree)
{
    NODE*pt=tree;
    int tt[100],top=-1;
    NODE*s[100];
    do
    {
        while(pt!=NULL)
        {
            s[++top]=pt;
            tt[top]=0;
            pt=pt->llink;
        }
        if(top>=0)
        {
            if(!tt[top])
            {
                pt=s[top];
                tt[top]=1;
                pt=pt->rlink;
            }
            else printf("%c",s[top--]->data);
        }
    }while((pt!=NULL)||(top!=-1));
}
int main()
{
    printf("请输入树的结点:\n");
    NODE*tree=(NODE*)malloc(sizeof(NODE));
    tree=build();
    printf("后序遍历的结果是:\n");
    houxu(tree);
    printf("\n");
    return 0;
}
/*测试数据
abd##e##cf###
输出:debfca
*/
阅读全文
1 0
原创粉丝点击