zoukankan      html  css  js  c++  java
  • 数据结构——链队列实现二叉树的层次遍历

    在二叉树的遍历这篇博客中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("
    please 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("
    ");
    127 }
    128 int main()
    129 {
    130     BiTree t=NULL;
    131     t=createBiTree(t);
    132     printf("
    
    层次遍历序列:");
    133     LevelOrder(t);
    134     release(t);
    135     return 0;
    136 }
  • 相关阅读:
    适配器
    装饰器
    getOwnPropertyDescriptor
    发布订阅
    策略模式
    window.requestAnimationFrame() 和 window.cancelAnimationFrame()
    L1-056 猜数字
    L1-055 谁是赢家
    L1-054 福到了
    L1-053 电子汪
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9975649.html
Copyright © 2011-2022 走看看