#include <stdio.h>
#include <stdlib.h>
#define M 100
//前序遍历:ABD#E##FG###C##
typedef struct tree/*二叉树*/
{ char data;
struct tree *lchild,*rchild;
}bintree;
typedef bintree *tree;
tree createTree()/*前序遍历建树*/
{
tree t;
char c=getchar();
if(c=='#') return NULL;
else
{ t=(tree)malloc(sizeof(bintree));
t->data=c;
t->lchild=createTree();
t->rchild=createTree();
}
return t;
}
/*非递归中序遍历:思路:边遍历边输出,先输出左子树的最左边一个节点,并放入栈,*/
void intree(tree t)
{ tree stack[M];int top=0;/*定义、初始化栈*/
while(top||t)
{ if(t!=NULL)
{ stack[top++]=t;
#include <stdlib.h>
#define M 100
//前序遍历:ABD#E##FG###C##
typedef struct tree/*二叉树*/
{ char data;
struct tree *lchild,*rchild;
}bintree;
typedef bintree *tree;
tree createTree()/*前序遍历建树*/
{
tree t;
char c=getchar();
if(c=='#') return NULL;
else
{ t=(tree)malloc(sizeof(bintree));
t->data=c;
t->lchild=createTree();
t->rchild=createTree();
}
return t;
}
/*非递归中序遍历:思路:边遍历边输出,先输出左子树的最左边一个节点,并放入栈,*/
void intree(tree t)
{ tree stack[M];int top=0;/*定义、初始化栈*/
while(top||t)
{ if(t!=NULL)
{ stack[top++]=t;