教課書上的一段二叉樹的非遞回中序遍歷演算法,感覺寫錯了,最后四行寫成了死循化了, 求大神指教
------------
//利用 堆疊的非遞回中序遍歷演算法遍歷二叉樹
void Inorder1(BinTree bt)
{ //同樣采用二叉鏈表存盤結構,并假定結點值為字符型
SeqStack S; BinTNode *p;
InitStack(&S); Push(&S,bt);
while(!StackEmpty(&S)){
while(GetTop(&S))
Push(&S,GetTop(&S)->lchild); //直到左子樹空為止
p=Pop(&S);
if(!StackEmpty(&S)){
printf("%c",GetTop(&S)->data); //訪問根結點
p=Pop(&S); Push(&S,p->rchild); //右子樹進堆疊
}
}
}
uj5u.com熱心網友回復:
https://blog.csdn.net/weixin_43982698/article/details/104514746uj5u.com熱心網友回復:
void ldr(BiTree T) {
BiTree t = T;
Stack S = createStackNode();
printf("中序遍歷:");
while (t != NULL || isStackEmpty(S) == FALSE) {
if (t != NULL) {
push(S, t);
t = t->lchild;
} else {
t = pop(S);
printf(" %c", t->data);
t = t->rchild;
}
}
printf("\n");
clearStack(S);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/44865.html
標籤:數據結構與算法
上一篇:spyder使用dnn報錯TypeError: readNetFromTensorflow() takes at most 1 argument (2 give
下一篇:MATLAB題想尋求一下幫助!
