#include
#include
#include
#include
#define MAX_TREE_SIZE 100
#define TElemType int
#define QElemType int
#define Status int
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef struct BiTNode//结点结构
{
QElemType data;
int parent;//双亲位置域
}PTNode;
typedef struct{//树结构
PTNode node[MAX_TREE_SIZE];
int n;//根的位置和结点数
char r;
}PTree;
typedef struct{//队列的链式存储
TElemType data;
struct *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
Status InitQueue(LinkQueue Q)//构造一个空队列
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue Q,PTree qq){//入队
QNode *pp;
pp=(QueuePtr)malloc(sizeof(QNode ));
if(!pp) exit(OVERFLOW);
pp->data=qq.r ;
pp->next=NULL;
Q.rear ->next =pp->next;
Q.rear =pp;
return OK;
}
Status DeQueue(LinkQueue Q,PTree qq){//出队
QNode *pp;
if(Q.front ==Q.rear ) return ERROR;
Q.front->next=pp->next ;
qq.r =pp->data;
Q.front->next =pp->next ;
if(Q.rear ==pp) Q.rear =Q.front ;
free(pp);
return OK;
}
Status QueueEmpty(LinkQueue Q){
if(Q.rear ==Q.front ) return OK;
else return ERROR;
}
void ClearTree(PTree *T)//构造空树
{
T->n=0;
}
void CreateTree(PTree T)//构造树
{
LinkQueue Q;
PTree p,qq;
int i,j,l;
// qq=(PTree )malloc(sizeof(PTNode ));
char c[MAX_TREE_SIZE];/ 临时存放孩子结点数组 /
InitQueue(Q);/ 初始化队列 /
printf("请输入根结点(字符型,空格为空):");
scanf("%c%*c",&T->node[0].data);/ 根结点序号为0,%*c吃掉回车符 /
if(!T->node[0].data)/ 非空树 /
{
T->node[0].parent=-1;/ 根结点无双亲 /
qq.r=T->node[0].data;
qq.n=0;
EnQueue(Q,qq);/ 入队此结点 /
while(i<MAX_TREE_SIZE&&QueueEmpty(Q))/ 数组未满且队不空 /
{
DeQueue(Q,qq); / 出队一个结点 /
printf("请按长幼顺序输入结点%c的所有孩子: ",qq.r);
gets(c);
l=strlen(c);
for(j=0;j
{
T->node[i].data=c[j];
T->node[i].parent=qq.n;
p.r=c[j];
p.n=i;
EnQueue(Q,p); / 入队此结点 /
i++;
}
}
if(i>MAX_TREE_SIZE)
{
printf("结点数超过数组容量\n");
exit(OVERFLOW);
}
T->n=i;
}
else
T->n=0;
}
//===== 判断树是否为空 =====
//
//Status TreeEmpty(PTree *T)
//{ / 初始条件:树T存在。操作结果:若T为空树,则返回TRUE,否则返回FALSE */
// return T->n==0;
//}
void main(){
PTree *T1;
T1=(PTree *)malloc(sizeof(PTNode ));
ClearTree(T1);
CreateTree(T1);
}
只写了这么一点,运行起有错,,求破,,