二叉树层次遍历
#include<iostream.h>#include<stdlib.h>
typedef char ElemType;
typedef struct node{
ElemType data;
struct BinaryTree *lch, *rch;
};
void level(NODE *root)
{
NODE *p,*node[MAX];
p=root;
int front=0,rear=0;
if(p!=NULL){
rear=(rear+1)%MaxLength ; //队尾指针后移
node[rear]=p;
}
while(front!=rear)
{
front=(front+1)%Maxlength;
p=node[front];
printf("%d",p->data);
if(p->lch!=NULL)
{ rear=rear+1)%MaxLength;
node[rear]=p->lch;
}
if(p->rch!=NULL)
{
rear=(rear 1)%MaxLength;
node[rear]=p->rch;
}
}
}