树的遍历系统 C语言 数据结构 C++

时间:2014-01-26 14:22:16
【文件属性】:

文件名称:树的遍历系统 C语言 数据结构 C++

文件大小:1KB

文件格式:RAR

更新时间:2014-01-26 14:22:16

树的遍历系统

树的遍历系统 C语言 数据结构 C++ void preorder(Bitree *bt) //先序遍历二叉树 { if(bt) { printf("%c\t",bt->data); preorder(bt->lchild); preorder(bt->rchild); } } void inorder(Bitree *bt) //中序遍历二叉树 { if(bt) { inorder(bt->lchild); printf("%c\t",bt->data); inorder(bt->rchild); } } void postorder(Bitree *bt)//后序遍历二叉树 { if(bt) { postorder(bt->lchild); postorder(bt->rchild); printf("%c\t",bt->data); } } int initqueue(linkq *q){ q->front=q->rear=(queueptr)malloc(sizeof(qnode)); if(!q->front) return 0; q->front->next=NULL; return 1; } int EnQueue(linkq *q,Bitree *e){ qnode *p; p=(queueptr )malloc(sizeof(qnode)); if(!p) return 0; p->data=e; p->next=NULL; q->rear->next=p; q->rear=p; return 1; }//入队 Bitree* DeQueue(linkq *q,Bitree *e){ qnode *p; p=(queueptr )malloc(sizeof(qnode)); p=q->front->next; e=p->data; q->front->next=p->next; if(q->rear==p) q->rear=q->front; free(p); return(e); }//出队 int QueueEmpty(linkq *q){ if(q->front==q->rear) return 0; else return 1; } void LayerOrder(Bitree *T) //层次遍历二叉树 { linkq Q; Bitree *p=0; initqueue(&Q); //建队(初始化队列) EnQueue(&Q,T); //结点T入队 while(QueueEmpty(&Q)) { p=DeQueue(&Q,p); //队首结点出队(送入p) printf("%c\t",p->data); if(p->lchild) EnQueue(&Q,p->lchild); //p的左孩子入队 if(p->rchild) EnQueue(&Q,p->rchild); //p的右孩子入队 }


【文件预览】:
tree.c

网友评论