二叉树层次序序列和度数创建子女右兄弟链表示

最近在准备考研,阅读王道数据结构单科书时看到一个问题,问题是给定一棵树(不一定是二叉树)所有节点的层次次序序列和度数,创建该树的子女右兄弟链表示
书中给出的参考答案略显繁琐,所以自己考虑了一个算法,运用层次次序的特点和队列解决问题。尽管代码简单,但详述这一算法非常费劲,可能吃力不讨好,如果想要理解这一算法可以选较简单的测试样例自己手动模拟一遍,这样效果可能更好。

#include "pch.h"
#include <deque>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

struct ChildRightBrotherNode  //子女右兄弟链节点
{
    char data;  //数据域
    ChildRightBrotherNode *first_child = nullptr;  //长子指针
    ChildRightBrotherNode *next_sibling = nullptr;  //右兄弟指针
    ChildRightBrotherNode(char d) :data(d) {}
};

struct DequeNode
{
    ChildRightBrotherNode *tree_node;  //每一层的树节点指针
    string::size_type degree;   //该树节点的度数
    DequeNode(ChildRightBrotherNode *t, int d) :tree_node(t), degree(d) {}
};

void preOutPut(ChildRightBrotherNode *root)
{
    if (root != nullptr)
    {
        cout << root->data;
        preOutPut(root->first_child);
        preOutPut(root->next_sibling);
    }
}

void inOrderOutPut(ChildRightBrotherNode *root)
{
    if (root != nullptr)
    {
        inOrderOutPut(root->first_child);
        cout << root->data;
        inOrderOutPut(root->next_sibling);
    }
}

int main()
{
    deque<DequeNode> work_queue;
    vector<pair<char, string::size_type>> node_degree_array = { {'A', 3}, {'B', 2}, {'C', 1}, {'D', 2}, {'E', 0}, {'F', 0}, {'G', 0}, {'H', 0}, {'I', 0} };  //节点数据域-度数数组,各树节点从左到右按层次序排列
    string::size_type i = 0;
    ChildRightBrotherNode *root = new ChildRightBrotherNode(node_degree_array[0].first);   //创建根节点
    work_queue.push_back(DequeNode(root, node_degree_array[0].second));  //根节点及其度数入队

    while (work_queue.empty() == false)   //队列不为空重复迭代
    {
        DequeNode p = work_queue.front();   //出队一个队列节点
        work_queue.pop_front();
        ChildRightBrotherNode *q = p.tree_node;  //获取当前层的树节点指针

        string::size_type j = 1;
        for (; j <= p.degree; ++j)  //将当前树节点的所有子女节点以右兄弟链的形式链接至当前树节点
        {
            if (j == 1)
            {
                q->first_child = new ChildRightBrotherNode(node_degree_array[i+j].first);  //创建当前树节点的长子节点
                work_queue.push_back(DequeNode(q->first_child, node_degree_array[i + j].second));  //长子节点及度数入队
                q = q->first_child;
            }
            else
            {
                q->next_sibling = new ChildRightBrotherNode(node_degree_array[i + j].first);  //创建当前树节点长子节点之后的兄弟节点
                work_queue.push_back(DequeNode(q->next_sibling, node_degree_array[i + j].second));  //兄弟节点及度数入队
                q = q->next_sibling;
            }
        }
        i = i + j-1;    //将i更新为当前树节点的直接子女节点中最右侧的子女节点在node_degree_array数组中的下标
    }
    cout << "普通树的先根次序序列为:";
    preOutPut(root);   //先序遍历子女右兄弟链输出先根次序序列
    cout << endl;
    cout << "普通树的后根次序序列为:";
    inOrderOutPut(root);   //中序遍历子女右兄弟链输出后根次序序列
    cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值