美团2017笔试题 给定一颗多叉树

给定一刻多叉树,每个节点保存一个int类型数字且节点数字不重复,要求从上到下按层次打印每个节点的数字,每一层按从左到右的顺序。
要求:
(1)实现一颗多叉树
(2)根据自定义输入,构造多叉树
(3)从左到右按层输出多叉树
输入包含多行,每行有空格隔开的多个数字,第一个数字为某一个父节点的值,后面N个数字为该父节点的所有子节点的值,按从左到右的顺序排列。所有节点的值为整数,取值范围[0,100]。
如:
5 2 3
2 6 7 8
(5为根节点,有两个子节点;2为5的第一个子节点,包含三个子节点)
输出包含一行,用空格隔开。
如:
5 2 3 6 7 8

代码如下:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class MultiTree
{
public:
    int data;
    MultiTree *brother;
    MultiTree *child;

    MultiTree(int val);
    ~MultiTree();

    MultiTree* Find(int value);
    void Print();
};

MultiTree::MultiTree(int val)
    :data(val)
    ,brother(NULL)
    ,child(NULL)
{
}

MultiTree::~MultiTree()
{
    if (brother)
    {
        delete brother;
        brother = NULL;
    }
    if (child)
    {
        delete child;
        child = NULL;
    }
}

MultiTree* MultiTree::Find(int value)
{
    if (data == value)
        return this;
    else
    {
        MultiTree* res = NULL;
        if (child) res = child->Find(value);
        if (res) return res;
        if (brother) res = brother->Find(value);
        if (res) return res;
        return res;
    }
}

void MultiTree::Print()
{
    cout<<data<<" ";
    if (brother) brother->Print();
    if (child) child->Print();
}

MultiTree* CreateMultiTree()
{
    MultiTree *root = NULL;

    string str;
    stringstream ss;
    int temp;
    if (getline(cin,str))//根节点
    {
        ss.clear();
        ss.str(str);

        if (ss>>temp)//第一个节点----根节点
        {
            root = new MultiTree(temp);

            if (ss>>temp)//第二个节点
            {
                MultiTree *subnode = new MultiTree(temp);
                root->child = subnode;
                MultiTree *pre = subnode;
                while (ss>>temp)//第三个节点及其以后
                {
                    MultiTree *bronode = new MultiTree(temp);
                    pre->brother = bronode;
                    pre = bronode;
                }
            }
        }

        while (getline(cin,str))//后续结点
        {
            ss.clear();
            ss.str(str);

            if (ss>>temp)//第一个节点
            {
                MultiTree* node = root->Find(temp);
                if (!node) return root;

                if (ss>>temp)//第二个节点
                {
                    MultiTree *subnode = new MultiTree(temp);
                    node->child = subnode;
                    MultiTree *pre = subnode;
                    while (ss>>temp)//第三个节点及其以后
                    {
                        MultiTree *bronode = new MultiTree(temp);
                        pre->brother = bronode;
                        pre = bronode;
                    }
                }
            }
        }
    }
    return root;
}

int main()
{
    MultiTree* root = CreateMultiTree();
    if (root) root->Print();
    delete root;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值