在按层序遍历二叉树的算法中,需要借助的数据结构是( )。
void LevelOrder(BiTree p) { queue<BiTree>Q;//使用C++ STL库 Q.push(p);//根节点入队 while(!Q.empty())//队列不空循环 { p=Q.front();//取对头 printf("%c",p->data);//左右孩子入队 if(p->lchild!=NULL) { Q.push(p->lchild); } if(p->rchild!=NULL) { Q.push(p->rchild); } Q.pop();//队头元素出队 } printf("\n"); }因此本题选A。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | void traverse(bitree bt) { linkqueue q; bitree p; initqueue(q); //初始化一个空的队列 p=bt; enqueue(q,p); //入队 while(queueempty(q)!=1) { dequeue(q,p); //出队 if(p->lchild!=NULL) enqueue(q,p->lchild); //访问左子树 if(p->rchild!=NULL) enqueue(q,p->rchild); //访问右子树 } printf("\n"); } |