數據結構——鏈隊列實現二叉樹的層次遍歷


在二叉樹的遍歷這篇博客中https://www.cnblogs.com/wkfvawl/p/9901462.html

對於二叉樹的層次遍歷我只是給出了基於C++ STL的代碼,這里我使用數據結構的鏈表,構建一個鏈隊列來實現。這也算是我第一次使用鏈隊列來完成某個任務,鏈隊列代碼還是來自課本,因為之前使用C++ STL時,queue中的某些函數是有返回值的列如Q.front(),而有些卻沒有返回值像Q.push(p),Q.pop(),就連有沒有參數也是不同的,在這里我沒有改動課本上的代碼來適應自己的習慣,還是按照課本上來所有函數都是有返回值的,對於想要返回值的函數直接使用地址傳遞。

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #define TRUE 1
  4 #define FALSE 0
  5 #define MAX 20
  6 typedef struct BTNode        /*節點結構聲明*/
  7 {
  8     char data ;               /*節點數據*/
  9     struct BTNode *lchild;
 10     struct BTNode *rchild ;  /*指針*/
 11 }*BiTree;
 12 typedef struct Node///隊列中結點結構
 13 {
 14     BiTree data;///數據域(二叉樹結構體)
 15     struct Node *next;///指針域
 16 } LinkQNode;
 17 typedef struct///隊列結構
 18 {
 19     LinkQNode *front;///隊頭指針
 20     LinkQNode *rear;///隊尾指針
 21 } LinkQueue;
 22 void InitLinkQueue(LinkQueue *Q)///初始化列隊列
 23 {
 24     Q->front=(LinkQNode *)malloc(sizeof(LinkQNode));
 25     Q->rear=Q->front;///隊頭指針和隊尾指針都指向頭結點
 26     Q->front->next=NULL;
 27 }
 28 int IsLQEmpty(LinkQueue *Q)///判斷隊列是否為空
 29 {
 30     if(Q->front==Q->rear)
 31     {
 32         return TRUE;
 33     }
 34     else
 35     {
 36         return FALSE;
 37     }
 38 }
 39 int EnLinkQueue(LinkQueue *Q,BiTree x)
 40 {
 41     LinkQNode *NewNode;
 42     NewNode=(LinkQNode *)malloc(sizeof(LinkQNode));///開辟新結點
 43     if(NewNode!=NULL)
 44     {
 45         NewNode->data=x;
 46         NewNode->next=NULL;
 47         Q->rear->next=NewNode;///在隊尾插入結點
 48         Q->rear=NewNode;///修改隊尾指針
 49         return TRUE;
 50     }
 51     else///溢出
 52     {
 53         return FALSE;
 54     }
 55 }
 56 int DeLinkQueue(LinkQueue *Q,BiTree *x)///刪除對頭指針,並用x返回刪除的值
 57 {
 58     LinkQNode *p;
 59     if(Q->front==Q->rear)
 60     {
 61         return FALSE;
 62     }
 63     p=Q->front->next;///p指向對頭元素
 64     Q->front->next=p->next;///對頭元素p出隊
 65     if(Q->rear==p)///如果隊列中只有一個元素p,則p出隊后變成空隊
 66     {
 67         Q->rear=Q->front;
 68     }
 69     *x=p->data;
 70     free(p);
 71     return TRUE;
 72 }
 73 int GetLQHead(LinkQueue *Q,BiTree *x)///獲取隊頭元素,用x返回其值
 74 {
 75     LinkQNode *p;///中間變量
 76     if(Q->front==Q->rear)
 77     {
 78         return FALSE;
 79     }
 80     p=Q->front->next;
 81     *x=p->data;
 82     free(p);
 83     return 1;
 84 }
 85 BiTree createBiTree(BiTree t)  /* 先序遍歷創建二叉樹*/
 86 {
 87     char s;
 88     printf("\nplease input data:(exit for #)");
 89     s=getchar();
 90     if(s=='#')
 91     {
 92         t=NULL;
 93         return t;
 94     }
 95     t=(BiTree)malloc(sizeof(struct BTNode));
 96     if(t==NULL)
 97     {
 98         printf("Memory alloc failure!");
 99         exit(0);
100     }
101     t->data=s;
102     t->lchild=createBiTree(t->lchild); /*遞歸建立左子樹*/
103     t->rchild=createBiTree(t->rchild); /*遞歸建立右子樹*/
104     return t;
105 }
106 
107 void LevelOrder(BiTree p)
108 {
109     LinkQueue *Q;
110     Q=(LinkQueue *)malloc(sizeof(LinkQueue));
111     InitLinkQueue(Q);
112     EnLinkQueue(Q,p);
113     while(!IsLQEmpty(Q))
114     {
115         DeLinkQueue(Q,&p);///出隊列並取隊頭元素
116         printf("%c",p->data);//左右孩子入隊
117         if(p->lchild!=NULL)
118         {
119             EnLinkQueue(Q,p->lchild);
120         }
121         if(p->rchild!=NULL)
122         {
123             EnLinkQueue(Q,p->rchild);
124         }
125     }
126     printf("\n");
127 }
128 int main()
129 {
130     BiTree t=NULL;
131     t=createBiTree(t);
132     printf("\n\n層次遍歷序列:");
133     LevelOrder(t);
134     release(t);
135     return 0;
136 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM