先序中序非递归 图的深度遍历

来源:互联网 发布:c语言中volatile byte 编辑:程序博客网 时间:2024/04/30 02:19
/*   先序  */inorder(bitree root){initstack(s);p=root;s.push(p);while(!isempty(s)){visit(s.top);s.pop(&p);if(p-r) s.push(p-r);if(p-l) s.push(p-l);}}


 

/*   中序   */inorder(bitree root){initstack(s);p=root;while(p!=NULL!isempty(s)){if(p!=NULL){s.push(p);p=p->l;}else{s.pop(&p)visit(p);p=p->r;}}}
int visit[NUM]={0};void TraverGraph(Graph g){for(int i=0;i<NUM;i++)if(!visit[i])  DepthVisit(g,i);}DepthVisit(Graph g,int i){visitadj(i);visit[i]=1;int w=fistAdj(g,i);while(w!=-1){if(!visit[w]) DepthVisit(g,w);w=nextadj(g,i,w);}}



 

原创粉丝点击