前面实现了二叉树,和栈,现在将二者结合,实现二叉树的非递归版本遍历算法
在实现的过程中,主要是 “*,&,->,.” 这几个的区别以及含义:
* :间接访问,操作符访问其操作数所表示的地址 & :取地址,操作符产生变量的地址,它是一个指针常量 -> : 访问结构指针成员,对应于& . :访问结构成员,对应于*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 typedef struct Bin Node{ char data; struct Bin Node *lchild,*rchild; }Bin Node,*Bin Tree; typedef struct SqStack{ Bin Tree *base; Bin Tree *top; int stacksize; }SqStack; int InitStack(SqStack &S); int DestroyStack(SqStack &S); int ClearStack(SqStack &S); int StackEmpty(SqStack &S); int StackLength(SqStack S); int GetTop (SqStack &S,Bin Tree &e) ; int Push(SqStack &S,Bin Tree &e); int Pop(SqStack &S,Bin Tree &e); int StackTraverse(SqStack S); //'#' 表示根节点 void createBin Tree(Bin Tree &T){ char ch; scanf("%c" ,&ch); if (ch == '#' ){ T = NULL; }else { if (!(T = (Bin Node*) malloc(sizeof(Bin Node)))){ exit (0 ); } T->data = ch; printf ("数据%c\n" , T->data); createBin Tree(T->lchild); createBin Tree(T->rchild); } } /*递归实现二叉树的遍历方法*/ void preOrderTraverse(Bin Tree &T){ if (T){ printf ("%c" ,T->data); preOrderTraverse(T->lchild); preOrderTraverse(T->rchild); } } void InOrderTravese(Bin Tree &T){ if (T){ InOrderTravese(T->lchild); printf ("%c" ,T->data); InOrderTravese(T->rchild); } } void BackOrderTraverse(Bin Tree &T){ if (T){ BackOrderTraverse(T->lchild); BackOrderTraverse(T->rchild); printf ("%c" ,T->data); } } /*非递归的实现二叉树的遍历,需要借助于栈结构*/ int InOrderTravese_1(Bin Tree &T){ SqStack S; InitStack(S); Bin Tree p; p = T; while ( p || !StackEmpty(S)){ if (p){ Push(S,p); p = p->lchild; } else { Pop(S,p); printf ("%c" ,p->data); p= p->rchild; } } return 1 ; } /*迭代版本,即非递归的先序遍历 */ int preOrderTraverse_1(Bin Tree &T){ SqStack S; InitStack(S); Bin Tree p = T; while (true ){ while (p){ printf ("%c" ,p->data); Push(S,p->rchild); p = p->lchild; } if (StackEmpty(S)) break ; Pop(S,p); } } int preOrderTraverse_2(Bin Tree &T){ SqStack S; InitStack(S); Bin Tree p= T; while (p || !StackEmpty(S)){ if (p){ printf ("%c" ,p->data); Push(S,p); p= p->lchild; }else { Pop(S,p); p = p->rchild; } } } int main (){ Bin Tree T; createBin Tree(T); printf ("递归版本的先序遍历" ); preOrderTraverse(T); printf ("\n递归版本的中序遍历" ); InOrderTravese(T); printf ("\n递归版本的后序遍历" ); BackOrderTraverse(T); printf ("\n迭代版本的中序遍历" ); InOrderTravese_1(T); printf ("\n迭代版本的先序遍历" ); preOrderTraverse_1(T); printf ("\n" ); preOrderTraverse_2(T); //printf ("\n迭代版本的后序遍历" ); //BackOrderTraverse_1(T); return 0 ; } int InitStack(SqStack &S){ S.base = (Bin Tree *) malloc (STACK_INIT_SIZE * sizeof(Bin Tree)); if (!S.base) exit (-1 ); S.top = S.base; S.stacksize = STACK_INIT_SIZE; return 1 ; } int GetTop(SqStack &S,Bin Tree &e){ if (S.top == S.base) return -1 ; e = *(S.top -1 ); return 1 ; } int Push(SqStack &S,Bin Tree &e){ if (S.top - S.base >= S.stacksize){ S.base = (Bin Tree * ) realloc(S.base,(S.stacksize + STACKINCREMENT)*sizeof(Bin Tree)); if (!S.base) exit (-1 ); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return 1 ; } int Pop(SqStack &S,Bin Tree &e){ if (S.top == S.base) return -1 ; e = *--S.top; return 1 ; } int StackLength(SqStack S){ return S.top - S.base; } int StackEmpty(SqStack &S){ if (S.top == S.base){ return 1 ; } else return 0 ; } int ClearStack(SqStack &S){ S.top = S.base; } int DestroyStack(SqStack &S){ free(S.base); S.base = NULL; S.top = NULL; S.stacksize = 0 ; return 1 ; }