用栈实现二叉树的非递归中序遍历

另见求树高 http://blog.csdn.net/Justme0/article/details/7694704

  1 /********************************************************************
  2 created:    2014/05/11 23:13
  3 filename:    main.c
  4 author:        Justme0 (http://blog.csdn.net/justme0)
  5 
  6 purpose:    用栈实现二叉树的非递归中序遍历
  7 *********************************************************************/
  8 
  9 #define _CRT_SECURE_NO_WARNINGS
 10 
 11 #include <stdio.h>
 12 #include <stdlib.h>
 13 #include <assert.h>
 14 
 15 typedef struct Node Node;
 16 typedef struct Stack Stack;
 17 typedef char TElemType;
 18 typedef Node * SElemType;
 19 
 20 #pragma region 栈的定义
 21 
 22 #define STACK_INIT_SIZE 100
 23 #define STACKINCREMENT 10
 24 
 25 struct Stack {
 26     SElemType *base;
 27     SElemType *top;
 28     int stacksize;
 29 };
 30 
 31 void InitStack(Stack *pS)
 32 {
 33     (*pS).base=(SElemType*)malloc(STACK_INIT_SIZE*(sizeof(SElemType)));
 34     if(!(*pS).base)
 35         exit(-2);
 36     (*pS).top=(*pS).base;
 37     (*pS).stacksize=STACK_INIT_SIZE;
 38 }
 39 
 40 void Push(Stack *pS,SElemType T)
 41 {
 42     if((*pS).top-(*pS).base>=(*pS).stacksize)
 43     {
 44         (*pS).base=(SElemType*)realloc((*pS).base,((*pS).stacksize+STACKINCREMENT)*sizeof(SElemType));
 45         if(!(*pS).base)
 46             exit(-2);
 47         (*pS).top=(*pS).base+(*pS).stacksize;
 48         (*pS).stacksize+=STACKINCREMENT;
 49     }
 50     *(*pS).top++=T;
 51 }
 52 
 53 int StackEmpty(Stack S)
 54 {
 55     return S.top == S.base;
 56 }
 57 
 58 int Pop(Stack (*pS),SElemType *pT)
 59 {
 60     if(StackEmpty(*pS))
 61         return 0;
 62     *pT=*(--(*pS).top);
 63     return 1;
 64 }
 65 
 66 int GetTop(Stack S,SElemType *pT)
 67 {
 68     if(StackEmpty(S))
 69         return 0;
 70     (*pT)=*(S.top-1);
 71     return 1;
 72 }
 73 
 74 #pragma endregion 栈的定义
 75 
 76 int Print(TElemType a)
 77 {
 78     printf("%c
", a);
 79 
 80     return 1;
 81 }
 82 
 83 #pragma region 二叉树
 84 
 85 struct Node {
 86     TElemType data;
 87     Node *lchild;
 88     Node *rchild;
 89 };
 90 
 91 int CreatBiTree(Node **pT)
 92 {//先序法创建二叉树,空格字符表示空树,每个节点一个字符
 93     //构造二叉链表表示的二叉树
 94     TElemType ch;
 95     scanf("%c",&ch);
 96     getchar();
 97     if(ch==' ')
 98     {
 99         *pT = NULL;
100     }
101     else
102     {
103         if(!(*pT=(SElemType)malloc(sizeof(Node))))
104             exit(-2);
105         (*pT)->data=ch;             //生成根节点
106         CreatBiTree(&((*pT)->lchild));     //构造左子树
107         CreatBiTree(&((*pT)->rchild));    //构造右子树
108     }
109 
110     return 1;
111 }
112 
113 //中序遍历二叉树T的非递归算法,对每个数据元素调用函数Visit。
114 int InOrderTraverse(Node *T, int (*Visit)(TElemType e))
115 {
116     Stack S;
117     Node *p = NULL;
118     S.base=(Node **)malloc(100*(sizeof(Node **)));
119     InitStack(&S);
120     Push(&S,T);
121     while(!StackEmpty(S))
122     {
123         while(GetTop(S,&p)&&p)
124             Push(&S,p->lchild);
125         Pop(&S,&p);
126         if(!StackEmpty(S))
127         {
128             Pop(&S,&p);
129             assert(NULL != p);
130             if(!Visit(p->data))
131                 return -1;
132             Push(&S,(p->rchild));
133         }
134     }
135     return 1;
136 }
137 
138 #pragma endregion 二叉树
139 
140 int main(void)
141 {
142     Node *T  = NULL;
143     freopen("cin.txt", "r", stdin);
144 
145     CreatBiTree(&T);
146     InOrderTraverse(T, Print);
147 
148     // TODO: FreeTree(T);
149 
150     system("PAUSE");
151     return 0;
152 }
153 
154 /* cin.txt
155 A
156 B
157 C
158 
159 
160 D
161 E
162 
163 G
164 
165 
166 F
167 
168 
169 
170 */
原文地址:https://www.cnblogs.com/jjtx/p/3722553.html