当前位置: 代码迷 >> C语言 >> 树的非递归算法
  详细解决方案

树的非递归算法

热度:224   发布时间:2008-05-15 12:58:27.0
树的非递归算法
/* Note:Your choice is C IDE */
#include "stdio.h"
#include<stdlib.h>
typedef struct bitnode
{
    char ch;
    struct bitnode *lchild,*rchild;
}bitnode,*bittree;
typedef struct stacknode
{
    bitnode *data;
    struct stacknode *next;
}linkstack;
void initstack(linkstack *s)
{ s->next=NULL; }
void push(linkstack *s,bittree x)
{
    linkstack *p;
    p=(linkstack *)malloc(sizeof(linkstack));
    p->data=x;
    p->next=s->next;
    s->next=p;
}
bittree pop(linkstack *s)
{
    bittree x;
    linkstack *p;
    p=s->next;
    if(s->next==0)return 0;
    x=p->data;
    s->next=p->next;
    free(p);
    return x;
}
bittree cre_tree()
{
    bittree root;
    char ch;
    ch=getchar();
    if(ch=='#')root=NULL;
    else
    {
        root=(bittree)malloc(sizeof(bitnode));
        root->ch=ch;
        root->lchild=cre_tree();
        root->rchild=cre_tree();
    }
    return root;
}
void preorder(bittree root)
{
    linkstack s;
    bittree p;
    push(&s,root);
    while(s.next)
    {
        p=pop(&s);
        while(p)
        {
            printf("%c",p->ch);
            if(p->rchild)push(&s,p->rchild);
            p=p->lchild;
        }
    }
}
main()
{
    bittree root;
    root=cre_tree();
    preorder(root);
}
帮我再调调一下,为什么运行后还提示错误。
搜索更多相关的解决方案: linkstack  递归  算法  next  bitnode  

----------------解决方案--------------------------------------------------------
  相关解决方案