先序、中序 遍歷非遞歸算法
1.先序遍歷非遞歸算法
- void PreOrderUnrec(Bitree *t)
- {
- Stack s;
- StackInit(s);
- Bitree *p=t;
- while (p!=NULL || !StackEmpty(s))
- {
- while (p!=NULL) //遍歷左子樹(shù)
- {
- visite(p->data);
- push(s,p);
- p=p->lchild;
- }
- if (!StackEmpty(s)) //通過(guò)下一次循環(huán)中的內(nèi)嵌while實(shí)現(xiàn)右子樹(shù)遍歷
- {
- p=pop(s);
- p=p->rchild;
- }//endif
- }//endwhile
- }
2.中序遍歷非遞歸算法
- void InOrderUnrec(Bitree *t)
- {
- Stack s;
- StackInit(s);
- Bitree *p=t;
- while (p!=NULL || !StackEmpty(s))
- {
- while (p!=NULL) //遍歷左子樹(shù)
- {
- push(s,p);
- p=p->lchild;
- }
- if (!StackEmpty(s))
- {
- p=pop(s);
- visite(p->data); //訪問(wèn)根結(jié)點(diǎn)
- p=p->rchild; //通過(guò)下一次循環(huán)實(shí)現(xiàn)右子樹(shù)遍歷
- }//endif
- }//endwhile
- }
posted on 2013-11-20 11:02 ** 閱讀(164) 評(píng)論(0) 編輯 收藏