22计算机408考研—数据结构—二叉树的层序遍历

343 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第25天,点击查看活动详情

手把手教学考研大纲范围内树定义,遍历,Huffman,并查集 22考研大纲数据结构要求的是C/C++,笔者以前使用的都是Java,对于C++还很欠缺, 如有什么建议或者不足欢迎大佬评论区或者私信指出 初心是用最简单的语言描述数据结构

Talk is cheap. Show me the code. 理论到处都有,代码加例题自己练习才能真的学会

二叉树的层序遍历(非递归,类似BFS)

简单来说:给二叉树分层,一层一层的输出,输出完本层就从下一层的左边开始输出

在这里插入图片描述

把结点存到队列,循环队列
循环到此结点的时候,输出此结点的值,然后把此结点的左右子结点添加到队列

层序遍历的过程: 在这里插入图片描述

层序遍历的结果: 在这里插入图片描述

层序遍历为什么要用BFS,(BFS与DFS的区别)

在这里插入图片描述

层序遍历代码:

  //层序遍历(非递归)
void treeFloorPrint(Tree tree) {
    cout << "层序遍历:";
    queue<Tree> q;  //使用队列存结点,同一层的在一块挨着,循环同一层的时候,末尾添加的是下一层的左右子结点
    q.push(tree);   //根结点插入队列
    while (!q.empty()) {    //结点队列不对空,一直循环
        Tree temp = q.front();  //保存队列头结点
        q.pop();
        cout << temp->data << " ";  //输出头结点
        if (temp->leftchild != NULL) {  //左子结点或者右子结点存在,把子结点放入队列
            q.push(temp->leftchild);
        }
        if (temp->rightchild != NULL) {
            q.push(temp->rightchild);
        }
    }
    cout << "\n";
}

层序遍历例题

Talk is cheap, Show me the Code. 用下面的例题,趁热打铁练习一下层序遍历

层序遍历例题链接

在这里插入图片描述

7-2 建立与遍历二叉树

以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉链式存储结构的二叉树,然后中序遍历该二叉树并输出结点数据。

输入格式:

字符串形式的先序序列(即结点的数据类型为单个字符)

输出格式:

中序遍历结果

输入样例:

在这里给出一组输入。例如:

ABC##DE#G##F###

输出样例:

在这里给出相应的输出。例如:

CBEGDFA

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

//根据前序遍历和中序遍历构建二叉树,求出后序遍历
#include "iostream"
#include "math.h"
#include "queue"

#define MAXSize 100

using namespace std;

int mid[MAXSize];
int post[MAXSize];

typedef struct TreeNode {
    char data;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode, *Tree;
int index = 0;
string str;
//  ABC##DE#G##F###
//创建二叉树
Tree createTree() { //先序遍历,
    Tree root = new TreeNode;
    if (str[index] == '#') {    //遇到# 就是空结点
        root->data = NULL;
        return root;
    } else {
        root->data = str[index];
    }
    index++;            // 下标+1,就是左子结点,
    root->left = createTree();
    index++;            //下标+1,就是右子结点
    root->right = createTree();

    return root;
}

//后序遍历
void preOrderPrintTree(Tree tree) {
    if (tree->data == NULL) return;
    preOrderPrintTree(tree->left);
    cout << tree->data;
    preOrderPrintTree(tree->right);
}


int main() {
    cin >> str; //string str;定义在上面  19行
    Tree tree = createTree();
    preOrderPrintTree(tree);
    return 0;
}



	

7-3 前序序列创建二叉树

编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以二叉链表存储)。 例如如下的先序遍历字符串: ABC##DE#G##F### 其中“#”表示的是空格,代表一棵空树。然后再对二叉树进行中序遍历,输出遍历结果。

输入格式:

多组测试数据,每组测试数据一行,该行只有一个字符串,长度不超过100。

输出格式:

对于每组数据,

输出二叉树的中序遍历的序列,每个字符后面都有一个空格。

每组输出一行,对应输入的一行字符串。

输入样例:(及其对应的二叉树)

snap858.jpg

abc##de#g##f###

输出样例:

c b e g d f a 

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

//根据前序遍历和中序遍历构建二叉树,求出后序遍历
#include "iostream"
#include "math.h"
#include "queue"

#define MAXSize 100

using namespace std;

int mid[MAXSize];
int post[MAXSize];

typedef struct TreeNode {
    char data;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode, *Tree;
int index = 0;
string str;
//  ABC##DE#G##F###
//创建二叉树
Tree createTree() {

    Tree root = new TreeNode;
    if (str[index] == '#') {
        root->data = NULL;
        return root;
    } else {
        root->data = str[index];
    }
    index++;
    root->left = createTree();
    index++;
    root->right = createTree();

    return root;
}

//后序遍历
void preOrderPrintTree(Tree tree) {
    if (tree->data == NULL) return;
    preOrderPrintTree(tree->left);
    cout << tree->data << " ";
    preOrderPrintTree(tree->right);
}

int main() {
    while (cin >> str) {    //不输入后,cin返回0
        index = 0;
        Tree tree = createTree();
        preOrderPrintTree(tree);
        cout << "\n";
    }
    return 0;
}