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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拔刀能留住落樱嘛.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值