• 【算法】二叉树的存储与遍历模板


    二叉树的存储与遍历

    const int N = 1e6 + 10;
    
    // 二叉树的存储,l数组为左节点,r数组为右结点
    int l[N], r[N];
    // 存储节点的数据
    char w[N];
    // 节点的下标指针
    int idx = 0;
    
    // 先序创建
    int pre_create(int n) {
    	cin >> w[n];
    	if (w[n] == '#') return -1;
    	l[n] = pre_create(++idx);
    	r[n] = pre_create(++idx);
    	return n;
    }
    
    // 中序创建
    int in_create(int n) {
    	if (w[n] == '#') return -1;
    	l[n] = in_create(++idx);
    	cin >> w[n];
    	r[n] = in_create(++idx);
    	return n;
    }
    
    // 后序创建
    int back_create(int n) {
    	if (w[n] == '#') return -1;
    	l[n] = back_create(++idx);
    	r[n] = back_create(++idx);
    	cin >> w[n];
    	return n;
    }
    
    // 先序遍历
    void pre_print(int n){
    	if (w[n] != '#') cout << w[n] << ' ';
    	if (l[n] > 0) pre_print(l[n]);
    	if (r[n] > 0) pre_print(r[n]);
    }
    
    // 中序遍历
    void in_print(int n){
    	if (l[n] > 0) in_print(l[n]);
    	if (w[n] != '#') cout << w[n] << ' ';
    	if (r[n] > 0) in_print(r[n]);
    }
    
    // 后序遍历
    void back_print(int n){
    	if (l[n] > 0) back_print(l[n]);
    	if (r[n] > 0) back_print(r[n]);
    	if (w[n] != '#') cout << w[n] << ' ';
    }
    
    // 层序遍历
    void bfs(int root){
    	queue<int> que;
    	que.push(root);
    	while (!que.empty()) {
    		int t = que.front();
    		cout << w[t] << ' ';
    		que.pop();
    		if (l[t] > 0 && w[l[t]] != '#')
    			que.push(l[t]);
    		if (r[t] > 0 && w[r[t]] != '#')
    			que.push(r[t]);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    应用

    int main(){
        // 先序创建
        pre_create(++idx);
        // 中序创建
        // in_create(++idx);
        // 后序创建
        // back_create(++idx);
        // 先序遍历
    	pre_print(1);
    	// 中序遍历
    	in_print(1);
    	// 后序遍历
    	back_print(1);
    	// 层序遍历
    	bfs(1);
        // 测试数据abc##de#g##f###
        // 输出如下:
        // a b c d e g f 
        // c b e g d f a 
        // c g e f d b a 
        // a b c d e f g 
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    存起来,一起用

  • 相关阅读:
    整合Druid数据源
    .net6 web api中使用SqlSugar(MySQL数据库)
    老年人Stroop任务期间颈动脉粥样硬化与脑激活模式的联系:fNIRS研究
    HTTPS的加密流程
    1.DesignForSetting\1.AutoCreateMatchGroup
    Matlab:在文本和值之间转换datetimeduration
    物流行业采购协同管理系统:规范采购体系,加快物流行业采购数字化升级
    匈牙利算法讲解
    看完阿里最新产500页微服务架构笔记,感觉我格局太小
    矩阵理论复习(二)
  • 原文地址:https://blog.csdn.net/qq_39757593/article/details/134541755