层序遍历二叉树

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 128
#define STR_SIZE 1024

typedef struct Node {    // 定义二叉链
    char         data;   // 数据元素
    struct Node* lchild; // 指向左孩子节点
    struct Node* rchild; // 指向右孩子节点
} BTNode;                // struct Node 的别名

typedef struct Quene {      // 定义顺序队
    int     front;          // 队头指针
    int     rear;           // 队尾指针
    BTNode* data[MAX_SIZE]; // 存放队中元素
} SqQueue;                  // struct Queue 的别名

/**
 * 队列函数
 */
void initQueue(SqQueue** q);             // 初始化队列
bool emptyQueue(SqQueue* q);             // 判断队列空
bool enQueue(SqQueue* q, BTNode* node);  // 入队
bool deQueue(SqQueue* q, BTNode** node); // 出队

/**
 * 二叉树函数
 */
// void createBTNode2(BTNode** BT);                  // 创建二叉树
int  createBTNode(BTNode** BT, char* str, int n); // 创建二叉树
void preOrder(BTNode* BT);                        // 前序遍历
void inOrder(BTNode* BT);                         // 中序遍历
void postOrder(BTNode* BT);                       // 后序遍历
void levelOrder(BTNode* BT);                      // 层次遍历


int main() {
    // 例子:ABDH###E##CF##G##
    BTNode* BT;
    printf("请输入字符串:");
    char* str = (char*)malloc(sizeof(char) * STR_SIZE);
    scanf("%s", str);
    if (strlen(str) == createBTNode(&BT, str, 0)) {
        printf("二叉树建立成功\n");
    }
    // printf("请输入字符串:");
    // createBTNode2(&BT);
    // draw(BT);

    printf("\n先序遍历结果:");
    preOrder(BT);

    printf("\n中序遍历结果:");
    inOrder(BT);

    printf("\n后序遍历结果:");
    postOrder(BT);

    printf("\n层序遍历结果:");
    levelOrder(BT);

    return 0;
}

// 初始化队列
void initQueue(SqQueue** q) {
    if (!((*q) = (SqQueue*)malloc(sizeof(SqQueue)))) {
        printf("内存分配失败!");
        exit(-1);
    }
    (*q)->front = (*q)->rear = -1; // 置 -1
}

// 判断队列是否为空
bool emptyQueue(SqQueue* q) {
    // 首指针和尾指针相等,说明为空。空-返回真,不空-返回假
    if (q->front == q->rear) {
        return true;
    }
    return false;
}

// 进队列
bool enQueue(SqQueue* q, BTNode* node) {
    // 判断队列是否满了。满(插入失败)-返回假,不满(插入成功)-返回真
    if (q->rear == MAX_SIZE - 1) {
        return false;
    }
    q->rear++;               // 头指针加 1
    q->data[q->rear] = node; // 传值
    return true;
}

// 出队列
bool deQueue(SqQueue* q, BTNode** node) {
    // 判断是否空了。空(取出失败)-返回假,不空(取出成功)-返回真
    if (q->front == q->rear) {
        return false;
    }
    q->front++;                // 尾指针加 1
    *node = q->data[q->front]; // 取值
    return true;
}

// 创建二叉树
int createBTNode(BTNode** BT, char* str, int n) {
    char ch = str[n++];  // 把第 n 个字符赋给ch,方便后面判断,字符下标后移
    if (ch != '\0') {    // 如果 ch 不等于结束符就继续创建,否则就结束
        if (ch == '#') { // 以 # 号代表 NULL,下面没有了
            *BT = NULL;
        } else {
            if (!(*BT = (BTNode*)malloc(sizeof(BTNode)))) {
                printf("内存分配失败!");
                exit(-1);
            } else {
                (*BT)->data = ch;
                n           = createBTNode(&((*BT)->lchild), str, n); // 左递归创建
                n           = createBTNode(&((*BT)->rchild), str, n); // 右递归创建
            }
        }
    }
    // 返回 n,记录字符串使用到哪里了
    return n;
}
// 先序遍历
void preOrder(BTNode* BT) {
    if (BT != NULL) {           // 判断不为空
        printf("%c", BT->data); // 访问根节点
        preOrder(BT->lchild);   // 递归,先序遍历左子树
        preOrder(BT->rchild);   // 递归,先序遍历右子树
    }
}

// 中序遍历
void inOrder(BTNode* BT) {
    if (BT != NULL) {
        inOrder(BT->lchild);
        printf("%c", BT->data);
        inOrder(BT->rchild);
    }
}

// 后序遍历
void postOrder(BTNode* BT) {
    if (BT != NULL) {
        postOrder(BT->lchild);
        postOrder(BT->rchild);
        printf("%c", BT->data);
    }
}

void levelOrder(BTNode* BT) {
    SqQueue* q;       // 定义队列
    initQueue(&q);    // 初始化队列
    if (BT != NULL) { // 根节点指针进队列
        enQueue(q, BT);
    }
    // 一层一层的把节点存入队列,当没有孩子节点时就不再循环
    while (!emptyQueue(q)) {      // 队不为空循环
        deQueue(q, &BT);          // 出队时的节点
        printf("%c", BT->data);   // 输出节点存储的值
        if (BT->lchild != NULL) { // 有左孩子时将该节点进队列
            enQueue(q, BT->lchild);
        }
        if (BT->rchild != NULL) { // 有右孩子时将该节点进队列
            enQueue(q, BT->rchild);
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值