树结构转换(先序转双亲)

本文介绍了一种方法,通过给定二叉树的先序遍历结果,使用C++实现了一个名为`BiTree`的类,用于构建二叉树并输出其双亲表示法。该类包含创建、层次遍历(BFS)和显示结点数据及双亲下标的函数。
摘要由CSDN通过智能技术生成

树结构转换(先序转双亲)

题目描述

给出一棵二叉树的特定字符先序遍历结果(空子树用字符’#'表示),构建该二叉树,并输出该二叉树的双亲表示法结果

双亲表示法的数组下标从0开始,根结点必定是在下标0元素,且根结点的双亲下标为-1,左右孩子按下标递增顺序排列,

结点下标是层次遍历顺序。

输入

第一个输入t,表示有t棵二叉树

接着t行,每行输入含特定字符的二叉树先序遍历序列

输出

共输出2t行

每棵二叉树输出两行,第一行输出各个结点的数值,第二行输出各结点的双亲下标

输入样例:

3
AB#C##D##
ABD##E##C##
AB##CDW###E#F##

输出样例:

A B D C
-1 0 0 1
A B C D E
-1 0 0 1 1
A B C D E W F
-1 0 0 2 2 3 4

参考代码:

#include <iostream>
#include <queue>

using namespace std;

class BiTreeNode {
private:
    char data;
    BiTreeNode *left_child;
    BiTreeNode *right_child;
    BiTreeNode *parent;
public:
    BiTreeNode() : data(' '), left_child(nullptr), right_child(nullptr), parent(nullptr) {}

    friend class BiTree;
};

class BiTree {
private:
    BiTreeNode *root;
    BiTreeNode *a[1000];
    int b[1000];
    int len;

    void Create(BiTreeNode *&t, BiTreeNode *par) {
        char ch;
        cin >> ch;
        if (ch != '#') {
            t = new BiTreeNode;
            t->data = ch;
            t->parent = par;
            Create(t->left_child, t);
            Create(t->right_child, t);
        } else
            t = nullptr;
    }

    int find(BiTreeNode *t) {
        if (t == nullptr)
            return -1;

        for (int i = 0; i < 1000; i++) {
            if (t == a[i])
                return i;
        }
    }

public:
    void Create() {
        Create(root, nullptr);
    }

    void BFS() {
        queue<BiTreeNode *> q;
        int index = 0;
        if (root != nullptr) {
            q.push(root);
            while (!q.empty()) {
                a[index] = q.front();
                b[index] = find(q.front()->parent);
                index++;

                if (q.front()->left_child != nullptr)
                    q.push(q.front()->left_child);
                if (q.front()->right_child != nullptr)
                    q.push(q.front()->right_child);
                q.pop();

            }
        }
        len = index;
    }

    void display() {
        for (int i = 0; i < len; i++) {
            cout << a[i]->data;
            if (i == len - 1)
                cout << endl;
            else
                cout << " ";
        }
        for (int i = 0; i < len; i++) {
            cout << b[i];
            if (i == len - 1)
                cout << endl;
            else
                cout << " ";
        }
    }
};

int main() {
    int t;
    cin >> t;
    while (t--) {
        BiTree tree{};
        tree.Create();
        tree.BFS();
        tree.display();
    }
    return 0;
}
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鷸鰥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值