二叉树的层序遍历(c++)

使用层序建树进行建树

二叉树的层序建树

层序遍历代码思路

借助一个辅助队列。

  • 先将二叉树的根结点入队,
  • 然后出队,访问出队结点,
    • 若它有左子树,则将左子树根节点入队;
    • 若它有右子树,则将右子树根节点入队。
    • 然后出队,
    • 访问出队结点……如此反复,直到队列为空
// biTree——树的结构体指针,seQueue——辅助队列的结构体指针
// qElemType,辅助队列中存储的元素类型,这里为树结点的指针
void levelOrder(biTree t) {
    seQueue seq;
    qElemType e;
    initQueue(seq);
    // 根节点入队
    enQueue(seq, t);

	// 辅助队列为空时结束循环
    while (seq->front != seq->rear) {
        // 结点出队
        deQueue(seq, e);
        // 访问结点
        cout << e->data << ' ';
        // 若有左子树,左子树根节点入队
        if (e->lChild != NULL) enQueue(seq, e->lChild);
        // 若有右子树,右子树根节点入队
        if (e->rChild != NULL) enQueue(seq, e->rChild);
    }
    cout << endl;
}

实现过程

根据上面的代码思路,需要实现两个数据结构,一个二叉树,一个队列,需要实现的功能有

  • 二叉树、队列的数据结构定义
  • 二叉树的创建、队列的初始化
  • 二叉树的层序遍历、队列的出队、入队操作

实现结果&完整代码

在这里插入图片描述

/**
 * 树的层序建树和层序遍历
 */
#include <iostream>

using namespace std;

const int MAXSIZE = 10;

typedef char tElemType;
typedef struct biTNode {
    tElemType data;
    struct biTNode *lChild, *rChild;
} biTNode, *biTree;

typedef biTree qElemType;
typedef struct {
    qElemType data[MAXSIZE];
    int front, rear;
} seQNode, *seQueue;

void initQueue(seQueue &q) {
    q = (seQueue) calloc(1, sizeof(seQNode));
}

void enQueue(seQueue q, qElemType e) {
    if ((q->rear + 1) % MAXSIZE != q->front) {
        q->data[q->rear] = e;
        q->rear = (q->rear + 1) % MAXSIZE;
    }
}

void deQueue(seQueue q) {
    if (q->rear == q->front) return;
    q->front = (q->front + 1) % MAXSIZE;
}

void deQueue(seQueue q, qElemType &e) {
    if (q->rear == q->front) return;

    e = q->data[q->front];
    q->front = (q->front + 1) % MAXSIZE;
}

void useLevelInitTree(biTree &t) {
    t = NULL;
    biTNode *newBTNode, *pCur;
    tElemType e;

    seQueue seq;
    initQueue(seq);
    while (scanf("%c", &e)) {
        if (e == '\n') break;

        newBTNode = (biTNode *) calloc(1, sizeof(biTNode));
        newBTNode->data = e;
        if (t == NULL) {
            t = newBTNode;
            enQueue(seq, newBTNode);
            continue;
        } else {
            enQueue(seq, newBTNode);
        }

        pCur = seq->data[seq->front];
        if (pCur->lChild == NULL) {
            pCur->lChild = newBTNode;
        } else if (pCur->rChild == NULL) {
            pCur->rChild = newBTNode;
            deQueue(seq);
        }
    }
}

void levelOrder(biTree t) {
    seQueue seq;
    qElemType e;
    initQueue(seq);
    enQueue(seq, t);

    while (seq->front != seq->rear) {
        deQueue(seq, e);
        cout << e->data << ' ';
        if (e->lChild != NULL) enQueue(seq, e->lChild);
        if (e->rChild != NULL) enQueue(seq, e->rChild);
    }
    cout << endl;
}

void inOrder(biTree t) {
    if (t != NULL) {
        inOrder(t->lChild);
        cout << t->data << ' ';
        inOrder(t->rChild);
    }
}

int main()
{
    biTree tree;
    useLevelInitTree(tree);

    cout << "---------------------------" << endl;
    cout << "inOrder: ";
    inOrder(tree);
    cout << endl << "leverOrder: ";
    levelOrder(tree);

    return 0;
}

王道数据结构

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值