include[HTML_REMOVED]
include[HTML_REMOVED]
using namespace std;
define MaxNodeNum 100
typedef struct bitnode{
char data;
struct bitnode lchild,rchild;
}bitnode,bitree;
void createbitree(bitree& bt,int &n,char pre[]){
char c=pre[n];
if(c==’#’){
bt=NULL;
return;
}
n+=1;
bt=(bitnode )malloc(sizeof(bitnode));
bt->data=c;
createbitree(bt->lchild,n,pre);
n+=1;
createbitree(bt->rchild,n,pre);
}
void levelorder(bitree bt){
bitnode * queue[MaxNodeNum];
int front=0,rear=0;//可插入位置和队头位置
//当可插入位置就是队头的时候,就是队列为空
bitnode *p=bt;
queue[rear]=p;
while(front[HTML_REMOVED]data<<’ ‘;
if(p->lchild!=NULL){
queue[rear]=p->lchild;
}
if(p->rchild!=NULL) queue[rear++]=p->rchild;
}
}
int main(){
bitree p;
char pre[]={‘a’,’b’,’d’,’#’,’#’,’e’,’#’,’g’,’#’,’#’,’c’,’f’,’#’,’#’,’#’};
int m=0;
int& n = m;
createbitree(p,n,pre);
cout<<”层序遍历”<<endl;
levelorder(p);
free(p);
}