xmkk

导航

 
 1 #include"BiThrTree.h"
 2 #include"LinkQueue.h"
 3 
 4 
 5 /*算法:第一步:定义一个存储树结点的队列;第二步:将树的根结点压人队列;
 6 第三步:循环输出队列首结点的存储的树结点的值,直到队列为空,并将输出的结点删除,同时将其左右孩子结点入队列*/
 7 
 8 Status Layer_Traverse(BiThrTree n)
 9 {
10     //BiThrTree n;//定义一个树的头节点
11     LinkQueue *q;//队列q
12     QElemType *c;
13    // n=p;
14 
15 
16     if(!InitQueue(q))//如果队列初始化失败,则返回错误
17         return ERROR;
18     if(n!=NULL)//树非空时,树节点入队列
19         EnQueue(q,*n);
20     else return ERROR;
21     c=&(q->front->data);
22     while(q->front!=q->rear)//当队列非空时,执行循环
23     {
24 
25         DeQueue(q,c);//获得队列中第一个结点的值,并删除该结点
26         printf("%c ",c->data);//输出刚刚获得的值
27         if(c->lchild!=NULL) EnQueue(q,*(c->lchild));//将刚刚删除结点的左右孩子入队列
28         if(c->rchild!=NULL) EnQueue(q,*(c->rchild));
29     }
30     return OK;
31 
32 }
33 
34 
35 int main()
36 {
37     BiThrTree T;
38     printf("请按前序输入二叉树(如:'ABDH##I##EJ###CF##G##')\n");
39     CreateBiThrTree(&T); /* 按前序产生二叉树  */
40 
41 
42     printf("按层遍历:");
43     Layer_Traverse(T);
44 
45 
46 
47     return 0;
48 }
 1 #include "string.h"
 2 #include "stdio.h"
 3 #include "stdlib.h"
 4 #include "io.h"
 5 #include "math.h"
 6 #include "time.h"
 7 
 8 #define OK 1
 9 #define ERROR 0
10 #define TRUE 1
11 #define FALSE 0
12 
13 #define MAXSIZE 100 /* 存储空间初始分配量  */
14 
15 typedef int Status;  /* Status是函数的类型,其值是函数结果状态代码,如OK等  */
16 typedef char TElemType;
17 typedef enum {Link,Thread} PointerTag;  /* Link==0表示指向左右孩子指针, */
18                     /* Thread==1表示指向前驱或后继的线索  */
19 typedef    struct BiThrNode  /* 二叉线索存储结点结构  */
20 {
21   TElemType data;  /* 结点数据  */
22   struct BiThrNode *lchild, *rchild;  /* 左右孩子指针  */
23   PointerTag LTag;
24   PointerTag RTag;    /* 左右标志  */
25 } BiThrNode, *BiThrTree;
26 
27 TElemType Nil='#'; /* 字符型以空格符为空  */
28 
29 
30 
31 /* 按前序输入二叉线索树中结点的值,构造二叉线索树T */
32 /* 0(整型)/空格(字符型)表示空结点  */
33 Status CreateBiThrTree(BiThrTree *T)
34 {
35   TElemType h;
36   scanf("%c",&h);
37 
38   if(h==Nil)
39     *T=NULL;
40   else
41   {
42     *T=(BiThrTree)malloc(sizeof(BiThrNode));
43     if(!*T)
44       exit(OVERFLOW);
45     (*T)->data=h; /* 生成根结点(前序) */
46     CreateBiThrTree(&(*T)->lchild); /* 递归构造左子树  */
47     if((*T)->lchild) /* 有左孩子  */
48       (*T)->LTag=Link;
49     CreateBiThrTree(&(*T)->rchild); /* 递归构造右子树  */
50     if((*T)->rchild) /* 有右孩子  */
51       (*T)->RTag=Link;
52   }
53   return OK;
54 }
 1 //typedef int Status;
 2 
 3 typedef BiThrNode QElemType; /* QElemType类型根据实际情况而定,这里假设为int */
 4 
 5 typedef struct QNode  /* 结点结构  */
 6 {
 7    QElemType data;
 8    struct QNode *next;
 9 }QNode,*QueuePtr;
10 
11 typedef struct     /* 队列的链表结构  */
12 {
13    QueuePtr front,rear; /* 队头、队尾指针  */
14 }LinkQueue;
15 
16 
17 
18 /* 构造一个空队列Q */
19 Status InitQueue(LinkQueue *Q)
20 {
21   Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
22   if(!Q->front)
23     exit(OVERFLOW);
24   Q->front->next=NULL;
25   return OK;
26 }
27 
28 
29 /* 若Q为空队列,则返回TRUE,否则返回FALSE */
30 Status QueueEmpty(LinkQueue Q)
31 {
32   if(Q.front==Q.rear)
33     return TRUE;
34   else
35     return FALSE;
36 }
37 
38 
39 
40 /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
41 Status GetHead(LinkQueue Q,QElemType *e)
42 {
43   QNode *p;
44   if(Q.front==Q.rear)
45     return ERROR;
46 
47   printf("break6");
48 
49   p=Q.front->next;
50 
51   printf("break8");
52 
53   e=&(p->data);
54 
55   printf("break9");
56 
57   return OK;
58 }
59 
60 
61 /* 插入元素e为Q的新的队尾元素  */
62 Status EnQueue(LinkQueue *Q,QElemType e)
63 {
64   QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
65   if(!s) /*  存储分配失败  */
66     exit(OVERFLOW);
67   s->data=e;
68   s->next=NULL;
69   Q->rear->next=s;  /* 把拥有元素e的新结点s赋值给原队尾结点的后继,见图中①  */
70   Q->rear=s;    /* 把当前的s设置为队尾结点,rear指向s,见图中②  */
71   return OK;
72 }
73 
74 /* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
75 Status DeQueue(LinkQueue *Q,QElemType *e)
76 {
77   QueuePtr p;
78   if(Q->front==Q->rear)
79     return ERROR;
80   p=Q->front->next;    /* 将欲删除的队头结点暂存给p,见图中①  */
81   *e=p->data;        /* 将欲删除的队头结点的值赋值给e */
82   Q->front->next=p->next;/* 将原队头结点的后继p->next 赋值给头结点后继,见图中②
83 */
84   if(Q->rear==p)   /* 若队头就是队尾,则删除后将rear指向头结点,见图中③  */
85     Q->rear=Q->front;
86   free(p);
87   return OK;
88 }

上面三个文件依次是Layer_Traverse.c,BiThrTree.h和LinkQueue.h

算法思路很简单,代码中有说明,之前,程序编译链接都能通过,但是运行的时候,无法输出预想的结果并且报错、终止。调试了一上午,发现是Layer_Traverse函数中定义的变量c没有初始化造成的,于是加了第21行(Layer_Traverse.c中),运行可输出预想结果。

但是还有个问题,Layer_Traverse函数中变量q在编译时也有警告:没有初始化。不知道怎样解决,希望哪位明白的话,可以告诉我,谢谢!

posted on 2013-03-26 22:20  xmkk  阅读(1881)  评论(0编辑  收藏  举报