二叉数中序遍历的非递归实现(使用栈)

代码
   
     
1 #include " BiTree_List.h "
2 #include " Stack_Sequence.h "
3 #include < cstdio >
4
5   void myInOrder(BiTree T,SqStack S)
6 {
7 initStack(S);
8 if ( ! T)
9 {
10 printf( " 栈为空!\n " );
11 } else {
12 push(S,T);
13 while ( ! isEmpty(S)){
14 while (T = get (S)) push(S,T -> lchild);
15 T = pop(S);
16 if ( ! isEmpty(S))
17 {
18 T = pop(S);
19 outputBiTNode(T);
20 push(S,T -> rchild);
21 }
22 }
23 }
24 printf( " \n " );
25 freeStack(S);
26 }
27 int main(){
28 BiTree T;
29 SqStack S;
30 createBiTree(T);
31 printf( " 非递归中序遍历为\n " );
32 myInOrder(T,S);
33 freeBiTree(T);
34 return 0 ;
35 }

你可能感兴趣的:(非递归)