数据结构中用c语言建立二叉树的程序

数据结构中用c语言建立二叉树的程序

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

typedef struct Bnode //二叉树节点类型
{
int m;
struct Bnode *Lchild,*Rchild;
}Btnode, *BTptr;

typedef struct Dnode //队列节点类型
{
Btnode *pr;
struct Dnode *next;
}Qnode,*Qlink;

typedef struct //q节点类型
{
Qnode *front,*rear;
}linkqueue;

void Lcreatqueue(linkqueue *q) //创建队列
{
q->front=(Qlink)malloc(sizeof(Qnode));
q->front->next=NULL;
q->rear=q->front;
}

Btnode *Getqtop(linkqueue *Q)
{
return(Q->front->pr);
}

void Enqueue(linkqueue *q,Btnode *e) //入队
{
Qlink p;
p=(Qlink)malloc(sizeof(Qnode));
p->pr=e;
p->next=NULL;
if(q->front==NULL)q->front=p;
else
q->rear->next=p;
q->rear=p;
}

Btnode *DeQueue(linkqueue *q) //出队
{ Qlink p;
if(q->front==q->rear) return(NULL);
else
{
p=q->front;
q->front=p->next;
free(p);
return(q->front->pr);
}
}

BTptr creatbtree(BTptr BT)
{
int i=1;
linkqueue *Q=NULL;
BTptr q;
//BTptr s;
Btnode *p,*Del;
Q=(linkqueue *)malloc(sizeof(linkqueue));
Lcreatqueue(Q);
Q->rear=Q->front=NULL;
BT=NULL;
while(i<=n)
{p=NULL;
p=(BTptr)malloc(sizeof(Btnode));
p->Lchild=p->Rchild=NULL;
p->m=i;
Enqueue(Q,p);
if(i==1)BT=p;
else
{
q=Getqtop(Q); //q指向二叉树的指针
if(p&&q)
if(i%2==0)q->Lchild=p;
else q->Rchild=p;
if(i%2==1) Del=DeQueue(Q);

}
i++;
}

return(BT);
}
main()
{BTptr *p=NULL;
p=creatbtree(p);
}

还是希望你自己会去编一遍
温馨提示:答案为网友推荐,仅供参考