第一种:
Tree q[MAXN]; void InOrderTraverse(Tree T){ int top = 0; Tree p = T; while(p || top != 0){ if(p) {q[top++] = p; p = p->lchild;}//根指针进栈,遍历左子树 else{ //根指针退栈,访问根结点,遍历右子树 p = q[--top]; printf("%c", p->data); p = p->rchild; } } }
第二种:
Tree q[MAXN]; void InOrderTraverse(Tree T){ int top = 0; Tree p; q[top++] = T; while(top != 0){ while((p = q[top-1]) != NULL){q[top++] = p->lchild;} //向左走到尽头 top--; //空指针出栈 if(top != 0){ //访问结点,向右一步 p = q[--top]; printf("%c", p->data); q[top++] = p->rchild; } } }