6_37_二叉树的先序遍历非递归算法

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{
    int data;
    struct node*lchild,*rchild;
}tnode,*tree;
tree creat()
{
    int x;
    tree t;
    scanf("%d",&x);
    if(x==0)t=NULL;
    else
    {
        t=(tnode*)malloc(sizeof(tnode));
        t->data=x;
        t->lchild=creat();
        t->rchild=creat();
    }
    return t;
}
void preorder(tree t)
{
    tree stack[200];
    int top=0;
    while(t||top){
        if(t){
            printf("%d\n",t->data);
            stack[top++]=t;
            t=t->lchild;
        }
        else t=stack[--top]->rchild;
    }
}
int main()
{
    tree t=creat();
    preorder(t);
}

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

posted @ 2015-04-25 16:10  Thereisnospon  阅读(137)  评论(0编辑  收藏  举报