当前位置: 首页 > news >正文

erchas

#include <iostream>
#include <vector>

https://gitee.com/tongchaowei/front-native-page-template/tree/main/image-display/template-01
using namespace std;

class BinaryTree {
private:
    vector<char> tree; // 存储二叉树的数组
    int size; // 二叉树的结点数

    // 计算索引i的左孩子和右孩子索引
    pair<int, int> getChildren(int i) {
        return {2 * i + 1, 2 * i + 2};
    }

    // 计算索引i的双亲索引
    int getParent(int i) {
        return (i - 1) / 2;
    }

public:
    BinaryTree() : size(0) {}

    // 插入结点
    void insert(char data) {
        tree.push_back(data);
        size++;
    }

    // 层序输出每个结点的双亲和孩子信息
    void printParentChild() {
        for (int i = 0; i < size; ++i) {
            char parent = getParent(i) >= 0 ? tree[getParent(i)] : '无';
            pair<int, int> children = getChildren(i);
            char leftChild = children.first < size ? tree[children.first] : '无';
            char rightChild = children.second < size ? tree[children.second] : '无';
            cout << tree[i] << "结点的双亲是" << parent << ",左孩子是" << leftChild << ",右孩子是" << rightChild << endl;
        }
    }

    // 前序遍历
    void preOrder(int index = 0) {
        if (index < size) {
            cout << tree[index] << " ";
            preOrder(getChildren(index).first);
            preOrder(getChildren(index).second);
        }
    }

    // 中序遍历
    void inOrder(int index = 0) {
        if (index < size) {
            inOrder(getChildren(index).first);
            cout << tree[index] << " ";
            inOrder(getChildren(index).second);
        }
    }

    // 后序遍历
    void postOrder(int index = 0) {
        if (index < size) {
            postOrder(getChildren(index).first);
            postOrder(getChildren(index).second);
            cout << tree[index] << " ";
        }
    }

    // 统计叶子结点数量,并输出每个叶子结点
    void countAndPrintLeaves() {
        int count = 0;
        for (int i = 0; i < size; ++i) {
            if (getChildren(i).first >= size && getChildren(i).second >= size) {
                cout << tree[i] << " ";
                count++;
            }
        }
        cout << endl << "叶子结点数量:" << count << endl;
    }
};

int main() {
    BinaryTree bt;
    // 假设输入的二叉树结点为:A B C D E F G H
    char nodes[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
    for (char node : nodes) {
        bt.insert(node);
    }

    cout << "层序输出每个结点的双亲和孩子信息:" << endl;
    bt.printParentChild();

    cout << "前序遍历:" << endl;
    bt.preOrder();
    cout << endl;

    cout << "中序遍历:" << endl;
    bt.inOrder();
    cout << endl;

    cout << "后序遍历:" << endl;
    bt.postOrder();
    cout << endl;

    cout << "统计叶子结点数量,并输出每个叶子结点:" << endl;
    bt.countAndPrintLeaves();

    return 0;
}

 

http://www.lryc.cn/news/488787.html

相关文章:

  • 【网络安全】SSL(一):为什么需要 Keyless SSL?
  • ggplot2 分面图等添加注释文字,相加哪里加哪里: 自定义函数 AddText()
  • 解读缓存问题的技术旅程
  • 洛谷P1597
  • 2411rust,76~79
  • vue2.0前端管理系统界面布局设置
  • 4. SQL视图
  • Simulink学习笔记【PID UG联动仿真】
  • 【Python】30个Python爬虫的实战项目!!!(附源码)
  • uni-app 界面TabBar中间大图标设置的两种方法
  • 什么是Sass,有什么特点
  • 服务器端渲染 (SSR) 与客户端渲染 (CSR)
  • 数据结构(Java版)第一期:时间复杂度和空间复杂度
  • 基于web的音乐网站(Java+SpringBoot+Mysql)
  • 用go语言后端开发速查
  • GeekChallenge 2024 第十五届极客大挑战 pwn AK
  • 禅道是什么,nas是什么,ssh是什么,finalshell是什么,git命令feat 、fix分别什么意思
  • 点云-半径搜索法-Radius Search
  • P11290 【MX-S6-T2】「KDOI-11」飞船
  • WebGIS地图框架有哪些?
  • 量化加速知识点(整理中。。。)
  • BLIP-2模型的详解与思考
  • 2024年11月22日 十二生肖 今日运势
  • 小米C++ 面试题及参考答案上(120道面试题覆盖各种类型八股文)
  • SQL SELECT 语句:基础与进阶应用
  • 微服务即时通讯系统的实现(服务端)----(1)
  • 《Spring 依赖注入方式全解析》
  • 【C++动态规划】1411. 给 N x 3 网格图涂色的方案数|1844
  • 外包干了3年,技术退步明显...
  • SpringBoot 2.x 整合 Redis