#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -1 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int Status; typedef char ElemType; typedef struct BTNode{//定义树节点结构体 ElemType data; struct BTNode *l; struct BTNode *r; }BTNode,*BinTree; typedef BinTree SElemType; typedef struct{//非递归遍历要使用的栈操作定义 SElemType *base; SElemType *top; int stacksize;//当前的栈空间容量 }SqStack; //定义二叉树的基本操作 BinTree CreateBinTree(BinTree T);//创建二叉树并且返回一个指针 Status Visit(ElemType e); Status Depth(BinTree T); Status PreOrderRecursionTraverse(BinTree T,Status(*Visit)(ElemType e)); Status LevelOrderRecursionTraverse(BinTree T,Status(*Visit)(ElemType e)); //定义栈的基本操作 Status InitStack(SqStack *S); Status Destroy(SqStack *S); Status StackEmpty(SqStack S); Status GetTop(SqStack *s,SElemType *e); Status Pop(SqStack *S,SElemType *e); Status Push(SqStack *S,SElemType e); Status StackTraverse(const SqStack *S);//常量? Status PreOrderNoneRecursionTraverse(BinTree T,Status(*Visit)(ElemType e));//非递归先序遍历 int main(){ //int depth; BinTree Tree=NULL; Status(*Visit)(ElemType e)=Visit;//使用函数指针,用指针调用函数 printf("请按先序遍历输入二叉树元素(空节点为=): "); Tree=CreateBinTree(Tree); printf(" 先序遍历: "); PreOrderRecursionTraverse(Tree,Visit); printf(" 层序遍历: "); LevelOrderRecursionTraverse(Tree,Visit); } BinTree CreateBinTree(BinTree T){ char ch; scanf("%c",&ch); if(ch=='='){ T=NULL; }else{ if(!(T=(BTNode *)malloc(sizeof(BTNode)))){ exit(OVERFLOW); } T->data=ch; T->l=CreateBinTree(T->l); T->r=CreateBinTree(T->r); } return T;//返回的T为最后一个结点 } Status Visit(ElemType e){ if(e=='